3
votes

I try to rectify an image and some points, which are on this image. Rectifying the image works very well (this part of the code is not from me):

(mapx, mapy) = cv2.initUndistortRectifyMap(camera_matrix,dist_coefs,np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]),newCameraMatrix,(int(resolution*w), int(resolution*h)), cv2.CV_32FC1)
RectImg = cv2.remap(img, mapx,mapy,cv2.INTER_LINEAR )

But when I rectify the points with undistortPoints and the same parameters, I get (for my opinion) wrong coordinates.

point_file = np.loadtxt(fn)
point_matrix = np.zeros(shape=(nr_points,1,2))

# fill Pointfile in n x 1 x 2-Matrix
for each in range(0, nr_points):
    point_matrix[each][0][0] = point_file[each][0]
    point_matrix[each][0][1] = point_file[each][1]

point_matrix_new = cv2.undistortPoints(point_matrix, newCameraMatrix, dist_coefs)

These are the parameters I'm using (newCameraMatrix & dist_coefs):

    [[  4.93906295e+02   0.00000000e+00   1.24539714e+03]
     [  0.00000000e+00   4.92616567e+02   1.03814593e+03]
     [  0.00000000e+00   0.00000000e+00   1.00000000e+00]]
    [  5.93179211e-01   3.59577119e-02  -3.34062329e-05  -8.92301489e-05
      -5.27895858e-04   9.28762999e-01   1.46636733e-01   0.00000000e+00]

I'm also not sure, what the output actually is. Are the resulting values are image-coordinates or sensor-coordinates and in which unit? I could only find a documentation of undistortPoints for cv (http://docs.opencv.org/modules/imgproc/doc/geometric_transformations.html) but not for cv2.

example (image size = 2448 x 2048 pixel / sensor size = 8.6 x 6.6 mm

Input (point_matrix):

[[[    0.     0.]]

 [[ 2448.     0.]]

 [[    0.  2048.]]

 [[ 2448.  2048.]]

 [[ 1224.  1024.]]]

Output (point_matrix_new):

[[[-6.49236118 -5.42726306]]

 [[ 6.39989444 -5.5358653 ]]

 [[-6.50237385  5.28935346]]

 [[ 6.58981334  5.54673511]]

 [[-0.04336093 -0.02874159]]]

Thank you for your help!

1

1 Answers

3
votes

so I found my error: I have to use the old and the new camera matrix in cv2.undistortPoints like in cv2.initUndistortRectifyMap.

so to correct it I just used this code:

point_matrix_new = cv2.undistortPoints(point_matrix,camera_matrix,dist_coefs,P=newCameraMatrix)