Ask Your Question
0

How to warp image with predefined homography matrix in OpenCV?

asked 2018-02-18 13:40:21 -0600

Mereth gravatar image

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

image description

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?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2018-02-18 14:28:03 -0600

LBerger gravatar image

Without your code it is difficult to understand the problem but this code is correct : no exception

    Mat H(3, 3, CV_64FC1);
    Mat imgbgr = imread("g:/lib/opencv/samples/data/rubberwhale1.png", IMREAD_COLOR);
    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;
    Mat result;
    warpPerspective(imgbgr, result, H, cv::Size(imgbgr.cols + imgbgr.cols, imgbgr.rows));
edit flag offensive delete link more

Comments

1

yes this works thank you, problem was in declaration of H, i just declared it as Mat H and findHomography did not have problem, but i had to change that for manual definition of H values

Mereth gravatar imageMereth ( 2018-02-18 14:42:11 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-02-18 13:40:21 -0600

Seen: 3,345 times

Last updated: Feb 18 '18