Basic Global Threshold method
When the image is composed of a dark background and a lighter object, the method of extracting the object from the background is to select a threshold T that separates the two gray values, that is, any point of f (x, Y) > T (x, y) is called
As an object point, otherwise the point is called a background point. Then, the segmented image g (x, Y) is given by the following formula:
Over here:
The threshold t can be a constant, then the global threshold processing is used
The threshold T can be changed on a graph, and a variable threshold is used to process
Threshold T can be multiple, use double (multi) threshold processing
For example, the following grayscale histogram, the left can use a single threshold, the right side requires a double threshold to split.
global threshold processing with an iterative approach
In real-world processing, there is usually a big change between images, even if it is possible to use global thresholds for each picture, but we also need to be able to self-
The threshold value of the motion and estimation. Therefore, the iterative approach is used for this purpose:
1. Select an initial value for the global threshold T (average grayscale selection)
2. Split the image with T, which produces two sets of pixels: G1 consists of all pixels with a grayscale value greater than T, G2 by a grayscale value less than t
Make up all the pixels
3. Calculate average gray values M1 and M2 for G1 and G2 pixels respectively
4. Calculate a new threshold value:
t= (M1+M2)/2
5. Repeat steps 2 through 4 until the difference between T values in the continuous iteration is less than a predefined parameter T.
Nmaxiter: Maximum number of iterations; Ndiffrec: Average gray difference between bright and dark regions using given threshold value
int detectthreshold (iplimage*img, int nmaxiter, int& IDIFFREC)//Threshold Segmentation: Iterative method
{
//image information
int height = img->height;
int width = img->width;
int step = img->widthstep/sizeof (Uchar);
Uchar *data = (uchar*) img->imagedata;
Idiffrec =0;
int f[256]={0}; The histogram array
int itotalgray=0;//The grayscale value and
int itotalpixel =0;//
pixels and the pixel value of a byte bt;//point
uchar Ithrehold, inewthrehold;//threshold, New threshold
Uchar imaxgrayvalue=0,imingrayvalue=255;//maximum grayscale value and minimum grayscale value in the original image
Uchar iMeanGrayValue1 , iMeanGrayValue2;
Gets the value of (i,j), stored in the histogram array F for
(int i=0;iimaxgrayvalue)
imaxgrayvalue = BT;
f[bt]++;
}
}
Ithrehold =0;//
inewthrehold = (imingrayvalue+imaxgrayvalue)/2;//initial threshold
Idiffrec = imaxgrayvalue- Imingrayvalue;
for (int a=0; (ABS (Ithrehold-inewthrehold) >0.5) &&a