Fast Binarization Algorithm for VC# 2012.
Fast Binarization Algorithm for VC# 2012¶
Introduction¶
Sometimes, we need to convert color images to black and white ones. Examples include black and white fax machines and photocopiers, both of which require conversion of color images to black and white ones. For this purpose, there are several approaches such as binarization and dithering, each of which includes several variations. The former includes Japanese invented Otsu’s method for automatic thresholding and the simplest fixed thresholding method.
To make it possible for readers to understand, all the program examples in this paper are accompanied with English comments so that readers not only can comprehend, but can also read the codes with ease. If you have these development tools, you can open the attached source codes. Whether you choose to compile or interpret them, you can run single-step debugging to check the computational process of the algorithm step by step. Or, you can set up breakpoints in the parts which are important or you do not understand so that the program will stop and wait until you have checked the saved values for all the variables. The most important thing is to become familiar with this binarization algorithm because it is a simpler algorithm among digital image processing techniques and does not contain any abstruse mathematic formulas. It requires only setting up thresholds and then comparing sizes to determine black or white. The key point is how to process pixels and calculate indices of pixels.
Using the Code¶
private void Form1_Paint(object sender, PaintEventArgs e) { // Establish a graphics object from external object Graphics g = e.Graphics; // if it is to open file successful. if (curBitmap != null) { // Red int iR = 0; // Green int iG = 0; // Blue int iB = 0; // Lock the bitmap's bits. Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height); System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat); // Get the address of the first line. IntPtr ptr = bmpData.Scan0; // Declare an array to hold the bytes of the bitmap. int bytes = Math.Abs(bmpData.Stride) * curBitmap.Height; byte[] rgbValues = new byte[bytes]; // Copy the RGB values into the array. System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); // Set every third value to 255. A 24bpp bitmap will binarization. for (int counter = 0; counter < rgbValues.Length; counter += 3) { // Get the red channel iR = rgbValues[counter + 2]; // Get the green channel iG = rgbValues[counter + 1]; // Get the blue channel iB = rgbValues[counter + 0]; // If the gray value more than threshold and then set a white pixel. if ((iR + iG + iB) / 3 > 100) { // White pixel rgbValues[counter + 2] = 255; rgbValues[counter + 1] = 255; rgbValues[counter + 0] = 255; } else { // Black pixel rgbValues[counter + 2] = 0; rgbValues[counter + 1] = 0; rgbValues[counter + 0] = 0; } } // Copy the RGB values back to the bitmap System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); // Unlock the bits. curBitmap.UnlockBits(bmpData); // Draw the modified image. g.DrawImage(curBitmap, 140, 10, curBitmap.Width, curBitmap.Height); } }
Download Source Code and Execute File:
https://drive.google.com/open?id=0BzHb_OyLyVZlNTB1SzV0cVNCUkk
Reference
[1] Gary Bradski and Adrian Kaehler, “Learning OpenCV: Computer Vision with the OpenCV Library,” O’REILLY, September 2008, ISBN:978-0-596-51613-0
Acknowledge
Thank you (Microsoft Visual Studio 2012, Lenna Sjööblom) very much for this great development utility and beautiful photo.