在OpenCV中,CountNonZero、Moment M00和ContourArea有何不同?

3
如果我有一个3x3的二进制图像,并且在位置(x,y):(0,0),(0,1),(1,0),(1,1)有轮廓,通过findContours方法获取轮廓。我想获得这个轮廓的面积:
  • 使用CountNonZero: 4
  • 使用contourArea: 1
  • 使用Moment M00: 1
什么是正确答案,它们之间有什么区别?
这个轮廓是正方形,所以面积为2*2 = 4
那么为什么ContourArea等于1?
我正在使用EmguCV,以下是我的代码:
VectorOfVectorOfPoint cont = new VectorOfVectorOfPoint();

    Image<Gray, byte> img = new Image<Gray, byte>(3,3);

    img[0, 0] = new Gray(255);
    img[0, 1] = new Gray(255);
    img[1, 0] = new Gray(255);
    img[1, 1] = new Gray(255);
    CvInvoke.FindContours(img, cont, null, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);

    Moments m = CvInvoke.Moments(cont[0], true);
    Console.WriteLine(CvInvoke.ContourArea(cont[0]));

    CvInvoke.Imshow("ss", img);
    CvInvoke.WaitKey(0);
2个回答

4

我不了解具体的实现细节,但我猜“轮廓(contour)”是一个多边形,它从像素中心到像素中心围绕物体。这个多边形比一组像素小,每条边向内移动半个像素距离。

这与2x2像素块的面积被测量为1个像素是一致的。

如果你想要测量面积,不要使用轮廓功能。使用连接组件分析(对象标记)并计算每个连接组件中像素的数量。


OpenCV不适用于精确量化,其中有很多东西我也不理解。


2
与Cris的答案相关,您这种情况下的轮廓是一个边长为1像素的正方形 ==> 面积 = 1像素平方。
以下是图像和轮廓的样子:
image: 
 [[255 255   0]
 [255 255   0]
 [  0   0   0]]

contour: 
 [[[0 0]]

 [[0 1]]

 [[1 1]]

 [[1 0]]]

area: 
1.0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接