Search code examples
c++opencvhomographywarp

How to warp image with predefined homography matrix in OpenCV?


I am trying to set predefined values to homography and then use function warpPerspective that will warp my image. First i used findHomography function and displayed result:

H = findHomography(obj, scene, CV_RANSAC);

for( int i=0; i<H.rows; i++){
for( int j=0; j<H.cols; j++){
    printf("H: %d %d: %lf\n",i,j,H.at<double>(i,j));
}
  }

warpPerspective(image1, result, H, cv::Size(image1.cols + image2.cols, image1.rows));

This works as it is supposed to and i get these values

enter image description here

After that i tried to set values for H and call warpPerspective like this:

H.at<double>(0, 0) = 0.766912;
H.at<double>(0, 1) = 0.053191;
H.at<double>(0, 2) = 637.961151;
H.at<double>(1, 0) = -0.118426;
H.at<double>(1, 1) = 0.965682;
H.at<double>(1, 2) = 3.405685;
H.at<double>(2, 0) = -0.000232;
H.at<double>(2, 1) = 0.000019;
H.at<double>(2, 2) = 1.000000;

warpPerspective(image1, result, H, cv::Size(image1.cols + image2.cols, image1.rows));

And now i get System NullReferenceException, do you have any idea why is this failing?


Solution

  • Okay i got help on OpenCV forum, my declaration of H was like this

    cv::Mat H;
    

    this was okay for function fingHomography, but when i wanted to add values manually, i had to declare H like this:

    cv::Mat H(3, 3, CV_64FC1);