【问题标题】:How to fix error with cv::boundingRect in OpenCV如何修复 OpenCV 中的 cv::boundingRect 错误
【发布时间】:2019-07-03 05:51:17
【问题描述】:

我使用的是 OpenCV 4.0.0 版。我正在尝试将一些图像拼接在一起并修剪生成的图像,虽然我能够缝合图像,但我无法修剪生成的图像。

我的程序不断中止并出现以下错误:

libc++abi.dylib:以 cv::Exception 类型的未捕获异常终止:OpenCV(4.0.0) /Users/RAR/opencv/modules/core/src/umatrix.cpp:545:错误:(- 215:断言失败)0

中止陷阱:6

错误发生在下面代码中的stitched = stitched(cv::boundingRect(c));行。

while (cv::countNonZero(sub) > 0) {
            cv::erode(minRect, minRect, cv::Mat());  // Erode the minimum rectangular mask
            cv::subtract(minRect, thresh, sub);  // Subtract the thresholded image from the minmum rectangular mask (count if there are any non-zero pixels left)
            std::vector<std::vector<cv::Point>> cnts4;
            cv::findContours(minRect.clone(), cnts4, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
            c = cnts4[0];
            for (auto iter = cnts4.begin(); iter != cnts4.end(); ++iter) {
                if (cv::contourArea(*iter) > cv::contourArea(c)) { // Finds the largest contour (the contour/outline of the stitched image)
                    c = *iter;
                }
            }

            stitched = stitched(cv::boundingRect(c));  // Extract the bounding box and use the bounding box coordinates to extract the final stitched images
}

为什么会出现这个错误?

【问题讨论】:

  • 打印边界矩形的坐标。这些坐标似乎超出了stitched 图像的尺寸
  • @shawn-mathew 我打印出边界矩形的坐标以及缝合的行和列,这就是我得到的:stitched: cols: 4295 rows: 2867 bounding rect[4274 x 2845 from (11, 12)] stitched: cols: 4274 rows: 2845 bounding rect[4272 x 2843 from (12, 13)] 如何检查边界矩形的坐标超出拼接图像的尺寸?

标签: c++ opencv


【解决方案1】:

来自 OP 的 cmets:

stitched: cols: 4295 rows: 2867 bounding rect[4274 x 2845 from (11, 12)] 
stitched: cols: 4274 rows: 2845 bounding rect[4272 x 2843 from (12, 13)]

在第一种情况下,矩形试图从stitched 图像中的(11, 12) 中提取(4274, 2845) 的大小。这意味着它从(11, 12)(4285, 2857) 获取像素,这在stitched 图像的范围内,因为stitched 图像的大小为(4295, 2867)没问题

在第二种情况下,矩形试图从stitched 图像中的(12, 13) 中提取(4272, 2843) 的大小。这意味着它将像素从(12, 13) 带到(4284, 2856),这超出了拼接 图像的范围,因为stitched 图像的大小为(4274, 2845)问题

您尝试提取的子图像比较大的图像大得多。

(-215: 断言失败) 0

错误消息也表明了这一点。错误消息中的roi 是指您尝试使用cv::boundingRect(c) 提取的子图像,mstitched 图像。此矩形的坐标超出了stitched 图像的大小。

您可以通过手动设置矩形的坐标来进行测试。

stitched(cv::Rect(11, 12, cv::Size(4274, 2845) 不应该出现错误

您将收到stitched(cv::Rect(12, 13, cv::Size(4272, 2843) 的错误

【讨论】:

    【解决方案2】:

    最后一次迭代是问题所在,因为它找不到任何轮廓。

    也许你可以尝试这样的事情:

    int nonZeroCount = 1;
    while (nonZeroCount) 
    {
        cv::erode(minRect, minRect, cv::Mat());
        cv::subtract(minRect, thresh, sub);
        nonZeroCount = cv::countNonZero(sub);
        if (nonZeroCount)
        {
            std::vector< std::vector<cv::Point> > cnts4;
            cv::findContours(minRect.clone(), cnts4, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
            c = cnts4[0];
            for (auto iter = cnts4.begin(); iter != cnts4.end(); ++iter) 
            {
                if (cv::contourArea(*iter) > cv::contourArea(c))
                {
                    c = *iter;
                }
            }
            stitched = stitched(cv::boundingRect(c));
        }
    }
    

    【讨论】:

    • 你能解释一下为什么在最后一次迭代中找不到轮廓吗?
    • 我尝试了您的代码,但在stitched = stitched(cv::boundingRect(c)); 行仍然出现异常:libc++abi.dylib: terminating with uncaught exception of type cv::Exception: OpenCV(4.0.0) /Users/ramyaasha/opencv/modules/core/src/umatrix.cpp:545: error: (-215:Assertion failed) 0 &lt;= roi.x &amp;&amp; 0 &lt;= roi.width &amp;&amp; roi.x + roi.width &lt;= m.cols &amp;&amp; 0 &lt;= roi.y &amp;&amp; 0 &lt;= roi.height &amp;&amp; roi.y + roi.height &lt;= m.rows in function 'UMat' Abort trap: 6
    猜你喜欢
    • 2021-09-26
    • 2012-08-04
    • 1970-01-01
    • 2023-03-08
    • 2018-03-09
    • 2019-11-13
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多