opencv - 在 opencv 中使用 cvCountNonZero 时出错

标签 opencv contour

我正在尝试使用 openCV(使用 C)计算从 Canny 边缘图像检索的轮廓中非零像素的数量。我正在使用 cvFindNextContour 来查找使用轮廓扫描仪检索到的后续轮廓。

但是当我在轮廓上使用 cvCountNonZero 时,会出现错误:

Bad flag (parameter or structure field) (Unrecognized or unsupported array type)
in function cvGetMat, C:\User\..\cvarray.cpp(2881)

我的代码是:

cvCvtColor(image, gray, CV_BGR2GRAY);
cvCanny(gray, edge, (float)edge_thresh, (float)edge_thresh*4, 3);

sc = cvStartFindContours( edge, mem,
                          sizeof(CvContour),
                          CV_RETR_LIST,
                          CV_CHAIN_APPROX_SIMPLE,
                          cvPoint(0,0) );

while((contour = cvFindNextContour(sc))!=NULL)
{
               CvScalar color = CV_RGB( rand()&255, rand()&255, rand()&255 );
               printf("%d\n",cvCountNonZero(contour));
               cvDrawContours(final, contour, color, CV_RGB(0,0,0), -1, 1, 8, cvPoint(0,0));

}

我们非常感谢任何形式的帮助。提前致谢。

最佳答案

cvCountNonZero(CvArr*) 用于查找数组或 IplImage 中非零的数量,但不适用于 CvSeq* 轮廓类型。这就是错误出现的原因。这是问题的解决方案。

        CvRect rect = cvBoundingRect( contour, 0);
        cvSetImageROI(img1,rect);
        cout<<cvCountNonZero(img1)<<endl;
        cvResetImageROI(img1);
//where img1 is the binary image in which you find the contours.

代码可以这样解释:

1.首先在每个轮廓周围制作一个矩形区域。

2.将图像 ROI 设置为该特定区域。

3.现在使用cvCountNonZero();函数查找区域中非零的数量。

4.重置图像ROI。

祝您编码愉快。

关于opencv - 在 opencv 中使用 cvCountNonZero 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11305512/

相关文章:

debugging - 退出显示图像OpenCV时堆损坏

python - 安装 OpenCV

python - 检查数组行是否为None并为其赋值

c++ - 绘制从二值图像中检索到的轮廓

c++ - Qt 窗口关闭时如何释放内存?

c++ - 从不同的平面中选择最大值

python - 使用轮廓裁剪手机屏幕

python - 如何将图像中的字符和单词分割成轮廓

python - 如何使用 matplotlib 绘制回归的决策边界?

python - Opencv 找不到所有轮廓