/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* function name: cvOtsu2D ()
* function parameters: CvMat * pGrayMat: gray-scale patterns for matrix
* the return value: int nThreshold
* function description: realize the gray image binarization segmentation - the between-cluster variance method (two-dimensional Otsu algorithm)
* note: at the time of construct two-dimensional histogram, using the mean gray level 3 * 3 neighborhood
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Int cvOtsu2D (CvMat * pGrayMat)
{
Double dHistogram [256] [256].//to establish the two-dimensional gray histogram
Double dTrMatrix=0.0;//the discrete matrix trace
Int height=pGrayMat - & gt; Rows;
Int width=pGrayMat - & gt; Cols.
Int N=height * width;//the total number
Int I, j;
for(i=0; I & lt; 256; I++)
{
for(j=0; J & lt; 256; J++)
DHistogram [I] [j]=0.0;//initialize a variable
}
for(i=0; I & lt; Height; I++)
{
for(j=0; J & lt; Width; J++)
{
Unsigned char nData1=(unsigned char) cvGetReal2D (pGrayMat, I, j);//the current grey value
Unsigned char nData2=0;
Int nData3=0;//note 9 values can be more than one byte
For (int m=I - 1; M & lt;=I + 1; M++)
{
For (int n=j - 1; N & lt;=j + 1; N++)
{
If ((m & gt;=0) & amp; & (m & lt; Height) & amp; & (n & gt;=0) & amp; & (n & lt; Width))
NData3 +=(unsigned char) cvGetReal2D (pGrayMat, m, n);//the current grey value
}
}
NData2=(unsigned char) (nData3/9);//for cross-border index value of zero padding, neighborhood average
DHistogram [nData1] [nData2] + +;
}
}
for(i=0; I & lt; 256; I++)
for(j=0; J & lt; 256; J++)
DHistogram [j]/[I]=N;//get the probability distribution of the normalized
Double Pai=0.0;//target zone average vector component I
Double Paj=0.0;//target zone average vector j component
Double Pbi company=0.0;//background area average vector component I
Double Pbj=0.0;//the background area average vector j component
Double Pti=0.0;//global average vector component I
Double Ptj=0.0;//global average vector j component
Double W0=0.0;//target zone of joint probability density
Double W1=0.0;//background area of the joint probability density
Double dData1=0.0;
Double dData2=0.0;
Double dData3=0.0;
Double dData4=0.0;//intermediate variable
Int nThreshold_s=0;
Int nThreshold_t=0;
Double temp=0.0;//to seek the maximum
for(i=0; I & lt; 256; I++)
{
for(j=0; J & lt; 256; J++)
{
Pti +=I * dHistogram [I] [j];
Ptj +=j * dHistogram [I] [j];
}
}
for(i=0; I & lt; 256; I++)
{
for(j=0; J & lt; 256; J++)
{
W0 +=dHistogram [I] [j];
DData1 +=I * dHistogram [I] [j];
DData2 +=j * dHistogram [I] [j];
W1=1 - W0;
DData3=Pti - dData1;
DData4=Ptj - dData2;
Pai=dData1/W0;
Paj=dData2/W0;
Pbi company=dData3/W1;
Pbj=dData4/W1;//get two mean vector, expressed in four component
DTrMatrix=(Pti - dData1) (W0 * * (W0 * Pti dData1) + (W0 * Ptj - dData2) * (W0 * Ptj - dData2))/(W0 * W1);
If (dTrMatrix & gt; Temp)
{
Temp=dTrMatrix;
NThreshold_s=I;
NThreshold_t=j;
}
}
}
NThreshold=(nThreshold_s + nThreshold_t)/2;//return threshold, there are a variety of forms, can one alone, also can//is the mean of the two
Return nThreshold;
}
Int main ()
{
Img=cvLoadImage IplImage * (" C: \ \ Miss BMP, 0).//load the picture
CvNamedWindow (" Miss ", CV_WINDOW_AUTOSIZE);//create a display window
CvShowImage (" Miss ", img);//display picture
CvNamedWindow (" two-dimensional Otsu ", CV_WINDOW_AUTOSIZE);
CvMat * imgMat=cvCreateMat (img - & gt; Height, img - & gt; Width, CV_8UC1);//create the matrix, its type is CV_8UC1
CvConvert (img, imgMat);//convert the image into matrix form
Int t=cvOtsu2D (imgMat);//for two-dimensional threshold segmentation threshold
CvThreshold (img, img, t., 255, CV_THRESH_BINARY);//get the threshold segmentation of image
CvShowImage (" two-dimensional Otsu, "img);
cvWaitKey(0);//to keep the image display window
CvReleaseImage (& amp; Img);//release image resources
CvReleaseMat (& amp; ImgMat);//release matrix resources
CvDestroyAllWindows ();//destruction of window
return 0;
}
CodePudding user response:
Under the code but not looking at the effect and one dimensional contrast, if a good many of samples