繁体   English   中英

使用AFORGE.NET ProcessImage的OutOfMemoryException

[英]OutOfMemoryException using AFORGE.NET ProcessImage

我有一个试图使用AFORGE.NET处理的大型System.Drawing.Bitmap(14399 X 10799)。 我的算法在较小的图像上效果很好,但是在此尺寸的图像上,我调用Process()时会收到OutOfMemoryException。

在调用Process()之前,我的任务管理器报告该应用程序正在使用613MB,在发生异常时,它正在使用609MB。

我尝试将平台目标设置为x64,但这似乎并不影响异常的发生。

我怀疑异常与内存无关,而是其他原因,我该如何规避呢?

// lock image
        BitmapData bitmapData = bitmap.LockBits(
            new Rectangle(0, 0, bitmap.Width, bitmap.Height),
            ImageLockMode.ReadWrite, bitmap.PixelFormat);

        // step 1 - turn background to black
        ColorFiltering colorFilter = new ColorFiltering();

        colorFilter.Red = new IntRange(0, 64);
        colorFilter.Green = new IntRange(0, 64);
        colorFilter.Blue = new IntRange(0, 64);
        colorFilter.FillOutsideRange = false;

        colorFilter.ApplyInPlace(bitmapData);

        // step 2 - locating objects
        BlobCounter blobCounter = new BlobCounter();

        blobCounter.FilterBlobs = true;
        blobCounter.MinHeight = 5;
        blobCounter.MinWidth = 5;

        blobCounter.ProcessImage(bitmapData); //Crash
        Blob[] blobs = blobCounter.GetObjectsInformation();
        bitmap.UnlockBits(bitmapData);

引发了类型为'System.OutOfMemoryException'的异常。

如果您的图像包含大量斑点,则内存使用量可能会很高。 您是否尝试过使用相同尺寸的空白图像?

如果确实是由于图像中的斑点太多,则可以尝试对斑点过滤添加更多约束。

我有类似的问题,只需在Aforge.Imaging.Filters中使用ResizeBilinear过滤器或任何其他调整大小的过滤器(如果您只是为了获取一些信息就不需要完整质量的图像)以及想要进行的任何修改通过缩小找到的对象的点来完成源图像,下面是获得临时缩小图像以进行处理的简单方法

 Bitmap ResizeMyImage(Bitmap Source, int scalefactor)
        {
            int newHeight = Source.Height / scalefactor;
            int newWidth = (int)(Source.Width * (newHeight / (float)Source.Height));
            ResizeBilinear filter = new ResizeBilinear(newWidth, newHeight);
            return filter.Apply(Source);
        }

暂无
暂无

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

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