Skip to main content

Fast Binarization Algorithm for VC# 2012.

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);
            }
        }


image.png

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.

Popular posts from this blog

Python 日期與時間的處理

Visual Basic 6.0 (VB6) 程式語言案例學習 (10. 條碼列印程式)

寫作:波蘭文學習之旅:1-1. 波蘭文字母與發音(注音版)

Python 日期與時間的處理

Image

Visual Basic 6.0 (VB6) 程式語言案例學習 (10. 條碼列印程式)

Image

寫作:波蘭文學習之旅:1-1. 波蘭文字母與發音(注音版)

Image

數位影像處理:最佳化處理策略之快速消除扭曲演算法

Image

Visual Basic 6.0 (VB6) 程式語言案例學習 (04. 人事考勤管理系統)

Image

用10種程式語言做影像二值化(Image binarization)

Visual Basic 6.0 (VB6) 程式語言案例學習 (07. 收據列印程式)

Image

Visual Basic .Net (VB.Net) 程式語言案例學習 (03. 場地預約系統)

Image

Visual Basic 6.0 (VB6) 程式語言案例學習 (11. 生產線拍照程式)

Image

Visual Basic .Net (VB.Net) 程式語言案例學習 (06. 題庫測驗系統)

Image