Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何修复OpenCV中cv::boundingRect的错误_C++_Opencv - Fatal编程技术网

C++ 如何修复OpenCV中cv::boundingRect的错误

C++ 如何修复OpenCV中cv::boundingRect的错误,c++,opencv,C++,Opencv,我使用的是OpenCV版本4.0.0。我正在尝试将一些图像缝合在一起并修剪生成的图像,虽然我能够缝合图像,但无法修剪生成的图像 我的程序不断中止,出现以下错误: libc++abi.dylib:以cv::exception:OpenCV(4.0.0)/Users/RAR/OpenCV/modules/core/src/umatrix类型的未捕获异常终止。cpp:545:错误:(-215:断言失败)0上一次迭代是问题,因为它找不到任何轮廓 也许你可以试试这样的方法: int nonZeroCoun

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

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


libc++abi.dylib:以cv::exception:OpenCV(4.0.0)/Users/RAR/OpenCV/modules/core/src/umatrix类型的未捕获异常终止。cpp:545:错误:(-215:断言失败)0上一次迭代是问题,因为它找不到任何轮廓

也许你可以试试这样的方法:

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));
    }
}
int非零计数=1;
while(非零计数)
{
侵蚀(minRect,minRect,cv::Mat());
cv::减法(minRect、thresh、sub);
nonZeroCount=cv::countNonZero(sub);
if(非零计数)
{
std::vectorcnts4;
cv::findContours(minRect.clone(),cnts4,cv::RETR_EXTERNAL,cv::CHAIN_APPROX_SIMPLE);
c=cnts4[0];
对于(自动iter=cnts4.begin();iter!=cnts4.end();++iter)
{
如果(cv::contourArea(*iter)>cv::contourArea(c))
{
c=*国际热核实验堆;
}
}
缝合=缝合(cv::boundingRect(c));
}
}
来自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)]
在第一种情况下,矩形试图从缝合的
图像中的
(11,12)
提取
(42742845)
大小。这意味着它将从
(11,12)
(42852857)
,这是在
缝合的
图像的范围内,因为
缝合的
图像的大小为
(42952867)
没问题

在第二种情况下,矩形试图从缝合的
图像中的
(12,13)
提取
(42722843)
大小。这意味着它从
(12,13)
(42842856)
,这超出了缝合的图像的范围,因为
缝合的
图像的大小为
(42742845)
问题

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


(-215:断言失败)0打印边框的坐标。这些坐标似乎超出了缝合的
的尺寸image@shawn-mathew我打印了边界矩形的坐标以及缝合的行和列,下面是我得到的:
缝合:cols:4295行:2867边界矩形[4274 x 2845 from(11,12)]缝合:cols:4274行:2845边界矩形[4272 x 2843 from(12,13)]
我如何检查边框的坐标是否超出缝合图像的尺寸?请解释一下为什么在上一次迭代中找不到轮廓?我尝试了你的代码,但仍然在
缝合=缝合(cv::boundingRect(c))时遇到异常;
line:
libc++abi.dylib:以cv类型的未捕获异常终止::异常:OpenCV(4.0.0)/Users/ramyaasha/OpenCV/modules/core/src/umatrix.cpp:545:错误:(-215:断言失败)0
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)]