OpenCV cvFindContours findet nur eine Kontur
Bin ich weiterhin meiner Arbeit mit openCV, meine ersten adventures, die ich beschrieben hier. Es geht ganz gut, aber ich habe stolperte über eine weitere heikle Sache. Ich möchte zu finden, die Konturen auf dem Bild habe ich angewendet adaptive Schwelle:
Also die cvFindContours
scheint zu funktionieren, ganz nett und hier ist das Ergebnis:
Das problem ist, wenn ich versuche, die Iteration über die gefundenen countours, es sagt, dass es nur eine Kontur (contours->total
im folgenden code gleich 1 ist). Hier ist der code:
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);
Sollte es so sein, dass es nur eine Kontur? Vielleicht verstehe ich nicht die openCV-definition einer Kontur? Ich würde Ihre Hilfe schätzen.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Eigentlich
cvFindContours
gibt die Anzahl der gefundenen Konturen (getestet habe ich dein Bild und zurück >1). Sehen docs. Aber ich weiß nicht, warumtotal
entspricht einer.total
sagt, es ist eine. Trotzdem danke für die Antwort, wenn ich nicht gefunden hatte, die post wäre ich wahrscheinlich immer noch darüber gegrübelt, warum es nicht funktioniert 🙂cvCreateSeq()
vorcvFindContours()
anstelle von Konturen->insgesamt, Durchlaufen Sie.
for (; contours != 0; contours = contours->h_next)