How to use Map class to implement image registration?

asked 2018-05-17 22:20:17 -0600

yode gravatar image

updated 2018-05-22 05:15:43 -0600

Actually, I have read the official documentation here about class Map in opencv to try to use the module reg. And This is my test image:

enter image description here

This is my code:

#include<opencv.hpp>
#include "opencv2/reg/mapshift.hpp"
#include "opencv2/reg/mappergradshift.hpp"
#include "opencv2/reg/mapperpyramid.hpp"

using namespace cv;
using namespace std;
using namespace cv::reg;

Mat highlight1(const Mat src, const Mat t_mask) {
    Mat srcImg = src.clone(), mask = t_mask.clone();
    threshold(mask, mask, 0, 255, THRESH_BINARY_INV + THRESH_OTSU);
    cvtColor(mask, mask, COLOR_GRAY2BGR);
    cvtColor(srcImg, srcImg, COLOR_GRAY2BGR);
    dilate(mask - Scalar(0, 0, 255), mask, Mat(), Point(-1, -1), 1);
    return srcImg - mask;
}

int main() {
    Mat img1 = imread("img.jpg", 0);
    Mat img2;

    // Warp original image
    Vec<double, 2> shift(5., 5.);
    MapShift mapTest(shift);
    mapTest.warp(img1, img2);

    // Register
    Ptr<MapperGradShift> mapper = makePtr<MapperGradShift>();
    MapperPyramid mappPyr(mapper);
    Ptr<Map> mapPtr = mappPyr.calculate(img1, img2);
    MapShift* mapShift = dynamic_cast<MapShift*>(mapPtr.get());

    // Display registration result
    Mat result;
    mapShift->inverseWarp(img2, result);
    Mat registration_before = highlight1(img1, img2);
    Mat registration_after = highlight1(img1, result);
    return 0;
}

But as we see, the registration_after is even worse than registration_before. What's I have missed?

This is registration_before:

This is registration_after:

edit retag flag offensive close merge delete