opencv閾值法分割圖像

 

http://www.cnblogs.com/skyseraph/archive/2010/12/21/1913058.html


1。手動設定閾值

3/*手動設置閥值*/
4IplImage* binaryImg = cvCreateImage(cvSize(w, h),IPL_DEPTH_8U, 1);
5cvThreshold(smoothImgGauss,binaryImg,71,255,CV_THRESH_BINARY);
6cvNamedWindow("cvThreshold", CV_WINDOW_AUTOSIZE );
7cvShowImage("cvThreshold", binaryImg );

2。自適應法
(1)將圖像平均分爲若干行列的子圖像,行數和列數可以根據應用需要進行選取;    
(2)對每一個子圖像,利用全局閾值化方法求最優閾值,所得閾值用圖像表示;    
(3)將表示閾值的圖像進行必要的延拓後進行濾波;    
(4)將濾波結果作爲子圖像的新閾值對每個子圖像進行閾值化處理

10/*自適應閥值 //計算像域鄰域的平均灰度,來決定二值化的值*/
11IplImage* adThresImg= cvCreateImage(cvSize(w, h),IPL_DEPTH_8U,1);
12double max_value=255;
13int adpative_method=CV_ADAPTIVE_THRESH_GAUSSIAN_C;//CV_ADAPTIVE_THRESH_MEAN_C
14 int threshold_type=CV_THRESH_BINARY;
15int block_size=3;//閾值的象素鄰域大小
16 int offset=5;//窗口尺寸
17  cvAdaptiveThreshold(smoothImgGauss,adThresImg,max_value,adpative_method,threshold_type,block_size,offset);
18cvNamedWindow("cvAdaptiveThreshold", CV_WINDOW_AUTOSIZE );
19cvShowImage("cvAdaptiveThreshold", adThresImg );
20cvReleaseImage(&adThresImg);

3.最大熵閥值分割法

圖像的信息熵反映了圖像的總體概貌。若圖像中包含目標,則在目標與背景可分割的交界處信息量(即熵)最大。
22/*最大熵閥值分割法*/
23IplImage* imgMaxEntropy= cvCreateImage(cvGetSize(imgGrey),IPL_DEPTH_8U,1);
24MaxEntropy(smoothImgGauss,imgMaxEntropy);
25cvNamedWindow("MaxEntroyThreshold", CV_WINDOW_AUTOSIZE );
26cvShowImage("MaxEntroyThreshold", imgMaxEntropy );//顯示圖像
27  cvReleaseImage(&imgMaxEntropy );
///////////////////////////////////////////////////////////
1/*============================================================================
2= 代碼內容:最大熵閾值分割
3= 修改日期:2009-3-3
4= 作者:crond123
5= 博客:http://blog.csdn.net/crond123/
6= E_Mail:[email protected]
7===============================================================================*/
8// 計算當前位置的能量熵
9double caculateCurrentEntropy(CvHistogram* Histogram1,int cur_threshold,entropy_state state)
10{
11int start,end;
12int total=0;
13double cur_entropy=0.0;
14if(state== back)
15{
16start =0;
17end = cur_threshold;
18}
19else
20{
21start = cur_threshold;
22end =256;
23}
24for(int i=start;i<end;i++)
25{
26total += (int)cvQueryHistValue_1D(Histogram1,i);//查詢直方塊的值 P304
27}
28for(int j=start;j<end;j++)
29{
30if((int)cvQueryHistValue_1D(Histogram1,j)==0)
31continue;
32double percentage= cvQueryHistValue_1D(Histogram1,j)/total;
33/*熵的定義公式*/
34cur_entropy+=-percentage*logf(percentage);
35/*根據泰勒展式去掉高次項得到的熵的近似計算公式
36cur_entropy += percentage*percentage;*/
37}
38return cur_entropy;
39// return (1-cur_entropy);
40}
41
42//尋找最大熵閾值並分割
43void MaxEntropy(IplImage*src,IplImage*dst)
44{
45assert(src!= NULL);
46assert(src->depth==8&& dst->depth==8);
47assert(src->nChannels==1);
48CvHistogram* hist= cvCreateHist(1,&HistogramBins,CV_HIST_ARRAY,HistogramRange);//創建一個指定尺寸的直方圖
49//參數含義:直方圖包含的維數、直方圖維數尺寸的數組、直方圖的表示格式、方塊範圍數組、歸一化標誌
50cvCalcHist(&src,hist);//計算直方圖
51double maxentropy=-1.0;
52int max_index=-1;
53// 循環測試每個分割點,尋找到最大的閾值分割點
54for(int i=0;i<HistogramBins;i++)
55{
56double cur_entropy= caculateCurrentEntropy(hist,i,object)+caculateCurrentEntropy(hist,i,back);
57if(cur_entropy>maxentropy)
58{
59maxentropy= cur_entropy;
60max_index= i;
61}
62}
63cout<<"The Threshold of this Image in MaxEntropy is:"<<max_index<<endl;
64cvThreshold(src, dst, (double)max_index,255, CV_THRESH_BINARY);
65cvReleaseHist(&hist);
66}
//////////////////////////////////////////////////////////////////////////


4.基本全局閥值法
p(x,y)代表點(x,y)處的灰度值,q(x,y)代表點鄰域的某種局部特性。
如果閾值的選取只與p(x,y)有關,則是全局閾值,全局閾值是利用全局信息(例如整個圖像的灰度直方圖)得到的閾值,它僅與全圖各像素的本事性質有關,對全圖使用;如果閾值的選取與p(x,y),q(x,y)有關,則是局部閾值。
29/*基本全局閥值法*/
30IplImage* imgBasicGlobalThreshold= cvCreateImage(cvGetSize(imgGrey),IPL_DEPTH_8U,1);
31cvCopyImage(srcImgGrey,imgBasicGlobalThreshold);
32int pg[256],i,thre;
33for (i=0;i<256;i++) pg[i]=0;
34for (i=0;i<imgBasicGlobalThreshold->imageSize;i++)// 直方圖統計
35  pg[(BYTE)imgBasicGlobalThreshold->imageData[i]]++;
36thre = BasicGlobalThreshold(pg,0,256);// 確定閾值
37  cout<<"The Threshold of this Image in BasicGlobalThreshold is:"<<thre<<endl;//輸出顯示閥值
38  cvThreshold(imgBasicGlobalThreshold,imgBasicGlobalThreshold,thre,255,CV_THRESH_BINARY);// 二值化
39  cvNamedWindow("BasicGlobalThreshold", CV_WINDOW_AUTOSIZE );
40cvShowImage("BasicGlobalThreshold", imgBasicGlobalThreshold);//顯示圖像
41  cvReleaseImage(&imgBasicGlobalThreshold);
//////////////////////////////////////////////////////////////////
1 /*============================================================================ 2 = 代碼內容:基本全局閾值法 3 ==============================================================================*/ 4 int BasicGlobalThreshold(int *pg,int start,int end) 5 { // 基本全局閾值法 6 int i,t,t1,t2,k1,k2; 7 double u,u1,u2; 8 t=0; 9 u=0; 10 for (i=start;i<end;i++) 11 { 12 t+=pg[i]; 13 u+=i*pg[i]; 14 } 15 k2=(int) (u/t); // 計算此範圍灰度的平均值 16 do 17 { 18 k1=k2; 19 t1=0; 20 u1=0; 21 for (i=start;i<=k1;i++) 22 { // 計算低灰度組的累加和 23 t1+=pg[i]; 24 u1+=i*pg[i]; 25 } 26 t2=t-t1; 27 u2=u-u1; 28 if (t1) 29 u1=u1/t1; // 計算低灰度組的平均值 30 else 31 u1=0; 32 if (t2) 33 u2=u2/t2; // 計算高灰度組的平均值 34 else 35 u2=0; 36 k2=(int) ((u1+u2)/2); // 得到新的閾值估計值 37 } 38 while(k1!=k2); // 數據未穩定,繼續 39 //cout<<"The Threshold of this Image in BasicGlobalThreshold is:"<<k1<<endl; 40 return(k1); // 返回閾值 41 }
///////////////////////////////////////////////////////////////////////
5.
OTSU(最大類間差分)
其基本思
想是用閾值把 圖像像素劃分爲兩類,通過使劃分
後得到的兩類的類間方差最大來確定最佳閾值 
44     IplImage* imgOtsu = cvCreateImage(cvGetSize(imgGrey),IPL_DEPTH_8U,1);
45     cvCopyImage(srcImgGrey,imgOtsu);
46     int thre2;
47     thre2 = otsu2(imgOtsu);
48     cout<<"The Threshold of this Image in Otsu is:"<<thre2<<endl;//輸出顯示閥值
49     cvThreshold(imgOtsu,imgOtsu,thre2,255,CV_THRESH_BINARY);  //  二值化    
50     cvNamedWindow("imgOtsu", CV_WINDOW_AUTOSIZE );
51     cvShowImage( "imgOtsu", imgOtsu);//顯示圖像    
52     cvReleaseImage(&imgOtsu);

////////////////////////////////////////////////////////////////
1 /*======================================================================*/
 2 /* OTSU global thresholding routine */
 3 /* takes a 2D unsigned char array pointer, number of rows, and */
 4 /* number of cols in the array. returns the value of the threshold */
 5 /*parameter: 
 6 *image --- buffer for image
 7 rows, cols --- size of image
 8 x0, y0, dx, dy --- region of vector used for computing threshold
 9 vvv --- debug option, is 0, no debug information outputed
10 */
11 /*
12 OTSU 算法可以說是自適應計算單閾值(用來轉換灰度圖像爲二值圖像)的簡單高效方法。
13 下面的代碼最早由 Ryan Dibble提供,此後經過多人Joerg.Schulenburg, R.Z.Liu 等修改,補正。
14 算法對輸入的灰度圖像的直方圖進行分析,將直方圖分成兩個部分,使得兩部分之間的距離最大。
15 劃分點就是求得的閾值。
16 */
17 /*======================================================================*/
18 int otsu (unsigned char *image, int rows, int cols, int x0, int y0, int dx, int dy, int vvv)
19 {
20     
21     unsigned char *np; // 圖像指針
22     int thresholdValue=1; // 閾值
23     int ihist[256]; // 圖像直方圖,256個點
24     
25     int i, j, k; // various counters
26     int n, n1, n2, gmin, gmax;
27     double m1, m2, sum, csum, fmax, sb;
28     
29     // 對直方圖置零
30     memset(ihist, 0, sizeof(ihist));
31     
32     gmin=255; gmax=0;
33     // 生成直方圖
34     for (i = y0 + 1; i < y0 + dy - 1; i++) 
35     {
36         np = (unsigned char*)image[i*cols+x0+1];
37         for (j = x0 + 1; j < x0 + dx - 1; j++)
38         {
39             ihist[*np]++;
40             if(*np > gmax) gmax=*np;
41             if(*np < gmin) gmin=*np;
42             np++; /* next pixel */
43         }
44     }
45     
46     // set up everything
47     sum = csum = 0.0;
48     n = 0;
49     
50     for (k = 0; k <= 255; k++) 
51     {
52         sum += (double) k * (double) ihist[k]; /* x*f(x) 質量矩*/
53         n += ihist[k]; /* f(x) 質量 */
54     }
55     
56     if (!n) 
57     {
58         // if n has no value, there is problems...
59         fprintf (stderr, "NOT NORMAL thresholdValue = 160\n");
60         return (160);
61     }
62     
63     // do the otsu global thresholding method
64     fmax = -1.0;
65     n1 = 0;
66     for (k = 0; k < 255; k++)
67     {
68         n1 += ihist[k];
69         if (!n1) 
70         { 
71             continue; 
72         }
73         n2 = n - n1;
74         if (n2 == 0)
75         { 
76             break; 
77         }
78         csum += (double) k *ihist[k];
79         m1 = csum / n1;
80         m2 = (sum - csum) / n2;
81         sb = (double) n1 *(double) n2 *(m1 - m2) * (m1 - m2);
82         /* bbg: note: can be optimized. */
83         if (sb > fmax) 
84         {
85             fmax = sb;
86             thresholdValue = k;
87         }
88     }
89     
90     // at this point we have our thresholding value
91     
92     // debug code to display thresholding values
93     if ( vvv & 1 )
94         fprintf(stderr,"# OTSU: thresholdValue = %d gmin=%d gmax=%d\n",
95         thresholdValue, gmin, gmax);
96     
97     return(thresholdValue);
98 } 
////////////////////////////////////////////////////////////////////////////////


6.
/*上下閥值法:利用正態分佈求可信區間*/

55IplImage* imgTopDown= cvCreateImage( cvGetSize(imgGrey), IPL_DEPTH_8U,1 );
56cvCopyImage(srcImgGrey,imgTopDown);
57CvScalar mean ,std_dev;//平均值、 標準差
58double u_threshold,d_threshold;
59cvAvgSdv(imgTopDown,&mean,&std_dev,NULL);
60u_threshold= mean.val[0]+2.5* std_dev.val[0];//上閥值
61d_threshold= mean.val[0]-2.5* std_dev.val[0];//下閥值
62//u_threshold = mean + 2.5 * std_dev;//錯誤
63//d_threshold = mean - 2.5 * std_dev;
64cout<<"The TopThreshold of this Image in TopDown is:"<<d_threshold<<endl;//輸出顯示閥值
65cout<<"The DownThreshold of this Image in TopDown is:"<<u_threshold<<endl;
66cvThreshold(imgTopDown,imgTopDown,d_threshold,u_threshold,CV_THRESH_BINARY_INV);//上下閥值
67cvNamedWindow("imgTopDown", CV_WINDOW_AUTOSIZE );
68cvShowImage("imgTopDown", imgTopDown);//顯示圖像
69cvReleaseImage(&imgTopDown);
/////////////////////////////////////////////////////////////////////
7.迭代法

迭代法是基於逼近的思想,其步驟如下: 
1. 求出圖象的最大灰度值和最小灰度值,分別記爲ZMAX和ZMIN,令初始閾值T0=(ZMAX+ZMIN)/2; 
2. 根據閾值TK將圖象分割爲前景和背景,分別求出兩者的平均灰度值ZO和ZB 
3. 求出新閾值TK+1=(ZO+ZB)/2; 
4. 若TK=TK+1,則所得即爲閾值;否則轉2,迭代計算。
當TK=TK+1時,說明閾值已經達到穩定了,不需在進行迭代,當TK!=TK+1時,說明閾值還沒有穩定,需要用迭代再進一步來確定。
72IplImage* imgIteration= cvCreateImage( cvGetSize(imgGrey), IPL_DEPTH_8U,1 );
73cvCopyImage(srcImgGrey,imgIteration);
74int thre3,nDiffRec;
75thre3 =DetectThreshold(imgIteration,100, nDiffRec);
76cout<<"The Threshold of this Image in imgIteration is:"<<thre3<<endl;//輸出顯示閥值
77cvThreshold(imgIteration,imgIteration,thre3,255,CV_THRESH_BINARY_INV);//上下閥值
78cvNamedWindow("imgIteration", CV_WINDOW_AUTOSIZE );
79cvShowImage("imgIteration", imgIteration);
80cvReleaseImage(&imgIteration);
/////////////////////////////////////////////////////////////////////

1/*======================================================================*/
2/* 迭代法*/
3/*======================================================================*/
4// nMaxIter:最大迭代次數;nDiffRec:使用給定閥值確定的亮區與暗區平均灰度差異值
5int DetectThreshold(IplImage*img,int nMaxIter,int& iDiffRec)//閥值分割:迭代法
6{
7//圖像信息
8int height= img->height;
9int width= img->width;
10int step= img->widthStep/sizeof(uchar);
11uchar *data= (uchar*)img->imageData;
12
13iDiffRec =0;
14int F[256]={0 };//直方圖數組
15int iTotalGray=0;//灰度值和
16int iTotalPixel=0;//像素數和
17byte bt;//某點的像素值
18
19uchar iThrehold,iNewThrehold;//閥值、新閥值
20uchar iMaxGrayValue=0,iMinGrayValue=255;//原圖像中的最大灰度值和最小灰度值
21uchar iMeanGrayValue1,iMeanGrayValue2;
22
23//獲取(i,j)的值,存於直方圖數組F
24for(int i=0;i<width;i++)
25{
26for(int j=0;j<height;j++)
27{
28bt = data[i*step+j];
29if(bt<iMinGrayValue)
30iMinGrayValue= bt;
31if(bt>iMaxGrayValue)
32iMaxGrayValue= bt;
33F[bt]++;
34}
35}
36
37iThrehold=0;//
38iNewThrehold= (iMinGrayValue+iMaxGrayValue)/2;//初始閥值
39iDiffRec = iMaxGrayValue- iMinGrayValue;
40
41for(int a=0;(abs(iThrehold-iNewThrehold)>0.5)&&a<nMaxIter;a++)//迭代中止條件
42{
43iThrehold= iNewThrehold;
44//小於當前閥值部分的平均灰度值
45for(int i=iMinGrayValue;i<iThrehold;i++)
46{
47iTotalGray+= F[i]*i;//F[]存儲圖像信息
48iTotalPixel+= F[i];
49}
50iMeanGrayValue1= (uchar)(iTotalGray/iTotalPixel);
51//大於當前閥值部分的平均灰度值
52iTotalPixel=0;
53iTotalGray=0;
54for(int j=iThrehold+1;j<iMaxGrayValue;j++)
55{
56iTotalGray+= F[j]*j;//F[]存儲圖像信息
57iTotalPixel+= F[j];
58}
59iMeanGrayValue2= (uchar)(iTotalGray/iTotalPixel);
60
61iNewThrehold= (iMeanGrayValue2+iMeanGrayValue1)/2;//新閥值
62iDiffRec = abs(iMeanGrayValue2- iMeanGrayValue1);
63}
64
65//cout<<"The Threshold of this Image in imgIteration is:"<<iThrehold<<endl;
66return iThrehold;
67}
68
///////////////////////////////////////////////////////////////////

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章