ImageMan.Net使用教程:如何判断位图图像为彩色,灰阶或黑白

ImageMan.Net是由Data-Techniques公司研发的一套丰富的可用于创建图像以及文件管理程序的类以及组件。您可以使用ImageMan.Net文件的类,以确定图像是否是彩色,灰度或黑白的。

下面是一些示例代码,使用:

C#代码 
  1. // Requires a reference to the DTI.ImageMan.TessOcr assembly  
  2. using DTI.ImageMan;  
  3.    
  4. ColorAnalyzer a = new ColorAnalyzer(viewer1.Images.CurrentImage);  
  5.    
  6. Console.WriteLine("Color = {0}, Greyscale = {1}, Black&WHite = {2}", a.IsColor, a.IsGreyscale, a.IsBlackAndWhite);  
  7. if (a.IsGreyscale)  
  8. Console.WriteLine("{0} Grey Entries", a.GreyScaleEntries);  

 

值得注意几点:
1.目前只支持4、8、24和32位图像,不支持16位的图像,但在不久的将来会支持。
2.大多数情况下扫描图像是黑白图像,但也有一些灰阶像素图,在这种情况下,它会返回IsGreyscale==ture,您可以检查GreyScaleEntries的属性,看有多少灰阶项,如果只是小数目,它可能将值减少到1比特的图像。你也可以修改此代码,以确认周围聚集的黑白作品灰度项。
3.这需要在你的解决方案中引用DTI.ImageMan.TessOcr。

C#代码 
  1. The ColorAnalyzerClass:  
  2.    
  3.     public class ColorAnalyzer  
  4.     {  
  5.         int[,] histogram;  
  6.         public ColorAnalyzer(ImImage img)  
  7.         {  
  8.             histogram = Analyze.GetColorHistogram(img);  
  9.             GreyScaleEntries = 0;  
  10.    
  11.             // Check for Color and Greyscale  
  12.             forint i = 0; i < 256; i++  ) {  
  13.                 if (histogram[0, i] == histogram[1, i] && histogram[1, i] == histogram[2, i])  
  14.                 {  
  15.                     if (histogram[0, i] > 0)  
  16.                     {  
  17.                         GreyScaleEntries++;  
  18.                         IsGreyscale = true;  
  19.                     }  
  20.                 }   
  21.                 else  
  22.                 {  
  23.                     IsGreyscale = false;  
  24.                     IsColor = true;  
  25.                 }  
  26.             }  
  27.    
  28.             // This could be a black & White image so lets check  
  29.             if (IsGreyscale)  
  30.             {  
  31.                 if (GreyScaleEntries == 2 && histogram[0, 0] > 0 && histogram[0, 255] > 0)  
  32.                 {  
  33.                     IsBlackAndWhite = true;  
  34.                     IsGreyscale = false;  
  35.                 }  
  36.             }  
  37.         }  
  38.    
  39.         public bool IsColor;  
  40.         public bool IsGreyscale;  
  41.         public bool IsBlackAndWhite;  
  42.         public int GreyScaleEntries;  
  43.     }  

 

posted @ 2013-10-08 17:20  verafan  阅读(678)  评论(0编辑  收藏  举报