Validation failed Error (-215) with Area () C ++ OpenCV 2.3.1 loop

I want to filter the contours of an image (webcam) by its area. I use findContours () and then convert them to closed polygons using approxPolyDP (). After that, I want to filter them by my area (a function that generates another structure with β€œuseful” contours) using this function:

std::vector<vector<Point> > _filterByArea(double minArea,double maxArea,const std::vector<vector<Point> > contours){ size_t ncontours=contours.size(); size_t useful_contours=0; size_t i,j; double area; std::vector<vector<Point> > filtered_contours; for(i=0;i<ncontours;i++){ area=contourArea(contours[i],false); if(area>minArea && area<maxArea){ useful_contours++; } } filtered_contours.resize(useful_contours); useful_contours=0; for(i=0;i<ncontours;i++){ double area=contourArea(contours[i],false); size_t contour_size=contours[i].size(); if(area>minArea && area<maxArea){ for(j=0;j<contour_size;j++){ //filtered_contours[useful_contours].resize(contour_size); filtered_contours.at(useful_contours).push_back(contours.at(i).at(j)); //filtered_contours.insert(filtered_contours.begin(),contours.at(i).begin(),contours.at(i)); } useful_contours++; } } return filtered_contours; 

}

But at startup, it crashes with the following error:

OpenCV error: statement failed (contour.checkVector (2)> = 0 && (contour.depth () == CV_32F || contour.depth () == CV_32S)) in contourArea, file / tmp / buildd / opencv-2.3 .1 / modules / imgproc / src / contours.cpp, line 1673 terminate call after calling the instance 'cv :: Exception' what (): / tmp / buildd / opencv-2.3.1 / modules / imgproc / src / contours.cpp : 1673: error: (-215) contour.checkVector (2)> = 0 && & (contour.depth () == CV_32F || contour.depth () == CV_32S) in the contourArea function

Having tried different things and codes, I found that the error is in the area of ​​the line = contourArea (contours [i], false); this is pretty obvious, but not so obvious to me, because I tried the same (same for ()) code before (but not in the function, inside main ()), and it worked fine. What am I doing wrong?

Thanks in advance!

+6
source share

All Articles