【问题标题】:Fillholes function in AforgeAforge 中的填充孔功能
【发布时间】:2015-05-05 15:39:51
【问题描述】:

我需要使用 Aforge 的 Fillholes 功能,它接受二进制图像。我在 c# 中使用以下代码将所有像素操作为黑色或白色像素:

bitmapimage.SetPixel(i, j, Color.FromArgb(255,255,255));   // for white pixel
bitmapimage.SetPixel(i, j, Color.FromArgb(0,0,0));         // for black pixel

但是当我将填充孔函数应用于位图图像时,我得到了这个异常:

“过滤器不支持源像素格式”

请任何人帮助我为什么会收到此异常...位图图像是否未全部使用setpixel 转换为二进制?

【问题讨论】:

    标签: c# image-processing aforge


    【解决方案1】:

    仅仅改变像素颜色不会改变图像的像素格式。

    你首先需要确保你有一个使用一些gray scale filter的灰度图像,然后通过一些threshold filter确保灰度图像是二进制的。使用这些步骤对图像进行预处理后,您可以应用FillHoles 过滤器。

    AForge.NET 提供了帮助类来合并多个过滤器,因此您可以使用 FiltersSequence 类将所有三个过滤器合并为一个总过滤器。

    假设您的原始Bitmap 图像名为bitmap,然后您可以应用填充孔过滤器,例如:

    var filter = new FiltersSequence(Grayscale.CommonAlgorithms.BT709, 
                                     new Threshold(100), new FillHoles());
    var newBitmap = filter.Apply(bitmap);
    

    【讨论】:

    • @amna 对此答案有任何反馈吗?解决方案对您有用吗?如果是,请接受答案。如果否,请添加信息以便更准确地回答您的问题。
    【解决方案2】:

    AForge FillHoles 类

    过滤器允许在二值图像中填充白色物体中的黑洞。可以使用 MaxHoleWidth 和 MaxHoleHeight 属性指定要填充的最大孔尺寸。

    过滤器只接受二进制图像,表示为 8 bpp 图像。 示例用法:

    C#

    // create and configure the filter
    FillHoles filter = new FillHoles( );
    filter.MaxHoleHeight = 20;
    filter.MaxHoleWidth  = 20;
    filter.CoupledSizeFiltering = false;
    // apply the filter
    Bitmap result = filter.Apply( image );
    

    以上发现于http://www.aforgenet.com/framework/docs/html/68bd57bd-1fd6-6c4e-4500-ed4726bc836e.htm

    您必须将您的 bitmapImage 转换为表示为 8 bpp 图像的二进制图像。这是一种方法。

    UnmanagedImage grayImage = null;
    
    if (image.PixelFormat == PixelFormat.Format8bppIndexed)
    {
        grayImage = bitmapImage;
    }
    else
    {
        grayImage = UnmanagedImage.Create(image.Width, image.Height, PixelFormat.Format8bppIndexed);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 2012-06-14
      • 1970-01-01
      相关资源
      最近更新 更多