Image binarization algorithm based on simple image statistics (simple image statistics, SIS.

Source: Internet
Author: User

This is a simple algorithm. It is a global binary algorithm with fast execution speed.

The algorithm process is described as follows:

Perform the following processing for each pixel:

1. Calculate the horizontal and vertical gradient of the current pixel. (Two gradients are calculated | I (x + 1, Y)-I (X-1, Y) | and | I (X, Y + 1)-I (X, y-1) | );

2. Take the maximum values of the two gradients as the weight. (Weight is calculated as maximum of two gradients );

3. Update the sum of weights. (Sum of weights is updated (weighttotal + = weight ));

4. Update the sum of weighted pixels (sum of weighted pixel values is updated (total + = weight * I (x, y )));

Then, the final threshold value is de-weighted and the sum of the pixels and weights.

This algorithm is described in the image processing lab in C # code.

In terms of actual operations, I think binary processing should only process grayscale images, so that the meaning is clear. Therefore, in the code, I provided a function to determine whether an image is a grayscale image:

Private bool isgraybitmap (bitmap BMP) {bool isgray; If (BMP. pixelformat = pixelformat. format8bppindexed )//. in net, the gray scale must first be the index image {isgray = true; If (BMP. palette. entries. length! = 256) // This requirement does not actually exist in PS isgray = false; else {for (INT x = 0; x <BMP. palette. entries. length; X ++) // check whether the values of each part in the palette are equal and must be equal to the order in which they appear in the palette {If (BMP. palette. entries [X]. r! = X | BMP. palette. Entries [X]. G! = X | BMP. palette. Entries [X]. B! = X) {isgray = false; break ;}}} else {isgray = false;} return isgray ;}

In fact, in the concept of PS, the number of gray-scale images is not necessarily 256, as long as the component values of each element in the palette are equal to the order in which they appear in the palette, PS considers it a grayscale image.

For ease of processing, I added a function to convert images in other modes to images in gray mode:

Private bitmap converttograymodebitmap (bitmap BMP) {int X, Y, srcstride, deststride, width, height; byte * srcdata, destdata; bitmapdata bmp data = BMP. lockbits (New rectangle (0, 0, BMP. width, BMP. height), imagelockmode. readonly, pixelformat. format24bpprgb); bitmap graybmp = new Bitmap (BMP. width, BMP. height, pixelformat. format8bppindexed); colorpalette pal = graybmp. palette; For (y = 0; y <pal. entri Es. length; y ++) pal. entries [y] = color. fromargb (255, Y); // you can specify the graybmp color palette for the grayscale image. palette = pal; // when the first parameter is as big as the image, and when the reading format is the same as the original one, the call time of the function is 0, in addition, the scan0 values of bitmapdata are the same after each call, while in most cases, the same parameter calls the function to return different scan0 values, GDI + allocates the same size of memory space as the corresponding bitmap when creating the image. // then we can call this function again when loading the image and remember the value of scan0, then, using a pointer to operate this area is equivalent to operating the image. Instead of locking and unlock every time. // at this level, the function is similar to getdibits. Bitmapdata graybmp DATA = graybmp. lockbits (New rectangle (0, 0, graybmp. width, graybmp. height), imagelockmode. readwrite, pixelformat. format8bppindexed); width = BMP data. width; Height = BMP data. height; srcstride = BMP data. stride; deststride = graybmp data. stride; // This value is not necessarily equal to width * height * color depth/8 for (y = 0; y 

In the minds of many people, the so-called gray images are images such as R = G = B. They can only be said to be out-of-the-box people. They are too unprofessional. Such an image can only be regarded as a color image with the same color weight and be corrected again.

The algorithms described above involve four fields of images. Therefore, we use a series similar to the Photoshop algorithm-styles-to search for the Sentinel algorithm in the edge article to expand the border of the backup image, the expanded data is filled with the value at the original image boundary. Because only four fields are involved, you need to add two pixels to the image width and height.

I still like to allocate memory for data filling, and I prefer to use APIs directly. This may be related to my personal habits. You can also process it in your own way.

Private byte getsimplestatisticsthreshold (Bitmap graybmp) {int width, height, stride, X, Y; int clonestride, ex, ey; int Weight = 0; long sumweight = 0; // for large images, this number will overflow, so use the long type variable byte * pointer, scan0, clonedata; bitmapdata graybmp DATA = graybmp. lockbits (New rectangle (0, 0, graybmp. width, graybmp. height), imagelockmode. readonly, pixelformat. format8bppindexed); width = graybmp. width; Height = graybmp. height; stride = graybmp data. stride; clonestride = width + 2; scan0 = (byte *) graybmp data. scan0; clonedata = (byte *) globalalloc (gptr, clonestride * (height * 2); For (y = 0; y 

In general, for the sake of program speed, I suggest you expand some small functions, such as the ABS function above, directly write if (ex <0) ex =-Ex will be faster. You can see the difference through the disassembly below:

                Ex = Math.Abs(Ex);00000161  js          00000167 00000163  mov         eax,esi 00000165  jmp         0000016E 00000167  mov         ecx,esi 00000169  call        638C54E4 0000016e  mov         esi,eax                 if (Ex < 0) Ex = -Ex;00000170  test        eax,eax 00000172  jge         00000176 00000174  neg         esi 

  

The effect of the split may still need to be explained using specific images. I will not comment too much here.

Engineering: http://files.cnblogs.com/Imageshop/ThresholdUseSIS.rar

* **************************** Basically, I do not provide source code, however, I will try to use text to clearly describe the corresponding algorithm or provide the reference documentation ************************* *

* ******************************* Because it is written by your own efforts and practices. the effect is exactly what you are, people must rely on their own ****************************

* ************************* Author: laviewpbt time: 2013.7.15 contact QQ: 33184777 reprinted. Please keep the information of this bank *************************

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.