我繼續我的工作與openCV,我描述的第一次冒險here。它很好,但我偶然發現了另一個棘手的問題。我想找到的圖像輪廓我已經應用自適應閾值:OpenCV cvFindContours找到只有一個輪廓
所以cvFindContours
似乎工作相當不錯,這裏的結果:
的問題是,當我嘗試遍歷找到的countours時,它說只有一個輪廓(以下代碼中的contours->total
等於1)。代碼如下:
IplImage* img;
if((img = cvLoadImage("photos/img-000012.ppm", 1)) == 0)
{
perror("cvLoadImage");
return 1;
}
cvNamedWindow("Image view", 1);
cvShowImage("Image view", img);
IplImage* gray = cvCreateImage(cvGetSize(img), 8, 1); // allocate a 1 channel byte image
cvCvtColor(img, gray, CV_BGR2GRAY);
cvShowImage("Image view", gray);
cvWaitKey(0);
cvAdaptiveThreshold(gray, gray,
255, // Non-zero value assigned to the pixels for which the condition is satisfied
CV_ADAPTIVE_THRESH_MEAN_C, // adaptiveMethod
CV_THRESH_BINARY_INV, // thresholdType
11, // blockSize
5); // Constant subtracted from the mean or weighted mean
cvShowImage("Image view", gray);
cvWaitKey(0);
IplConvKernel *se = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_RECT, NULL);
cvErode(gray, gray, se, 1);
cvShowImage("Image view", gray);
cvWaitKey(0);
IplImage *canny_out = cvCreateImage(cvGetSize(gray), 8, 1);
cvCanny(gray, canny_out, 50, 100, 3);
cvShowImage("Image view", canny_out);
cvWaitKey(0);
CvMemStorage *storage = cvCreateMemStorage(0);
CvSeq *contours = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvPoint), storage);
cvFindContours(gray, storage, &contours, sizeof(CvContour), CV_RETR_LIST,
CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
printf("contours->total = %d\n", contours->total);
cvDrawContours(img, contours, CV_RGB(0,255,0), CV_RGB(0,0,255),
2, 1, 8, cvPoint(0, 0));
cvShowImage("Image view", img);
cvWaitKey(0);
是否應該這樣,只有一個輪廓?也許我不明白openCV對輪廓的定義?我會很感激你的幫助。
是的,我已經找到了另一種方式,通過在這裏進行迭代: http://stackoverflow.com/questions/6044119/opencv-cvfindcontours-how-do-i-separate-輪廓零件 (karlphillip的帖子),實際上有多個輪廓。我不知道爲什麼'total'說這是一個。無論如何謝謝你的答案,如果我沒有找到那篇文章,我可能仍然會思考爲什麼它沒有工作:) – Wojtek 2012-08-07 19:52:26