Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

This is what I get:

Registration before:

Registration before

Registration after:

Registration after


Looks like you need to convert the image to float. See the corresponding unit test.


Full code:

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.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("RuesS.jpg", IMREAD_GRAYSCALE);
  Mat img1_float;
  img1.convertTo(img1_float, CV_32FC1);
  Mat img2_float, img2;

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

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

  // Display registration result
  Mat result_float, result;
  mapShift->inverseWarp(img2_float, result_float);
  result_float.convertTo(result, CV_8UC1);
  Mat registration_before = highlight1(img1, img2);
  Mat registration_after = highlight1(img1, result);

  imshow("registration_before", registration_before);
  imshow("registration_after", registration_after);
  waitKey();
  return 0;
}

This is what I get:

Registration before:

Registration before

Registration after:

Registration after


Looks like you need to convert the image to float. See the corresponding sample code and corresponding unit test.


Full code:

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.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("RuesS.jpg", IMREAD_GRAYSCALE);
  Mat img1_float;
  img1.convertTo(img1_float, CV_32FC1);
  Mat img2_float, img2;

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

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

  // Display registration result
  Mat result_float, result;
  mapShift->inverseWarp(img2_float, result_float);
  result_float.convertTo(result, CV_8UC1);
  Mat registration_before = highlight1(img1, img2);
  Mat registration_after = highlight1(img1, result);

  imshow("registration_before", registration_before);
  imshow("registration_after", registration_after);
  waitKey();
  return 0;
}