2

I have taken a photo A using an RGB camera. And I know the position of a point g in photo A. The camera needs to do a camera calibration. Now I want to know the position of point g after calibration. I am using the code as following, but I want to get the point position, not image. How can I do that? Can you give me some advice?

initUndistortRectifyMap(
        cameraMatrix,   
        distCoeffs,     
        Mat(),      
        Mat(),      
        Size(640, 480),
        CV_32FC1,      
        map1, map2);  
 remap(A, B, map1, map2, cv::INTER_LINEAR);  

Point2f g = Point2f(...,...);//i want to get the new position of the point not image B   
Georgy
  • 12,464
  • 7
  • 65
  • 73
kookoo121
  • 1,116
  • 2
  • 12
  • 21
  • Very related: https://stackoverflow.com/questions/41703210/inverting-a-real-valued-index-grid – pasbi Feb 24 '21 at 10:38

3 Answers3

3

Just get coordinates using maps:

x,y - coordinates after (not before),as pasbi correctly noticed in comments, mapping.

(map1(y,x),map2(y,x)) - coordinates before mapping

In other words:

  • map1.at<float>(y,x) contains source x coordinates for each destination point p(x,y).

  • map2.at<float>(y,x) contains source y coordinates for each destination point p(x,y).

See documentation on remap function.

Andrey Smorodov
  • 10,649
  • 2
  • 35
  • 42
  • Thank you.I have tried it. But in my result,map1 contains transformed y coordinates and map2 contains transformed x? – kookoo121 Dec 15 '15 at 01:49
  • Yes, you are right I've mistyped in my answer, must be: map1.at(y,x) because matrix coordinates are (row,col). Corrected now. – Andrey Smorodov Dec 15 '15 at 10:13
  • I was doing image rectification(remapping), locating interesting points on a rectified image, and wanted to plot them on the original undistorted image. If I wanted to rotate back the whole image, then I would have to calculate other maps from an inverse homography matrix, like [here](https://stackoverflow.com/a/41982442/7851470). But in the case when I want to rotate back just the set of points, not the whole image, I don't need to calculate new maps, I simply need to use old maps from the first rectification process. This took me a while to understand. – Georgy May 24 '18 at 13:44
  • 4
    *This answer is not correct*. As stated in the docs you refer to, it is `dst(x, y) = src(map_x(x, y), map_y(x, y))`. Transforming points `dst -> src` is easy (lookup in `map_x` and `map_y`), but the OP wants the other (more natural) direction: `src -> dst`. This is admittingly confusing because `cv::remap` works "inversely" (for numerical stability reasons). I.e., in order to map an image `src -> dst`, you supply a mapping from `dst -> src`. Unfortunately, that's only efficient when transforming many points on a regular grid (i.e. image). Transforming a single random point is pretty difficult. – pasbi Feb 24 '21 at 10:17
  • 1
    Thank you! You are absolutery right, my bad. Fixed answer as possible, may be will found better solution. – Andrey Smorodov Feb 24 '21 at 11:28
1

undistortPoints() is your need。

// src_pts are points in raw(distort) img, rectify_pt_vec are in rectifyImageL
// RL, PL are from stereoRectify()
   cv::undistortPoints(src_pts, rectify_pt_vec, cameraMatrixL, distCoeffL, RL, PL);  

how to get point in srcimg from dstimg just like pasbi commented below.

yaodi
  • 61
  • 1
  • 3
0

best method i found was to recreate a camera matrix, with inverted parameters. work to a certain extent, with like basic images modifications