【问题标题】:What would be a good TRUE black and white colormatrix?什么是好的真正的黑白彩色矩阵?
【发布时间】:2011-02-14 07:45:33
【问题描述】:

我想将图像从彩色转换为黑白(即没有灰度,只有黑白)。有没有人有一个很好的色彩矩阵来实现这一点?

【问题讨论】:

    标签: c# vb.net image image-processing colormatrix


    【解决方案1】:

    我终于找到了解决问题的方法:

    1. 使用众所周知的颜色矩阵将图像转换为灰度。
    2. 使用 ImageAttributes 类的 SetThreshold 方法设置黑白分界阈值。

    这是 C# 代码:

    using (Graphics gr = Graphics.FromImage(SourceImage)) // SourceImage is a Bitmap object
            {                
                var gray_matrix = new float[][] { 
                    new float[] { 0.299f, 0.299f, 0.299f, 0, 0 }, 
                    new float[] { 0.587f, 0.587f, 0.587f, 0, 0 }, 
                    new float[] { 0.114f, 0.114f, 0.114f, 0, 0 }, 
                    new float[] { 0,      0,      0,      1, 0 }, 
                    new float[] { 0,      0,      0,      0, 1 } 
                };
    
                var ia = new System.Drawing.Imaging.ImageAttributes();
                ia.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(gray_matrix));
                ia.SetThreshold(0.8); // Change this threshold as needed
                var rc = new Rectangle(0, 0, SourceImage.Width, SourceImage.Height);
                gr.DrawImage(SourceImage, rc, 0, 0, SourceImage.Width, SourceImage.Height, GraphicsUnit.Pixel, ia);                
            }
    

    我已经对这段代码进行了基准测试,它比逐像素操作快大约 40 倍。

    【讨论】:

    • 这将做黑白(非灰度):New ColorMatrix(New Single()() _ {New Single() {1.5, 1.5, 1.5, 0, 0}, _ New Single() {1.5, 1.5, 1.5, 0, 0}, _ New Single() {1.5, 1.5, 1.5, 0, 0}, _ New Single() {0, 0, 0, 1, 0}, _ New Single() {-1, -1, -1, 0, 1}})
    • @Otaku:不,您的代码将不起作用。它会提供更锐利的边缘,但仍然是灰度值。
    • @Pedery:你一定没有正确地实现它,这正是黑白的方法(没有任何灰色阴影)。
    • 关于绘制图像的 MSDN 文档:msdn.microsoft.com/en-us/library/zbybfaff(v=vs.110).aspx
    【解决方案2】:

    VB.NET 版本:

    Using gr As Graphics = Graphics.FromImage(SourceImage) 'SourceImage is a Bitmap object'
      Dim gray_matrix As Single()() = {
        New Single() {0.299F, 0.299F, 0.299F, 0, 0},
        New Single() {0.587F, 0.587F, 0.587F, 0, 0},
        New Single() {0.114F, 0.114F, 0.114F, 0, 0},
        New Single() {0, 0, 0, 1, 0},
        New Single() {0, 0, 0, 0, 1}
      }
      Dim ia As New System.Drawing.Imaging.ImageAttributes
      ia.SetColorMatrix(New System.Drawing.Imaging.ColorMatrix(gray_matrix))
      ia.SetThreshold(0.8)
      Dim rc As New Rectangle(0, 0, SourceImage.Width, SourceImage.Height)
      gr.DrawImage(SourceImage, rc, 0, 0, SourceImage.Width, SourceImage.Height, GraphicsUnit.Pixel, ia)
    End Using
    

    【讨论】:

      【解决方案3】:

      您不需要颜色矩阵来实现这一点,只需将编码更改为 CCITT!那只有黑白。结果保持正确,结果文件非常小。也比System.DrawImage 更高效、更快。

      这是完美的解决方案:

      public void toCCITT(string tifURL)
      {
          byte[] imgBits = File.ReadAllBytes(tifURL);
      
          using (MemoryStream ms = new MemoryStream(imgBits))
          {
              using (Image i = Image.FromStream(ms))
              {
                  EncoderParameters parms = new EncoderParameters(1);
                  ImageCodecInfo codec = ImageCodecInfo.GetImageDecoders()
                                                       .FirstOrDefault(decoder => decoder.FormatID == ImageFormat.Tiff.Guid);
      
                  parms.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
      
                  i.Save(@"c:\test\result.tif", codec, parms);
              }
          }
      }
      

      【讨论】:

      • 知道如何在 Android Xamarin 解决方案中做到这一点吗?无法访问 EncoderParameters,也无法访问 ImageCodeInfo...
      • 我认为您也可以在 Xamarin 中为此使用 BitmapEncoder 类。你打算用手机扫描送货单吗?
      • 不,我试图绕过 epson SDK,因为我们遇到了一些问题,直接打印到打印机套接字。
      【解决方案4】:

      如果您希望它看起来不错,您可能需要应用某种形式的抖动。

      这是一个完整的讨论,如果有点过时:

      http://www.efg2.com/Lab/Library/ImageProcessing/DHALF.TXT

      【讨论】:

      • 我想要黑白,因为这将是图像识别过程的一部分,即发现一些形状,而不管它们的颜色如何。
      • 在这种情况下,您可能可以通过“Pixel[x, y] = ((R(x, y) + G(x, y) + B(x) , y)) / 3) >= 127 ? 1 : 0"。
      • 这是我已经尝试过的,但是逐像素改变图像的速度非常慢。
      猜你喜欢
      • 2018-08-19
      • 2019-11-27
      • 1970-01-01
      • 2011-04-23
      • 1970-01-01
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多