您可以使用ImageMan.Net文件的类,以确定图像是否是彩色,灰度或黑白的。
下面是一些示例代码,使用:
// Requires a reference to the DTI.ImageMan.TessOcr assembly using DTI.ImageMan; ColorAnalyzer a = new ColorAnalyzer(viewer1.Images.CurrentImage); Console.WriteLine("Color = {0}, Greyscale = {1}, Black&WHite = {2}", a.IsColor, a.IsGreyscale, a.IsBlackAndWhite); if (a.IsGreyscale) Console.WriteLine("{0} Grey Entries", a.GreyScaleEntries);
值得注意几点:
1.目前只支持4、8、24和32位图像,不支持16位的图像,但在不久的将来会支持。
2.大多数情况下扫描图像是黑白图像,但也有一些灰阶像素图,在这种情况下,它会返回IsGreyscale==ture,您可以检查GreyScaleEntries的属性,看有多少灰阶项,如果只是小数目,它可能将值减少到1比特的图像。你也可以修改此代码,以确认周围聚集的黑白作品灰度项。
3.这需要在你的解决方案中引用DTI.ImageMan.TessOcr。
The ColorAnalyzerClass: public class ColorAnalyzer { int[,] histogram; public ColorAnalyzer(ImImage img) { histogram = Analyze.GetColorHistogram(img); GreyScaleEntries = 0; // Check for Color and Greyscale for( int i = 0; i < 256; i++ ) { if (histogram[0, i] == histogram[1, i] && histogram[1, i] == histogram[2, i]) { if (histogram[0, i] > 0) { GreyScaleEntries++; IsGreyscale = true; } } else { IsGreyscale = false; IsColor = true; } } // This could be a black & White image so lets check if (IsGreyscale) { if (GreyScaleEntries == 2 && histogram[0, 0] > 0 && histogram[0, 255] > 0) { IsBlackAndWhite = true; IsGreyscale = false; } } } public bool IsColor; public bool IsGreyscale; public bool IsBlackAndWhite; public int GreyScaleEntries; }