在OpenCV中用canny運算元進行邊緣檢測速度很快,不過有點不爽的就是高低閾值需要輸入。在matlab中,如果不指定閾值的話,由函數自適應確定,因此仿照matlab中的做法,對canny函數進行了修改,以便當使用者沒有指定高低閾值時,由函數自適應確定閾值。
我在OpenCv原碼庫中增加了一個函數,用於確定高低閾值。
view plaincopy to clipboardprint?
- // 仿照matlab,自適應求高低兩個門限
- CV_IMPL void AdaptiveFindThreshold(CvMat *dx, CvMat *dy, double *low, double *high)
- {
- CvSize size;
- IplImage *imge=0;
- int i,j;
- CvHistogram *hist;
- int hist_size = 255;
- float range_0[]={0,256};
- float* ranges[] = { range_0 };
- double PercentOfPixelsNotEdges = 0.7;
- size = cvGetSize(dx);
- imge = cvCreateImage(size, IPL_DEPTH_32F, 1);
- // 計算邊緣的強度, 並存於映像中
- float maxv = 0;
- for(i = 0; i < size.height; i++ )
- {
- const short* _dx = (short*)(dx->data.ptr + dx->step*i);
- const short* _dy = (short*)(dy->data.ptr + dy->step*i);
- float* _image = (float *)(imge->imageData + imge->widthStep*i);
- for(j = 0; j < size.width; j++)
- {
- _image[j] = (float)(abs(_dx[j]) + abs(_dy[j]));
- maxv = maxv < _image[j] ? _image[j]: maxv;
- }
- }
-
- // 計算長條圖
- range_0[1] = maxv;
- hist_size = (int)(hist_size > maxv ? maxv:hist_size);
- hist = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
- cvCalcHist( &imge, hist, 0, NULL );
- int total = (int)(size.height * size.width * PercentOfPixelsNotEdges);
- float sum=0;
- int icount = hist->mat.dim[0].size;
-
- float *h = (float*)cvPtr1D( hist->bins, 0 );
- for(i = 0; i < icount; i++)
- {
- sum += h[i];
- if( sum > total )
- break;
- }
- // 計算高低門限
- *high = (i+1) * maxv / hist_size ;
- *low = *high * 0.4;
- cvReleaseImage( &imge );
- cvReleaseHist(&hist);
- }
-
- 在把cvCanny函數進行以下修改。
- 在函數體中,當程式用兩個sobel運算元計算完水平和垂直兩個方向的梯度強度過後加入以下代碼
- // 自適應確定閾值
- if(low_thresh == -1 && high_thresh == -1)
- {
- AdaptiveFindThreshold(dx, dy, &low_thresh, &high_thresh);
- }
// 仿照matlab,自適應求高低兩個門限<br />CV_IMPL void AdaptiveFindThreshold(CvMat *dx, CvMat *dy, double *low, double *high)<br />{<br /> CvSize size;<br /> IplImage *imge=0;<br /> int i,j;<br /> CvHistogram *hist;<br /> int hist_size = 255;<br /> float range_0[]={0,256};<br /> float* ranges[] = { range_0 };<br /> double PercentOfPixelsNotEdges = 0.7;<br /> size = cvGetSize(dx);<br /> imge = cvCreateImage(size, IPL_DEPTH_32F, 1);<br /> // 計算邊緣的強度, 並存於映像中<br /> float maxv = 0;<br /> for(i = 0; i < size.height; i++ )<br /> {<br /> const short* _dx = (short*)(dx->data.ptr + dx->step*i);<br /> const short* _dy = (short*)(dy->data.ptr + dy->step*i);<br /> float* _image = (float *)(imge->imageData + imge->widthStep*i);<br /> for(j = 0; j < size.width; j++)<br /> {<br /> _image[j] = (float)(abs(_dx[j]) + abs(_dy[j]));<br /> maxv = maxv < _image[j] ? _image[j]: maxv;<br /> }<br /> }</p><p> // 計算長條圖<br /> range_0[1] = maxv;<br /> hist_size = (int)(hist_size > maxv ? maxv:hist_size);<br /> hist = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);<br /> cvCalcHist( &imge, hist, 0, NULL );<br /> int total = (int)(size.height * size.width * PercentOfPixelsNotEdges);<br /> float sum=0;<br /> int icount = hist->mat.dim[0].size;</p><p> float *h = (float*)cvPtr1D( hist->bins, 0 );<br /> for(i = 0; i < icount; i++)<br /> {<br /> sum += h[i];<br /> if( sum > total )<br /> break;<br /> }<br />// 計算高低門限<br /> *high = (i+1) * maxv / hist_size ;<br /> *low = *high * 0.4;<br /> cvReleaseImage( &imge );<br /> cvReleaseHist(&hist);<br />}</p><p>在把cvCanny函數進行以下修改。<br />在函數體中,當程式用兩個sobel運算元計算完水平和垂直兩個方向的梯度強度過後加入以下代碼<br />// 自適應確定閾值<br /> if(low_thresh == -1 && high_thresh == -1)<br /> {<br /> AdaptiveFindThreshold(dx, dy, &low_thresh, &high_thresh);<br /> }
這樣,在調用cvCanny函數時,指定高低門限為-1,則cvCanny函數就自適應確定門限。 最後,別忘了重新編譯cv庫,對lib和dll庫進行更新。that's all!http://blog.chinaunix.net/u/30231/showart_233944.html