gpt4 book ai didi

opencv - 在 opencv 中使用 cvCountNonZero 时出错

转载 作者:太空宇宙 更新时间:2023-11-03 21:38:16 26 4
gpt4 key购买 nike

我正在尝试计算使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com