是的,你可以用cvFindContours()
做到這一點。它將指針返回到找到的第一個序列。使用該指針可以遍歷所有找到的序列。
// your image converted to grayscale
IplImage* grayImg = LoadImage(...);
// image for drawing contours onto
IplImage* colorImg = cvCreateImage(cvGetSize(grayImg), 8, 3);
// memory where cvFindContours() can find memory in which to record the contours
CvMemStorage* memStorage = cvCreateMemStorage(0);
// find the contours on image *grayImg*
CvSeq* contours = 0;
cvFindContours(grayImg, memStorage, &contours);
// traverse through and draw contours
for(CvSeq* c = contours; c != NULL; c = c->h_next)
{
cvCvtColor(grayImg, colorImg, CV_GRAY2BGR);
cvDrawContours(
colorImg,
c,
CVX_RED,
CVX_BLUE,
0, // Try different values of max_level, and see what happens
2,
8
);
}
除了這個方法,我建議你看一看cvBlobs
或cvBlobsLib
。後者一個作爲官方的blob檢測庫集成在OpenCV 2.0中。
來源
2010-03-04 17:47:39
Adi
+1和@tommed:無論如何,這就是cvBlob的cvlabel.cpp,爲什麼要重新實現它? – AVB 2010-03-04 15:21:19
這完美的作品!非常感謝!! – tommed 2010-03-05 10:20:39
@AB:我不想使用cvBlob的原因是因爲我認爲它仍然是一個單獨的庫(它顯然最近與v 2.0合併),並且我想限制我的程序所需的prereq的數量。 – tommed 2010-03-08 10:27:12