Image binarization algorithm [iterative method and greater law operator]

Source: Internet
Author: User

Binarization is a very complicated theoretical issue. It cannot be done without specific application requirements.

Simplest:

For (......)

If (pixely [I, j]> T)

Pixely [I, j] = 255;

Else

Pixely [I, j] = 0;

There are no more than 100 binarization algorithms if you consider specific issues.

/*************************************** ************************************
* Function name
* Ostuthreshold ()
* Parameters
* Lpstr lpdibbits-pointer to the source DIB image
* Long lwidth-source image width (number of bytes)
* Long lheight-source Image Height (number of bytes)
* Return Value
* If the bool operation succeeds, true is returned. Otherwise, false is returned.
* Description
* This function uses the Dajin Method for threshold segmentation binarization.
**************************************** ***********************************/
Bool winapi ostuthreshold (lpstr lpdibbits, long lwidth, long lheight)
{
// Pixel pointer to the source Image
Lpstr lpsrc;

// Pointer to the cached Image
Lpstr lpdst;

// Point to the cached image pixel pointer
Lpstr lpnewdibbits;
Hlocal hnewdibbits;

// Cyclic variable
Int I, j, T;

// Calculate the intermediate variable of the two regions
Long lp1, LS1, lp2, ls2;

// Pixel value
Unsigned char pixel;

// Grayscale histogram Array
Long lhistogram [256];

// Threshold value, maximum gray value and minimum gray value, average gray value in two regions
Unsigned char ithreshold, inewthreshold, imaxgrayvalue, imingrayvalue, imean1grayvalue, imean2grayvalue;

// Percentage of foreground points to image, and percentage of background points to image
Double w0, W1;

// Variance
Double G, tempg;

// Number of characters in each row of the image
Long llinebytes;

// Temporarily allocate memory to save new images
Hnewdibbits = localalloc (lhnd, lwidth * lheight );

If (hnewdibbits = NULL)
{
// Memory allocation failed
Return false;
}

// Lock the memory
Lpnewdibbits = (char *) locallock (hnewdibbits );

// Initialize the newly allocated memory. The initial value is 255.
Lpdst = (char *) lpnewdibbits;
Memset (lpdst, (byte) 255, lwidth * lheight );

Llinebytes = widthbytes (lwidth * 8 );

For (I = 0; I <256; I ++)
{
Lhistogram [I] = 0;
}

// Obtain the grayscale histogram, the maximum gray scale value, and the minimum gray scale value.
Imaxgrayvalue = 0;
Imingrayvalue = 255;
For (I = 0; I <lwidth; I ++)
{
For (j = 0; j <lheight; j ++)
{
Lpsrc = (char *) lpdibbits + llinebytes * j + I;
Pixel = (unsigned char) * lpsrc;
Lhistogram [pixel] ++;

// Modify the maximum and minimum gray values
If (imingrayvalue> pixel)
{
Imingrayvalue = pixel;
}
If (imaxgrayvalue <pixel)
{
Imaxgrayvalue = pixel;
}
}
}

// Traverse T and select the optimal threshold
For (t = imingrayvalue; t <imaxgrayvalue; t ++)
{
Inewthreshold = T;
Lp1 = 0;
LS1 = 0;
Lp2 = 0;
Ls2 = 0;

// Calculate the average gray level of the foreground, background, and percentage of points
For (I = imingrayvalue; I <= inewthreshold; I ++)
{
Lp1 + = lhistogram [I] * I;
LS1 + = lhistogram [I];
}
Imean1grayvalue = (unsigned char) (lp1/LS1 );
W0 = (double) (LS1)/(lwidth * lheight );
For (I = inewthreshold + 1; I <= imaxgrayvalue; I ++)
{
Lp2 + = lhistogram [I] * I;
Ls2 + = lhistogram [I];
}
Imean2grayvalue = (unsigned char) (lp2/ls2 );
W1 = 1-W0;

// Calculate inter-Class Variance
G = (double) W0 * W1
* (Imean1grayvalue-imean2grayvalue) * (imean1grayvalue-imean2grayvalue );
If (G> tempg)
{
Tempg = g;
Ithreshold = inewthreshold;
}
}

// Binarization the image based on the threshold
For (I = 0; I <lwidth; I ++)
{
For (j = 0; j <lheight; j ++)
{
Lpsrc = (char *) lpdibbits + llinebytes * j + I;
Lpdst = (char *) lpnewdibbits + llinebytes * j + I;
Pixel = (unsigned char) * lpsrc;
If (pixel <= ithreshold)
{
* Lpdst = (unsigned char) 0;
}
Else
{
* Lpdst = (unsigned char) 255;
}
}
}

// Copy the image
Memcpy (lpdibbits, lpnewdibbits, lwidth * lheight );

// Release the memory
Localunlock (hnewdibbits );
Localfree (hnewdibbits );

// Return
Return true;
}

/*************************************** **********************************
*
* Function Name:
* Thresholddib ()
*
* Parameters:
* Lpstr lpdibbits-pointer to the source DIB image
* Long lwidth-source image width (like prime number)
* Long lheight-source Image Height (like prime number)
*
* Return value:
* If the bool operation succeeds, true is returned. Otherwise, false is returned.
*
* Note:
* This function uses iterative methods to perform Threshold Value Segmentation on images.
*
**************************************** ********************************/

Bool winapi thresholddib (lpstr lpdibbits, long lwidth, long lheight)
{
 
// Pointer to the source Image
Lpstr lpsrc;
 
// Pointer to the cached Image
Lpstr lpdst;
 
// Pointer to the cached DIB image
Lpstr lpnewdibbits;
Hlocal hnewdibbits;

// Cyclic variable
Long I;
Long J;

// Pixel value
Unsigned char pixel;

// Histogram Array
Long lhistogram [256];

// Threshold value, maximum gray value and minimum gray value, average gray value in two regions
Unsigned char ithreshold, inewthreshold, imaxgrayvalue, imingrayvalue, imean1grayvalue, imean2grayvalue;

// The intermediate variable used to calculate the regional gray average.
Long lp1, lp2, LS1, ls2;

// Number of iterations
Int iiterationtimes;

// Number of bytes per line of the image
Long llinebytes;

// Temporarily allocate memory to save new images
Hnewdibbits = localalloc (lhnd, lwidth * lheight );

If (hnewdibbits = NULL)
{
// Memory allocation failed
Return false;
}
 
// Lock the memory
Lpnewdibbits = (char *) locallock (hnewdibbits );

// Initialize the newly allocated memory. The initial value is 255.
Lpdst = (char *) lpnewdibbits;
Memset (lpdst, (byte) 255, lwidth * lheight );

// Calculate the number of bytes per line of the image
Llinebytes = widthbytes (lwidth * 8 );

For (I = 0; I <256; I ++)
{
Lhistogram [I] = 0;
}

// Obtain the Histogram
Imaxgrayvalue = 0;
Imingrayvalue = 255;
For (I = 0; I <lwidth; I ++)
{
For (j = 0; j <lheight; j ++)
{
// Refers to the pointer to the nth row of the source image and the nth pixel of the source Image
Lpsrc = (char *) lpdibbits + llinebytes * j + I;
 
Pixel = (unsigned char) * lpsrc;

Lhistogram [pixel] ++;
// Modify the maximum and minimum gray value
If (imingrayvalue> pixel)
{
Imingrayvalue = pixel;
}
If (imaxgrayvalue <pixel)
{
Imaxgrayvalue = pixel;
}
}
}

// Calculate the optimal threshold value by Iteration
Inewthreshold = (imingrayvalue + imaxgrayvalue)/2;
Ithreshold = 0;
 
For (iiterationtimes = 0; ithreshold! = Inewthreshold & iiterationtimes <100; iiterationtimes ++)
{
Ithreshold = inewthreshold;
Lp1 = 0;
Lp2 = 0;
LS1 = 0;
Ls2 = 0;
// Calculate the gray average of the two regions
For (I = imingrayvalue; I <ithreshold; I ++)
{
Lp1 + = lhistogram [I] * I;
LS1 + = lhistogram [I];
}
Imean1grayvalue = (unsigned char) (lp1/LS1 );
For (I = ithreshold + 1; I <imaxgrayvalue; I ++)
{
Lp2 + = lhistogram [I] * I;
Ls2 + = lhistogram [I];
}
Imean2grayvalue = (unsigned char) (lp2/ls2 );
Inewthreshold = (imean1grayvalue + imean2grayvalue)/2;
}

// Binarization the image based on the threshold
For (I = 0; I <lwidth; I ++)
{
For (j = 0; j <lheight; j ++)
{
// Refers to the pointer to the nth row of the source image and the nth pixel of the source Image
Lpsrc = (char *) lpdibbits + llinebytes * j + I;
 
// Point to the J-row at the bottom of the target image and the pointer to the I-pixel.
Lpdst = (char *) lpnewdibbits + llinebytes * j + I;

Pixel = (unsigned char) * lpsrc;

If (pixel <= ithreshold)
{
* Lpdst = (unsigned char) 0;
}
Else
{
* Lpdst = (unsigned char) 255;
}
}
}

// Copy the image
Memcpy (lpdibbits, lpnewdibbits, lwidth * lheight );

// Release the memory
Localunlock (hnewdibbits );
Localfree (hnewdibbits );

// Return
Return true;
}

 

 

//////////////////////////////////////// ///////////////////////

 

Image binarization Principle and Implementation 1. Basic Principle of image binarization processing is about image Set the gray level of the vertex 0 Or 255That is to say, the entire image shows a significant black/white effect. About 256 gray-scale images with brightness levels will be obtained by selecting appropriate thresholds and a binarization image that still reflects the overall and local features of the image. Binary Images play a very important role in digital image processing. Especially in practical image processing, there are many systems that are implemented by binary image processing, to process and analyze a binary image, first binarization the gray image to obtain a binary image, which facilitates further processing of the image, the set nature of an image is only related to the position of a point with a pixel value of 0 or 255, and does not involve multi-level values of pixels. This simplifies processing and reduces the amount of data processing and compression. To obtain an ideal binary image, a closed and connected boundary is usually used to define areas that do not overlap. All pixels whose gray level is greater than or equal to the threshold value are determined to belong to a specific object, and their gray level value is 255 Otherwise, these pixels are excluded from the object area and the gray value is 0. Indicates the background or the area of the exception object.If a specific object has an even and consistent gray value inside and is in an even background with a gray value of other levels, you can use the threshold method to obtain a better segmentation effect. If the difference between the object and the background is not displayed on the gray level (for example, the texture is different), you can convert the difference feature to the gray level difference, and then use the threshold value selection technology to split the image. The dynamic control valve value achieves image binarization and can dynamically observe the specific results of its segmentation image. 2. The binarization program of the image adjusts the threshold through the Delphi scale control to achieve dynamic control. The procedure is as follows: Procedure tform1.button1click (Sender: tobject); var P: pbytearray; gray, x, Y: integer; begin testbmp: = tbitmap. create; changedbmp: = tbitmap. create; testbmp. assign (image1.picture); for Y: = 0 to testbmp. height-1 do begin P: = testbmp. scanline [y]; for X: = 0 to testbmp. width-1 do begin // First, grayscale the imageGray: = round (P [x * 3 + 2] * 0.3 + P [x * 3 + 1] * 0.59 + P [x * 3] * 0.11 ); if gray> trackbar1.position then // Binarization by thresholdBegin P [x * 3]: = 255; P [x * 3 + 1]: = 255; P [x * 3 + 2]: = 255; end else begin P [x * 3]: = 0; P [x * 3 + 1]: = 0; P [x * 3 + 2]: = 0; end; end; end; changedbmp. assign (testbmp); paintbox1.canvas. copymode: = srccopy; paintbox1.canvas. draw (0, 0, changedbmp); end; 3. Processing effect

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 886749

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.