Ask Your Question
0

findContours and Mat(contours[i]) problems

asked 2013-09-24 04:14:24 -0500

leafjungle gravatar image

updated 2013-09-24 04:25:58 -0500

std::vector<std::vector<cv:Point> > contours;
cv::findContours(gray_img->clone(), contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
int contour_num=contours.size();
for (int i = 0; i < contour_num; i++){
   double area = cv::contourArea(contours[i]); //"area" is correct, such as 5000.               
   cv::Mat pointsf;
   cv::Mat(contours[i]).convertTo(pointsf, CV_32F);
   int W2=pointsf.rows;
   int H2=pointsf.cols;  //H2 is always 1, but why?
   cout<<"W2="<<W2<<", H2="<<H2<<endl;
}

The value of H2 is always 1, what's the problem?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-09-24 04:31:08 -0500

Change your code to this, and it will work :)

std::vector<Mat> contours;
Mat temp = gray_img->clone();
cv::findContours(temp, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); i++){
   double area = cv::contourArea(contours[i]); //"area" is correct, such as 5000.               
   int W2 = contours[i].rows;
   int H2 = contours[i].cols;
   cout << "W2 = " << W2 << ", H2 = " << H2 << endl;
}

Let the internal functionality of the Mat element handle the points vectorization.

edit flag offensive delete link more

Comments

I changed the code as yours, but the problems remains.

area=24752, W2=68, H2=1.

leafjungle gravatar imageleafjungle ( 2013-09-24 07:08:01 -0500 )edit

Can you check if gray_img.cols is larger than 1?

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-24 07:10:46 -0500 )edit

Question Tools

Stats

Asked: 2013-09-24 04:14:24 -0500

Seen: 232 times

Last updated: Sep 24 '13