La bla bla
La bla bla

Reputation: 8708

openCV cvContourArea

I'm trying to use cvFindContours, which definitely seems like the way to go. I'm having a problem with getting the largest one. There is a function call cvContourArea, which suppose to get the area of a contour in a sequence. I'm having trouble with it.

int conNum = cvFindContours(outerbox, storage, &contours, sizeof(CvContour),CV_RETR_LIST,CV_CHAIN_APPROX_NONE,cvPoint(0, 0));
CvSeq* current_contour = contours;
double largestArea = 0;
CvSeq* largest_contour = NULL;


while (current_contour != NULL){
    double area = fabs(cvContourArea(&storage,CV_WHOLE_SEQ, false));       
    if(area > largestArea){
        largestArea = area;
        largest_contour = current_contour;
    }
    current_contour = current_contour->h_next;
}

I tried replacing storage (in the cvContourArea) with contours, but same error keeps coming up no matter what:

OpenCV Error: Bad argument (Input array is not a valid matrix) in cvPointSeqFromMat, file /Volumes/ramdisk/opencv/OpenCV-2.2.0/modules/imgproc/src/utils.cpp, line 53

I googled and could hardly find example of cvContourArea that takes 3 arguments.. as if it's changed recently.. I want to loop thru the found contours and find the biggest one and after that draw it using the cvDrawContours method.. Thanks!

Upvotes: 2

Views: 5007

Answers (1)

brucelin
brucelin

Reputation: 26

Try to change &storage to current_contour in the following statement.

Change

double area = fabs(cvContourArea(&storage,CV_WHOLE_SEQ, false)); 

to

double area = fabs(cvContourArea(current_contour,CV_WHOLE_SEQ, 0));

Upvotes: 1

Related Questions