Reputation: 2497
I have the following contour:
https://drive.google.com/file/d/0B45BJEUVEkjseFd3X3RITzM5S3c/edit?usp=sharing
containing the following points (printed in order):
https://drive.google.com/file/d/0B45BJEUVEkjsN3NIRU5lOFBDb00/edit?usp=sharing
However, when I calculate the area of this contour (using the function contourArea), I get 157, which is too low for the size of that contour. I expect it to be in the thousands. Why is the contour area being calculated incorrectly, and how may I fix it?
The following is the code I'm using to calculate the areas of all contours of the image. The contour of interest is the last one. The original image I'm using is here:
https://drive.google.com/file/d/0B45BJEUVEkjsbGhXM3E3UW1lZWs/edit?usp=sharing
int main(int argc, char* argv[])
{
Mat imgOriginal = imread(argv[1], 0);
if(imgOriginal.empty())
return -1;
Mat img;
resize(imgOriginal, img, Size(640, 480));
medianBlur(img, img, 11);
Canny(img, img, 25, 100);
vector< vector<Point> > contours;
findContours(img, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
for (int i = 0; i < contours.size(); i++)
cout << "Area " << i << ": " << contourArea(contours[i]) << endl;
return 0;
}
Also, I noticed that several points in the contour are duplicates (I'm not sure why), which may be causing the area to be calculated incorrectly.
Upvotes: 5
Views: 9964
Reputation: 37
Correction:
int main(int argc, char* argv[])
{
Mat imgOriginal = imread(argv[1], 0);
if(imgOriginal.empty())
return -1;
Mat img;
resize(imgOriginal, img, Size(640, 480));
medianBlur(img, img, 11);
Canny(img, img, 25, 100);
vector< vector<Point> > contours;
findContours(img, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
cout << "Area " << ": " << contourArea(contours[1]) << endl;
return 0;
}
Upvotes: 0
Reputation: 16553
Maybe because your contour isn't closed?
Update: I see you're feeding it the result of a canny operation. I would try the code on an image with a very well defined closed shape to test the code and rule out a problem in the specific contour. Something like the one used in the moments tutorial.
Upvotes: 3
Reputation: 510
Because your image has no contours but series of green pixels and a background.you need to close the contour to get contour area.
Upvotes: 2