2011-09-02 67 views

回答

2

訣竅來檢索圖像作爲一個輪廓似乎與Canny被處理圖像執行cvFindContours之前。

IplImage* src = cvLoadImage(argv[1], CV_LOAD_IMAGE_GRAYSCALE); 

IplImage* cc_img = cvCreateImage(cvGetSize(src), src->depth, 3); 
cvSetZero(cc_img); 
CvScalar(ext_color); 

CvMemStorage *mem; 
mem = cvCreateMemStorage(0); 
CvSeq *contours = 0; 

// edges returned by Canny might have small gaps between them, which causes some problems during contour detection 
// Simplest way to solve this s to "dilate" the image. 
cvCanny(src, src, 10, 50, 3); 
cvShowImage("Tutorial", src); 
cvWaitKey(0); 

int n = cvFindContours(src, mem, &contours, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0)); 

CvSeq* ptr = 0; 
for (ptr = contours; ptr != NULL; ptr = ptr->h_next) 
{ 
    ext_color = CV_RGB(rand()&255, rand()&255, rand()&255); //randomly coloring different contours 
    cvDrawContours(cc_img, ptr, ext_color, CV_RGB(0,0,0), -1, CV_FILLED, 8, cvPoint(0,0)); 
} 

cvNamedWindow("Tutorial"); 
cvShowImage("Tutorial", cc_img); 
//cvSaveImage("out.png", cc_img); 

cvWaitKey(0); 

輸出:

enter image description here

+0

嗨,感謝您的回覆。我使用opencv android。我最初執行雙邊濾鏡(Imgproc.bilateralFilter(inputMatrix,bilateralFilteredMatrix,3,50,50))和MorphologyEx操作(MORPH_OPEN和MORPH_CLOSE),然後在圖像上執行Canny。我看到Opencv在那之後返回了2個輪廓。但如果我不按照你的方法做最初的降噪並且只做Canny,它會給我一個輪廓?!但對於有噪聲的其他實時圖像,我需要應用這些濾鏡。爲什麼FindContours在應用過濾器後返回2個輪廓? – user662239

+0

其中一個過濾器可能會將圖像分開,從而在符號的繪製中產生間隙,對於'cvContours'來說足夠小以至於認爲它處理2個獨立的組件。要麼是這樣,要麼在執行邏輯時可能會遇到其他一些問題,但由於這個問題沒有代碼,所以我們無法幫助您。 – karlphillip