How does BitmapData solve the problem of binarization of Verification Code images,
Sorry, this is an article for help. First, it's hard to avoid being clear and zoom in a bit. below is the picture. There is no binarization above. below is the result after binarization, in fact, I don't know what binary is, that is, a certain range turns black and white.
Problem:
Why are there a lot of colored dots on my results? None of them ......
Who can help me see how to change the code! Thank you !!
Bitmap bit1 = new Bitmap (bit); Rectangle rect1 = new Rectangle (0, 0, bit1.Width, bit1.Height); BitmapData bitd = bit1.LockBits (rect1, ImageLockMode. readWrite, PixelFormat. format24bppRgb); IntPtr srcPtr = bitd. scan0; int scanWidth = bitd. width * 3; int src_bytes = scanWidth * bitd. height; byte [] srcRGBValues = new byte [src_bytes]; Marshal. copy (srcPtr, srcRGBValues, 0, src_bytes); // grayscale processing int m = 0, I = 0, j = 0; // m indicates the row, j indicates that the column int k = 0; for (I = 0; I <bit. height; I ++) // only obtain the rows row pixel value of the image {for (j = 0; j <bit. width; j ++) {// only process the pixel data of each row of the image and Discard unused space. // note that in the bitmap structure, RGB is stored in BGR order. k = 3 * j; var grays = Convert. toInt16 (srcRGBValues [I * scanWidth + k + 2]) + Convert. toInt16 (srcRGBValues [I * scanWidth + k + 1]) + Convert. toInt16 (srcRGBValues [I * scanWidth + k + 0]); if (grays >=382) {srcRGBValues [I * scanWidth + k + 2] = Convert. toByte (255); srcRGBValues [I * scanWidth + k + 1] = Convert. toByte (255); srcRGBValues [I * scanWidth + k + 0] = Convert. toByte (255);} else {srcRGBValues [I * scanWidth + k + 2] = Convert. toByte (0); srcRGBValues [I * scanWidth + k + 1] = Convert. toByte (0); srcRGBValues [I * scanWidth + k + 0] = Convert. toByte (0) ;}} m ++;} Marshal. copy (srcRGBValues, 0, srcPtr, src_bytes); bit1.UnlockBits (bitd); Image image1 = bit1;