Search code examples
c++opencvperspectivecamera

Why is findHomography returning bad result?


I let the user pick 5 points on two images, which are not the same size (maybe that's the problem). As the user picks a point on whichever image, I'm pushing that Point2f a vector dedicated to that particular image.

So in one instance I have the following data:

1. Image 2 at x: 607, and y: 286
2. Image 2 at x: 750, and y: 367
3. Image 2 at x: 527, and y: 353
4. Image 2 at x: 752, and y: 469
5. Image 2 at x: 584, and y: 482

--

1. Image 1 at x: 301, and y: 343
2. Image 1 at x: 440, and y: 328
3. Image 1 at x: 275, and y: 472
4. Image 1 at x: 478, and y: 457
5. Image 1 at x: 389, and y: 597

However findHomohraphy returns

[0, 0, 0;

 0, 0, 0;

 0, 0, 0]

This is what my function does once it gets a hold of 2 vectors with 4 points.

Mat tranform( std::vector<cv::Point2f> src, std::vector<cv::Point2f> dst) {
     transMatrix = findHomography(src, dst, CV_RANSAC);
     std::cout << transMatrix << std::endl;
     return transMatrix;
}

I also tried changing the method argument of findHomohraphy from 0 to CV_RANSAC.

I'm debugging a bit further, and I'm having trouble printing the Point2f vector. Maybe I'm not assigning it correctly

This is what I'm currently doing in an image window callback function.

imgOnePoints.assign(imgOneIndex, Point2f(x,y));

Solution

  • Turns out i really wasn't assigning the vector correctly

    I fixed it by implemented assignment using push_back

    imgOnePoints.push_back(Point2f(x,y));
    

    Now I'm getting the following result

    [9.160730442076495, -1.849442562331516, -180.7111392985398;
      4.977742556341154, 2.013216209765684, -1267.475935610038;
      0.007750660030394876, -0.0003148605099947704, 0.9999999999999999]