This article describes the method of image binarization (color to black and white) in C# digital image processing. Share it for your reference. The details are as follows:
//Define image binarization functionprivate static Bitmap PBinary(Bitmap src,int v) { int w = ; int h = ; Bitmap dstBitmap = new Bitmap( , ,System .Drawing .Imaging .PixelFormat .Format24bppRgb ); srcData = (new Rectangle(0, 0, w, h), , .Format24bppRgb); dstData = (new Rectangle(0, 0, w, h), , .Format24bppRgb); unsafe { byte* pIn = (byte*)srcData.(); byte* pOut = (byte*)dstData.(); byte* p; int stride = ; int r, g, b; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { p = pIn; r = p[2]; g = p[1]; b = p[0]; pOut [0]=pOut [1]=pOut [2]=(byte)(((byte)(0.2125 * r + 0.7154 * g + 0.0721 * b) >= v) ? 255 : 0); pIn += 3; pOut += 3; } pIn += - w * 3; pOut += - w * 3; } (srcData ); (dstData ); return dstBitmap; } }
I hope this article will be helpful to everyone's C# programming.