我正在使用OpenCV版本4.0.0。我试图将一些图像拼接在一起并修剪生成的图像,虽然能够拼接图像,但无法修剪生成的图像。
我的程序不断中止,并出现以下错误:

该错误在下面的代码中的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
}
为什么会出现此错误?

最佳答案

从OP的评论:

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)]

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

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

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



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

您可以通过手动设置矩形的坐标来进行测试。
stitched(cv::Rect(11, 12, cv::Size(4274, 2845)应该不会出错

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

09-07 02:18