cvStartFindContours 出现奇怪的问题?

发布于 2024-12-12 19:16:15 字数 1661 浏览 2 评论 0原文

每当我将图像传递给 cvStartFindContours 时,我的图像就会变成全黑,而不是找到轮廓。我完全不知道为什么。我知道这不是由代码的另一部分引起的,即使我注释掉所有内容,但保留对 cvStartFindContours 的函数调用,我的图像仍然变黑。然而,当我注释掉对 cvStartFindContours 的调用时,我的图像仍然正常。为什么会发生这种情况?我传入的图像 (skinMask) 是一个包含数据的 8 位、1 通道图像 (IPL_DEPTH_8U)。 (它不是黑色的,上面有白色的碎片)。

为什么我的图像变黑了?谢谢

void connectedComponents ()
{
     cvMorphologyEx (skinMask, skinMask, 0, 0, CV_MOP_OPEN, 1);
     cvMorphologyEx (skinMask, skinMask, 0, 0, CV_MOP_CLOSE, 1);

     CvMemStorage *connectedCompStorage = cvCreateMemStorage (0);
     CvSeq *temp = NULL; //used to loop through contour perimeter checking
     CvSeq *connectedComp = NULL; //stores all derived connected component contours

     CvContourScanner connectedCompScanner = cvStartFindContours (skinMask, connectedCompStorage); //FUNCTION CALL CAUSING PROBLEMS

     while ((temp = cvFindNextContour (connectedCompScanner)) != NULL)
     {
         double perimeter = cvContourPerimeter (temp);

         if (perimeter < CC_PERIMETER_THRESH)
         {
             cvSubstituteContour (connectedCompScanner, NULL);
         }

         else
         {
             temp = cvApproxPoly (temp, sizeof (CvContour), connectedCompStorage, CV_POLY_APPROX_DP, 2, 0);
             cvSubstituteContour (connectedCompScanner, temp);
         }
     }

     connectedComp = cvEndFindContours (&connectedCompScanner);

     cvZero (skinMask);

     for (temp = connectedComp; temp != NULL; temp = temp -> h_next);
     {
         cvDrawContours (skinMask, temp, cvScalar (255, 255, 255), cvScalar (0, 0, 0), -1,   CV_FILLED, 8);
     }

     cvReleaseMemStorage (&connectedCompStorage);
}

Whenever I pass in an image to cvStartFindContours, instead of finding the contours, my image turns completely black. I have absolutely no idea why. I know this isn't caused by another part of the code even if I comment everything out, but leave in the function call to cvStartFindContours, my image still turns black. However the second I comment out the call to cvStartFindContours, my image remains normal. Why is this happening? The image I pass in (skinMask) is a 8 bit, 1 channel image (IPL_DEPTH_8U) with CONTAINS data. (it isn't black it has pieces of white on it).

Why is my image turning black? Thanks

void connectedComponents ()
{
     cvMorphologyEx (skinMask, skinMask, 0, 0, CV_MOP_OPEN, 1);
     cvMorphologyEx (skinMask, skinMask, 0, 0, CV_MOP_CLOSE, 1);

     CvMemStorage *connectedCompStorage = cvCreateMemStorage (0);
     CvSeq *temp = NULL; //used to loop through contour perimeter checking
     CvSeq *connectedComp = NULL; //stores all derived connected component contours

     CvContourScanner connectedCompScanner = cvStartFindContours (skinMask, connectedCompStorage); //FUNCTION CALL CAUSING PROBLEMS

     while ((temp = cvFindNextContour (connectedCompScanner)) != NULL)
     {
         double perimeter = cvContourPerimeter (temp);

         if (perimeter < CC_PERIMETER_THRESH)
         {
             cvSubstituteContour (connectedCompScanner, NULL);
         }

         else
         {
             temp = cvApproxPoly (temp, sizeof (CvContour), connectedCompStorage, CV_POLY_APPROX_DP, 2, 0);
             cvSubstituteContour (connectedCompScanner, temp);
         }
     }

     connectedComp = cvEndFindContours (&connectedCompScanner);

     cvZero (skinMask);

     for (temp = connectedComp; temp != NULL; temp = temp -> h_next);
     {
         cvDrawContours (skinMask, temp, cvScalar (255, 255, 255), cvScalar (0, 0, 0), -1,   CV_FILLED, 8);
     }

     cvReleaseMemStorage (&connectedCompStorage);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福%小乖 2024-12-19 19:16:16

我也被这个咬过。来自 findContours 的 OpenCV 文档:

image – 源,8 位单通道图像。非零像素是
被视为 1。零像素仍为 0,因此图像被视为
二进制 .您可以使用 compare()inRange()threshold()
adaptiveThreshold()Canny() 等用于创建二进制图像
灰度或彩色。 该函数修改图像,同时
提取轮廓。

因此,如果您不希望修改 skinMask 图像,则需要传入它的副本...

I have been bitten by this as well. From the OpenCV documentation for findContours:

image – Source, an 8-bit single-channel image. Non-zero pixels are
treated as 1’s. Zero pixels remain 0’s, so the image is treated as
binary . You can use compare() , inRange() , threshold() ,
adaptiveThreshold() , Canny() , and others to create a binary image
out of a grayscale or color one. The function modifies the image while
extracting the contours.

So, if you don't want your skinMask image to be modified, you need to pass in a copy of it...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文