0
我在學習opencv,通過使用同名的書。我想計算一個輪廓的面積,但它總是返回0.輪廓被繪成封閉的多邊形,所以這似乎是正確的。opencv countour area returns zero
這裏有一些樣品,但他們使用vector<vector<Point>> contours
。我的下面的代碼是基於書籍樣本。我正在使用的參考圖像是一個grayscale之一。
所以我的問題是:我錯過了什麼讓該地區!= 0?
#include <opencv\cv.h>
#include <opencv\highgui.h>
#define CVX_RED CV_RGB(0xff,0x00,0x00)
#define CVX_BLUE CV_RGB(0x00,0x00,0xff)
int main(int argc, char* argv[]) {
cvNamedWindow(argv[0], 1);
IplImage* img_8uc1 = cvLoadImage(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
IplImage* img_edge = cvCreateImage(cvGetSize(img_8uc1), 8, 1);
IplImage* img_8uc3 = cvCreateImage(cvGetSize(img_8uc1), 8, 3);
cvThreshold(img_8uc1, img_edge, 128, 255, CV_THRESH_BINARY);
CvMemStorage* storage = cvCreateMemStorage();
CvSeq* contours = NULL;
int num_contours = cvFindContours(img_edge, storage, &contours, sizeof(CvContour),
CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0, 0));
printf("Total Contours Detected: %d\n", num_contours);
int n=0;
for(CvSeq* current_contour = contours; current_contour != NULL; current_contour=current_contour->h_next) {
printf("Contour #%d\n", n);
int point_cnt = current_contour->total;
printf(" %d elements\n", point_cnt);
if(point_cnt < 20){
continue;
}
double area = fabs(cvContourArea(current_contour, CV_WHOLE_SEQ, 0));
printf(" area: %d\n", area);
cvCvtColor(img_8uc1, img_8uc3, CV_GRAY2BGR);
cvDrawContours(img_8uc3, current_contour, CVX_RED, CVX_BLUE, 0, 2, 8);
cvShowImage(argv[0], img_8uc3);
cvWaitKey(0);
n++;
}
printf("Finished contours.\n");
cvCvtColor(img_8uc1, img_8uc3, CV_GRAY2BGR);
cvShowImage(argv[0], img_8uc3);
cvWaitKey(0);
cvDestroyWindow(argv[0]);
cvReleaseImage(&img_8uc1);
cvReleaseImage(&img_8uc3);
cvReleaseImage(&img_edge);
return 0;
}
+1爲C++接口 – Mailerdaimon
非常感謝提示! – Spindizzy