Question

I develop a simple stabilizer and I'm having difficulty with the shift of the image. Example - I have two images (A and B) are shifted relative to each other by a few pixels. I calculate the offset using phase correlations. The next step I need to move the second image by the image's offset. An example is presented on the image. How do I solve this problem?

There is link to preview image:

illustration

Was it helpful?

Solution

cv::Rect and cv::Mat::copyTo

cv::Mat img=cv::imread("image.jpg");
cv::Mat imgTranslated(img.size(),img.type(),cv::Scalar::all(0));
img(cv::Rect(50,30,img.cols-50,img.rows-30)).copyTo(imgTranslated(cv::Rect(0,0,img.cols-50,img.rows-30)));

OTHER TIPS

setTo(0) the destination image, then use the operator() of cv::Mat to create a subimage of the input image and of the output image (you'll use 2 cv::Rect with same size and different displacement; the size is dependent on the displacement, i.e. bigger displacement means smaller part of the image you can copy in output). Then use method copyTo.

Said this: normally when asking a question you would show some code showing what you have tried so far.

My implementation allows the shift to be in any direction...

using namespace cv;
//and whatever header 'abs' requires...

Mat offsetImageWithPadding(Const Mat& originalImage, int offsetX, int offsetY, Scalar backgroundColour){
        padded = Mat(originalImage.rows + 2 * abs(offsetY), originalImage.cols + 2 * abs(offsetX), CV_8UC3, backgroundColour);
        originalImage.copyTo(padded(Rect(abs(offsetX), abs(offsetY), originalImage.cols, originalImage.rows)));
        return Mat(padded,Rect(abs(offsetX) + offsetX, abs(offsetY) + offsetY, originalImage.cols, originalImage.rows));
}

//example use with black borders along the right hand side and top:
Mat offsetImage = offsetImageWithPadding(originalImage, -10, 6, Scalar(0,0,0));

It's taken from my own working code but some variables changed, if it doesn't compile, very likely just a small thing needs changing - but you get the idea re. the abs function...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top