【问题标题】:What is difference between (CountNonZero) and (Moment M00) and (ContourArea) in OpenCV?OpenCV中的(CountNonZero)和(Moment M00)和(ContourArea)有什么区别?
【发布时间】:2020-08-01 02:46:06
【问题描述】:

如果我有一个 3x3 的二值图像并且在位置 (x,y) 中有一个轮廓:(0,0), (0,1), (1,0), (1,1)

我通过findContours 方法得到轮廓。

我想得到这个轮廓的面积:

  • 使用 CountNonZero:4
  • 轮廓区域: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);

【问题讨论】:

    标签: opencv image-processing emgucv opencv-contour opencv3.1


    【解决方案1】:

    我不知道实现细节,但我怀疑“轮廓”是围绕对象从像素中心到像素中心的多边形。这个多边形小于像素集,每条边向内移动半个像素距离。

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

    如果您想测量面积,请不要使用等高线功能。使用连通分量分析(对象标记)并计算每个连通分量中的像素数。


    OpenCV 不是用来精确量化的,其中有很多东西对我来说没有意义。

    【讨论】:

      【解决方案2】:

      与Cris's answer 相关,在您的情况下,轮廓是一个边长为 1 像素 ==> 面积 = 1 像素平方的正方形。

      这就是图像和轮廓的样子:

      image: 
       [[255 255   0]
       [255 255   0]
       [  0   0   0]]
      
      contour: 
       [[[0 0]]
      
       [[0 1]]
      
       [[1 1]]
      
       [[1 0]]]
      
      area: 
      1.0
      

      【讨论】:

        猜你喜欢
        • 2017-08-31
        • 1970-01-01
        • 1970-01-01
        • 2013-09-02
        • 2014-08-09
        • 1970-01-01
        • 2015-06-17
        • 2019-11-24
        • 2018-05-31
        相关资源
        最近更新 更多