什么是好的 TRUE 黑白颜色矩阵? [英] What would be a good TRUE black and white colormatrix?

查看:18
本文介绍了什么是好的 TRUE 黑白颜色矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to convert an image from color to B/W (i.e. no grayscale, just black and white). Does anyone have a good colormatrix to achieve this?

推荐答案

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

I've finally found a solution to my problem:

  1. 使用众所周知的颜色矩阵将图像转换为灰度.
  2. 使用ImageAttributes 类的SetThreshold 方法来设置区分黑白的阈值.
  1. Transform the image to grayscale, using well a known colormatrix.
  2. Use SetThreshold method of the ImageAttributes class to set the threshold that separates black from white.

这是 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 倍.

I've benchmarked this code and it is approximately 40 times faster than pixel by pixel manipulation.

这篇关于什么是好的 TRUE 黑白颜色矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆