2015-05-05 55 views
1

我需要使用Aforge的Fillholes函数,它接受二进制图像。我操作的所有像素为黑色或白色像素使用C#下面的代码:Aforge中的填充函数

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

但是当我申请fillholes功能位图图像,我得到这个异常:

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

请帮助为什么我得到这个例外...是所有使用setpixel没有转换为二进制位图图像?

回答

0

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

你首先需要确保你有一个灰度图像使用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); 
+0

@amna对此问题的任何反馈?该解决方案是否适合您?如果是,请接受答案。如果否,请添加信息,以便您的问题得到更准确的回答。 –

0

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); 
}