OpenCV findContours 问题

发布于 2024-12-18 22:30:39 字数 1961 浏览 2 评论 0原文

我有以下代码,它执行背景减法,然后使用 findContours 在前景对象周围绘制边界。

// frame - Input frame from a camera.
// output - Output frame to be displayed.
    void process(cv:: Mat &frame, cv:: Mat &output) {

    cv::cvtColor(frame, gray, CV_BGR2GRAY); 

    // initialize background to 1st frame
    if (background.empty())
        gray.convertTo(background, CV_32F);

    // convert background to 8U
    background.convertTo(backImage,CV_8U);

    // compute difference between current image and background
    cv::absdiff(backImage,gray,foreground);

    // apply threshold to foreground image
    cv::threshold(foreground,output,threshold,255,cv::THRESH_BINARY_INV);

    // accumulate background
    cv::accumulateWeighted(gray, background, learningRate, output);

    // Find regions of interest
    std::vector<std::vector<cv::Point> > v; // Detected foreground points

    cv::findContours(output,v,CV_RETR_LIST,CV_CHAIN_APPROX_NONE);

    // Sort to find the entry with the most points at the beginning.
            // This is done to overcome noisy input.
    std::sort(v.begin(), v.end(), DescendingCompare);

    cv::Mat drawing = frame;

    std::vector<std::vector<cv::Point>> contours_poly(1);

    // Determine an approximate polygon for v[0] which is the largest contour
    cv::approxPolyDP( cv::Mat(v[0]), contours_poly[0], 3, false );

    // Draw polygonal contour
     cv::Scalar color = cv::Scalar( 0,0,255 );
     cv::drawContours( drawing, contours_poly, 0, color, 2, 8, std::vector<cv::Vec4i>(), 0, cv::Point() );

     // Show in a window
    output = drawing;
    v.clear();

}

该图像只是一个空白的白色背景,但 findContours() 返回具有图像 4 个边缘的轮廓。这最终成为找到的最大轮廓,否定了我在代码中的逻辑。有没有办法解决这个问题?我希望它在屏幕空白时返回一个空向量。

http://imgur.com/a/hJCQl

http://imgur.com/a/hJCQl

另外,是否可以以任何方式改进此代码以提高效率?

I have the following code which performs background subtraction and then uses findContours to draw a boundary around the foreground object.

// frame - Input frame from a camera.
// output - Output frame to be displayed.
    void process(cv:: Mat &frame, cv:: Mat &output) {

    cv::cvtColor(frame, gray, CV_BGR2GRAY); 

    // initialize background to 1st frame
    if (background.empty())
        gray.convertTo(background, CV_32F);

    // convert background to 8U
    background.convertTo(backImage,CV_8U);

    // compute difference between current image and background
    cv::absdiff(backImage,gray,foreground);

    // apply threshold to foreground image
    cv::threshold(foreground,output,threshold,255,cv::THRESH_BINARY_INV);

    // accumulate background
    cv::accumulateWeighted(gray, background, learningRate, output);

    // Find regions of interest
    std::vector<std::vector<cv::Point> > v; // Detected foreground points

    cv::findContours(output,v,CV_RETR_LIST,CV_CHAIN_APPROX_NONE);

    // Sort to find the entry with the most points at the beginning.
            // This is done to overcome noisy input.
    std::sort(v.begin(), v.end(), DescendingCompare);

    cv::Mat drawing = frame;

    std::vector<std::vector<cv::Point>> contours_poly(1);

    // Determine an approximate polygon for v[0] which is the largest contour
    cv::approxPolyDP( cv::Mat(v[0]), contours_poly[0], 3, false );

    // Draw polygonal contour
     cv::Scalar color = cv::Scalar( 0,0,255 );
     cv::drawContours( drawing, contours_poly, 0, color, 2, 8, std::vector<cv::Vec4i>(), 0, cv::Point() );

     // Show in a window
    output = drawing;
    v.clear();

}

The image is just a blank white background but findContours() is returning a contour with the 4 edges of the image. This ends up being the largest contour found, negating my logic in the code.Is there anyway to fix this ? I want it to return a null vector when the screen is blank.

http://imgur.com/a/hJCQl

http://imgur.com/a/hJCQl

Also, can this code be improved in anyway to improve efficiency ?

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

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

发布评论

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

评论(1

与君绝 2024-12-25 22:30:39

您的背景应为黑色 (0),并且您想要轮廓的任何对象应为白色(或 >= 1)。您将其颠倒过来,这就是 FindContours 将背景检测为轮廓而不是对象的原因。

Your background should be black (0) and any object you want to contour should be white( or >= 1). You have it reversed and that's why FindContours detects the background as a contour and not the object.

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