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?
Asked
Active
Viewed 2.8k times
5 Answers
29
I've finally found a solution to my problem:
- Transform the image to grayscale, using well a known colormatrix.
- Use SetThreshold method of the ImageAttributes class to set the threshold that separates black from white.
Here is the C# code:
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);
}
I've benchmarked this code and it is approximately 40 times faster than pixel by pixel manipulation.

vbocan
- 1,364
- 1
- 12
- 18
-
4This will do B&W (not grayscale): `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}})` – Todd Main Jun 11 '10 at 20:54
-
1@Otaku: Nope, your code won't work. It will give a sharper edge, but still grey values. – Pedery May 08 '11 at 22:41
-
@Pedery: you must not be implementing it correctly, that is exactly how to do black and white (no shades of grey whatsoever). – Todd Main May 08 '11 at 23:01
-
MSDN Doc on draw image: https://msdn.microsoft.com/en-us/library/zbybfaff(v=vs.110).aspx – Bender Bending Jul 28 '17 at 13:21
6
You dont need a color matrix to achive this, just simply change encoding to CCITT! That only Black & White. Result remains correct and result file size is very small. Also much more efficient and faster than System.DrawImage
.
This is the perfect solution:
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);
}
}
}

Norbert Varga
- 61
- 1
- 2
-
Any idea how to do that in a Android Xamarin solution? No access to EncoderParameters, neither ImageCodeInfo... – Nicolas Belley Aug 29 '18 at 11:34
-
I think You could use BitmapEncoder class for this in Xamarin as well. You are going to scan delivery notes with phone? – Norbert Varga Oct 13 '18 at 06:45
-
No, I was trying to circumvent the epson SDK because we had some problems with it, and print directlty to the printer socket. – Nicolas Belley Oct 16 '18 at 15:36
5
VB.NET version:
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

SSS
- 4,807
- 1
- 23
- 44
3
If you want it to look halfway decent, you'll probably want to apply some form of dithering.
Here's a full discussion, if a bit dated:

500 - Internal Server Error
- 28,327
- 8
- 59
- 66
-
I want black and white because this would be part of an image recognition process, i.e. spotting some shapes regardless of their colors. – vbocan Apr 30 '10 at 19:12
-
In that case, you can probably get away with something along the lines of "Pixel[x, y] = ((R(x, y) + G(x, y) + B(x, y)) / 3) >= 127 ? 1 : 0". – 500 - Internal Server Error Apr 30 '10 at 20:32
-
1This is what I've already tried, but altering the image pixel by pixel in very slow. – vbocan May 03 '10 at 08:33
0
If unsafe is acceptable and ColorMatrix not required, you could use the following.
It's approx. 3 times faster than DrawImage and ColorMatrix.
// 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);
}

grzwolf
- 11
- 1
- 2