2

I use the following code to georeference images

with input

grid    = "for example a utm grid"
img_raw = cv2.imread(filename)
mtx, dist = "intrinsic camera matrix and 
             distortion coefficient from calibration matrix"
src_pts = "camera location of gcp on undistorted image"
dst_pts = "world location of gcp in the grid coordinate"

I correct camera distortion and apply homography

img = cv2.undistort(img_raw, mtx, dist, None, None)

H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
img_geo = cv2.warpPerspective(img,(grid.shape[0],grid.shape[1]),\
                              flags=cv2.INTER_NEAREST,borderValue=0)

then I want to get the location of the camera. I try to use the rotation and translation matrix calculated in cv2.solvePnP as shown here for example. If I am right, I need camera and world coordinates for at least a set of 4 coplanar points.

flag, rvec, tvec = cv2.solvePnP(world, cam, mtx, dist)

Again if I am right, in solvePnP, the camera coordinates need to be from the raw image frame and not the undistorted frame as in src_pts.

So my question is, how could I get the pixels location of src_pts in the raw image frame? Or is there any other way to get rvec and tvec?

Community
  • 1
  • 1
paugam
  • 150
  • 2
  • 12

2 Answers2

0

Maybe the function projectPoints is what you need. Here the link: http://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#projectpoints

damianodamiano
  • 2,528
  • 2
  • 13
  • 21
  • coord_pts_in = cv2.undistortPoints(coord_pts_in_raw, K, D, P=K) – paugam Oct 14 '16 at 05:57
  • thanks, though `projectPoints` required input of rvec and tvec which I was trying to get from solvePnP. I reply below with the workaround I found. – paugam Oct 14 '16 at 06:05
0

This is the solution I found

grid    = "for example a utm grid"
img_raw = cv2.imread(filename)
mtx, dist = "intrinsic camera matrix and 
         distortion coefficient from calibration matrix"
src_pts = "camera location of gcp on raw image"
dst_pts = "world location of gcp in the grid coordinate"

note src_pts are now points in the original distorted image

src_pts_undistorted = cv2.undistortPoints(src_pts, K, D, P=K)
img = cv2.undistort(img_raw, mtx, dist, None, None)

H, mask = cv2.findHomography(src_pts_undistorted, dst_pts, cv2.RANSAC,5.0)
img_geo = cv2.warpPerspective(img,(grid.shape[0],grid.shape[1]),\
                          flags=cv2.INTER_NEAREST,borderValue=0)

then I can get the pose from solvePnP

flag, rvec, tvec = cv2.solvePnP(dst_pts, src_pts, mtx, dist)
paugam
  • 150
  • 2
  • 12
  • How does third code fragment related to second? As I can see, all arguments to `solvePnP` are inputs from first code fragment. – RomanS Aug 02 '19 at 12:38