c++opencvimage-stitchingopencv-stitching

Opencv Resize a stitch


I'm doing a mosaic from a video in Opencv. I'm using this example for stitching the frames of the videos: http://docs.opencv.org/doc/tutorials/features2d/feature_detection/feature_detection.html. At the end I'm doing this for merging the new frame with the stitch created at the passed iteration:

Mat H = findHomography(obj, scene, CV_RANSAC);

static Mat rImg;
warpPerspective(vImg[0], rImg, H, Size(vImg[0].cols, vImg[0].rows), INTER_NEAREST);//(vImg[0], rImg, H, Size(vImg[0].cols * 2, vImg[0].rows * 2), CV_INTER_LINEAR);

static Mat final_img(Size(rImg.cols*2, rImg.rows*2), CV_8UC3);
static Mat roi1(final_img, Rect(0, 0, vImg[1].cols, vImg[1].rows));
Mat roi2(final_img, Rect(0, 0, rImg.cols, rImg.rows));

rImg.copyTo(roi2);
vImg[1].copyTo(roi1);

imwrite("stitch.jpg", final_img);
vImg[0] = final_img;

So here's my problem: obviously the stitch becomes larger at each iteration, so how can I resize it to make it fit in the final_img image?

EDIT Sorry but I had to remove images


Solution

  • For the second question, what you observe is an error in the homography that was estimated. This may come either from:

    1. drift (if you chain homographies along the sequence), ie, small errors that accumulate and become large after dozens of frames
    2. or (more likely) because your reference image is too old with respect to your new image, and they exhibit too few matching points to give an accurate homography, but yet enough to find one that passes the quality test inside cv::findHomography().

    For your first question, you need to add some code that keeps track of the current bounds of the stitched image in a fixed coordinate frame. I would suggest to choose the coordinates linked with the first image.

    Then, when you stitch a new image, what you do really is to project this image onto this coordinate frame. You can compute first for example the projected coordinates of the 4 corners of the incoming frame, test if they fit into the current stitching result, copy it to a new (bigger) image if necessary, then proceed with stitching the new image.