Source code of image binarization Algorithm
By: Neusoft
/*************************************** **********************************
*
* 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 is used for threshold value segmentation.
*
**************************************** ********************************/
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 {
Lhistogram [I] = 0;
}
// Obtain the Histogram
Imaxgrayvalue = 0;
Imingrayvalue = 255;
For (I = 0; I {
For (j = 0; 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 {
Imaxgrayvalue = pixel;
}
}
}
// Calculate the optimal threshold value by Iteration
Inewthreshold = (imingrayvalue + imaxgrayvalue)/2;
Ithreshold = 0;
For (iiterationtimes = 0; ithreshold! = Inewthreshold & iiterationtimes {
Ithreshold = inewthreshold;
Lp1 = 0;
Lp2 = 0;
LS1 = 0;
Ls2 = 0;
// Calculate the gray average of the two regions
For (I = imingrayvalue; I {
Lp1 + = lhistogram [I] * I;
LS1 + = lhistogram [I];
}
Imean1grayvalue = (unsigned char) (lp1/LS1 );
For (I = ithreshold + 1; 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 {
For (j = 0; 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 {
* 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;
}
This article is transferred from
Http://www.scipapers.com/ITwenzhang/chengxusheji/2007-12-01/chengxusheji234.html