我想将一张彩色图片转换为黑白图像(即不是灰度图,而是纯黑白)。有人知道如何使用好的颜色矩阵来实现这个目标吗?
我想将一张彩色图片转换为黑白图像(即不是灰度图,而是纯黑白)。有人知道如何使用好的颜色矩阵来实现这个目标吗?
我终于找到了解决方法:
以下是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倍。
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);
}
}
}
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
如果你想让它看起来还算不错,你可能需要应用某种形式的抖动。
这里有一个完整的讨论,虽然有点过时:
// 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);
}
新的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