I have a camera with a fish-eye lens. I successfully undistort my image, but I want to undistort point coordinates. I know the maps contain src image coordinates for each pixel in dst so I cannot directly use those without some iterative algoritm. I thought undistortPoints() would do that, but it incorrectly transforms the first point and it doesn't change the second point. Using undistortPointsIter() and setting the criteria higher doesn't work either.
A similar question was asked before and this answer doesn't work for me either. I get the exact same results with undistortPoints().
So how do I get the pixel points for my undistorted image from pixel points in the distorted image?
My camera parameters:
print(K)
print(D)
print(Dims)
[[338.37324094 0. 319.5 ]
[ 0. 339.059099 239.5 ]
[ 0. 0. 1. ]]
[[ 0.01794191]
[-0.12190366]
[ 0.14111533]
[-0.09602948]]
(640, 480)
My code:
img = cv2.imread('Chessboards\img_021.jpg')
img_undistorted = cv2.remap(img, unfishmap1, unfishmap2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
points1=[]
points1.append((165,260))
points1.append((175,410))
print(points1)
img2= img.copy()
for p in points1:
cv2.circle(img2,p,6,(0,0,255),2)
imgshow(img2)
point_matrix = np.zeros(shape=(len(points1),1,2),dtype=np.float32)
for i in range(0, len(points1)):
point_matrix[i][0][0] = points1[i][0]
point_matrix[i][0][1] = points1[i][1]
print(point_matrix)
points_undistorted = cv2.undistortPoints(point_matrix,K,D,R=None,P=K)
points2=[]
for p in points_undistorted:
points2.append( (int(p[0][0]),int(p[0][1])) )
print(points2)
img2= img_undistorted.copy()
for p in points2:
cv2.circle(img2,p,6,(0,0,255),2)
imgshow(img2)
#expected
points3=[]
points3.append((155,265))
points3.append((150,443))
print(points3)
img2= img_undistorted.copy()
for p in points3:
cv2.circle(img2,p,6,(0,0,255),2)
imgshow(img2)
Result:
It worked thanks to solution provided by micka.
This is what worked for me:
source image
Result: