简体   繁体   English

Emgu CV高分辨率图像拼接问题

[英]Emgu cv high resolution images stitching Issue

I am using EmguCV library to stitch images. 我正在使用EmguCV库来缝合图像。 Its working fine for small images but getting exception when processing high resolution images or images above 20MB size or even if I try to process more than 30 images it fails. 它适用于小图像,但在处理高分辨率图像或20MB以上的图像时,即使我尝试处理30幅以上的图像,也会出现异常,但异常。

Libraries I am using 我正在使用的图书馆

Emgu.CV.UI
Emgu.CV.UI.GL
Emgu.CV.World
opencv_core2410
opencv_imgproc2410

Code

List<Image<Bgr, Byte>> sourceImages = new List<Image<Bgr,byte>>();
foreach (string path in ImgPath)
    sourceImages.Add(new Image<Bgr, Byte>(path));
using (Stitcher stitcher = new Stitcher(false))
{
    using (VectorOfMat vm = new VectorOfMat())
    {
        Mat result = new Mat();
        vm.Push(sourceImages.ToArray());
        stitcher.Stitch(vm, result);
        if (result.Bitmap != null)
        {
            result.Bitmap.Save(Application.StartupPath + "\\imgs\\StitchedImage.png");
        }
        else
        {
            MessageBox.Show("Some thing went wrong"); return null;
        }
    }
}

Exception 例外

((Emgu.CV.MatDataAllocator)(result))._dataHandle.Target' threw an exception of type 'System.InvalidOperationException

Image 图片 在此处输入图片说明 在此处输入图片说明

I was fairly certain that you are running into a memory issue and so I went ahead and made a simple console app targeting .Net 4.7.2, using the latest EmguCV package from NuGet , which is version 3.4.3.3016, and using the code below on sample data from Adobe, which can be downloaded here . 我相当确定您遇到了内存问题,因此我继续使用NuGet的最新EmguCV软件包(版本3.4.3.3016)并使用以下代码制作了一个针对.Net 4.7.2的简单控制台应用程序。有关Adobe样本数据的信息,可在此处下载。 If I compile as "AnyCPU" with "prefer 32 bit" and run this code against the rio image set (I loaded up the png's for the test purposes) and let it run the memory will slowly jump up until it hits close to 3 GB and then shortly thereafter crashes giving the exception about the refcount. 如果我使用“ prefer 32 bit”作为“ AnyCPU”进行编译,并针对rio图像集运行此代码(出于测试目的,我加载了png的图像),然后让它运行,内存将缓慢上升,直到达到3 GB然后不久便崩溃,给出有关refcount的异常。 Pretty clearly a memory issue. 很明显是内存问题。 I then recompiled targeting 64 bit and was able to successfully run the code. 然后,我重新编译了针对64位的代码,并能够成功运行代码。 The memory usage peaked out around 6 GB. 内存使用量达到了约6 GB的峰值。 So, with that in mind, I would be fairly certain that your issue is also memory related. 因此,考虑到这一点,我可以肯定地说您的问题也与内存有关。 You have yet to answer the question on whether you are building a 64 bit app or not, but based on what you are seeing, I would guess that you are not. 您尚未回答是否要构建64位应用程序的问题,但是根据您所看到的内容,我想您不是。 So, the solution to your problem is to compile as 64 bit and then be sure you have enough memory. 因此,解决问题的方法是编译为64位,然后确保您有足够的内存。 With the rio test set it jumped up to close to 6 GB. 使用rio测试集,它跳升至接近6 GB。 Without having your images I can't tell you how large it might grow, but these type of operations are pretty memory intensive - so more is better. 没有您的图像,我无法告诉您它可能会增长多少,但是这些类型的操作占用大量内存,因此更好。 This would explain both the issue with large image files and the issue with a large number of small image files. 这样既可以解释大型图像文件的问题,也可以解释大量小型图像文件的问题。 I was able to successfully process sets of images that were between 10 and 20 images using a 32 bit build, but as soon as I moved to the 50+ image sets it would only work with a 64 bit build due to the memory requirements. 我能够使用32位版本成功处理10到20张图像之间的图像集,但是由于内存需求,一旦移至50+张图像集,它就只能使用64位版本。

var images = Directory.EnumerateFiles(@"C:\test\adobe\rio", "*.png", SearchOption.TopDirectoryOnly).Select(x => new Mat(x)).ToArray();
using(var stitcher = new Stitcher(false))
{
    using (var vm = new VectorOfMat(images))
    {
        var result = new Mat();
        stitcher.Stitch(vm, result);
        result.Bitmap.Save(@"C:\test\adobe\rio_stitched.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        result.Dispose();
    }
}
foreach (var image in images) { image.Dispose(); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM