4

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.

4

4 回答 4

2

因为您的图像没有轮廓,而是一系列绿色像素和背景。您需要关闭轮廓以获得轮廓区域。

于 2013-11-11T09:46:12.990 回答
2

也许是因为你的轮廓没有闭合?

更新:我看到您正在为它提供精明操作的结果。我会在具有非常明确的闭合形状的图像上尝试代码来测试代码并排除特定轮廓中的问题。就像在时刻教程中使用的一样。

于 2013-11-11T05:47:23.520 回答
1

也许这个讨论会有所帮助。

于 2013-11-11T06:36:31.417 回答
0

更正:

  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;
   }
于 2014-03-14T09:31:06.197 回答