Thijser
Thijser

Reputation: 2633

opencv combine 2 images with offset in y dirrection

I have the following code to display 2 images next to eachother with a slight offset both over the x dirrection(spacing) and the 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);
}

The critical point is the yoffset which I cannot seem to get to work, the current version gives a

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 

error. If I remove the +yoffset from Mat roiImg2 = right(Rect(0, 0, right.cols, right.rows+yoffset)); it will not give me an exception but the resulting image looks really weird. Anybody got any experience with this?

So in somewhat immages this is what the code does:

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

all of this in just one window(don't care what is in the rest so that is just random data that was in memory).

Upvotes: 0

Views: 1493

Answers (1)

BConic
BConic

Reputation: 8980

This exception says that the specified ROI spans beyond the image boundaries.

I think you have a typo on these lines:

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

Assuming yoffset is positive, you should have the following:

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

If negative yoffset are to be expected, the appropriate code would be a little more complex (as you would have to crop the right image or shift the left one).

Upvotes: 1

Related Questions