opencv将2个图像与y方向的偏移相结合

时间:2014-09-07 09:43:07

标签: c++ image opencv image-processing

我有以下代码在彼此旁边显示2个图像,在x dirrection(间距)和y dirrection(yoffset)上都有轻微偏移:

void output(Mat left, Mat right) {
    Mat imgResult(left.rows + abs(yoffset), right.cols + left.cols + spacing,
            left.type());

    Mat roiImgResult_Left = imgResult(Rect(0, 0, left.cols, left.rows));
    Mat roiImgResult_Right = imgResult(
            Rect(left.cols + spacing, 0, right.cols, right.rows+yoffset));

    Mat roiImg1 = left(Rect(0, 0, left.cols, left.rows));
    Mat roiImg2 = right(Rect(0, 0, right.cols, right.rows+yoffset));
    //Mat roiImg  = Rect(0,0,spacing,right.rows);

    roiImg1.copyTo(roiImgResult_Left); //Img1 will be on the left of imgResult
    roiImg2.copyTo(roiImgResult_Right); //Img2 will be on the right of imgResult
    resize(imgResult, imgResult, imagesize);
    imshow("Final imgage", imgResult);
    cv::moveWindow("Final imgage", screenx, screeny);
}

关键点是yoffset,我似乎无法开始工作,当前版本给出了

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file /home/thijs/Desktop/opencv-2.4.9/Install-OpenCV/Ubuntu/OpenCV/opencv-2.4.9/modules/core/src/matrix.cpp, line 323
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/thijs/Desktop/opencv-2.4.9/Install-OpenCV/Ubuntu/OpenCV/opencv-2.4.9/modules/core/src/matrix.cpp:323: error: (-215) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function Mat 

错误。如果我从Mat roiImg2 = right(Rect(0, 0, right.cols, right.rows+yoffset));删除+ yoffset,它将不会给我一个异常,但结果图像看起来很奇怪。有人有这方面的经验吗?

因此,在某些程度上,这就是代码的作用:

_______
|      |              ^ yoffset 
|left  |   ______     v
|______|   |right|
           |_____|
        <-> spacing 

所有这一切只在一个窗口中进行(不要关心其余部分,以便只是内存中的随机数据)。

1 个答案:

答案 0 :(得分:1)

此异常表示指定的ROI跨越图像边界。

我认为你在这些方面有一个错字:

Mat roiImgResult_Right = imgResult(Rect(left.cols + spacing, 0, right.cols, right.rows+yoffset));
...
Mat roiImg2 = right(Rect(0, 0, right.cols, right.rows+yoffset));

假设yoffset为正,您应该具备以下条件:

Mat roiImgResult_Right = imgResult(Rect(left.cols + spacing, yoffset, right.cols, right.rows));
...
Mat roiImg2 = right(Rect(0, 0, right.cols, right.rows));

如果预期为负yoffset,则相应的代码会稍微复杂一些(因为您必须裁剪右图像移动左图像。)