一个好的真正的黑白色彩矩阵是什么?

16

我想将一张彩色图片转换为黑白图像(即不是灰度图,而是纯黑白)。有人知道如何使用好的颜色矩阵来实现这个目标吗?

5个回答

29

我终于找到了解决方法:

  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倍。


4
这将生成黑白图片(不是灰度): 新的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}}) - Todd Main
1
@Otaku:不,你的代码不会起作用。它会给出更锐利的边缘,但仍然是灰度值。 - Pedery
@Pedery: 你可能没有正确地实现它,这就是完全实现黑白(没有任何灰色阴影)的方式。 - Todd Main
MSDN关于绘制图像的文档:https://msdn.microsoft.com/zh-cn/library/zbybfaff(v=vs.110).aspx - Bender Bending

6
你不需要使用颜色矩阵来实现这个,只需将编码更改为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... - Nicolas Belley
我认为在Xamarin中也可以使用BitmapEncoder类来实现这个。你要用手机扫描交货单吗? - Norbert Varga
不,我是试图绕过爱普生SDK,因为我们在使用它时遇到了一些问题,直接打印到打印机套接字中。 - Nicolas Belley

5

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

我想要黑白的图片,因为这将是图像识别过程的一部分,即无论颜色如何都能识别出某些形状。 - vbocan
在这种情况下,你可以使用类似于“Pixel[x, y] = ((R(x, y) + G(x, y) + B(x, y)) / 3) >= 127 ? 1 : 0”的代码。 - 500 - Internal Server Error
1
这是我已经尝试过的,但逐像素修改图像非常缓慢。 - vbocan

0
如果不需要安全性且不需要使用ColorMatrix,您可以使用以下方法。
它的速度大约是DrawImage和ColorMatrix的3倍。
    // make in place Bitmap 24bpp B&W from a Bitmap 24bpp color
    public static unsafe void Bmp24bppColorToBmp24bppBW(ref Bitmap bmp) {
        // sanity check
        if ( bmp.PixelFormat != PixelFormat.Format24bppRgb ) {
            return;
        }
        // lock bmp
        BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
                                          ImageLockMode.ReadWrite,
                                          PixelFormat.Format24bppRgb);
        byte* scan0 = (byte*)bmpData.Scan0.ToPointer();
        // loop bmp length
        int bmpLen = bmpData.Stride * bmp.Height;
        for ( int i = 0; i < bmpLen; i += 3 ) {
            // current pixel value
            byte pixVal = (byte)((scan0[i] + scan0[i + 1] + scan0[i + 2]) / 3.0f + 0.5f);
            // make B & W --> adjust threshold, here 200
            pixVal = (byte)(pixVal > 200 ? 255 : 0);
            // write to bmp
            scan0[i + 0] = pixVal;
            scan0[i + 1] = pixVal;
            scan0[i + 2] = pixVal;
        }
        // unlock bmp
        bmp.UnlockBits(bmpData);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接