I am trying to undistort a fisheye image using OpenCV. I already calibrated the camera, and also getting a decent undistorted image using the following code (I just posted the relevant part):
img = cv2.imread('img.jpeg') #image path of pic to distort
h,w = img.shape[:2]
Knew = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(K,D,(w,h),None)
Knew[(0,1), (0,1)] = .3* Knew[(0,1), (0,1)]
mapx, mapy = cv2.fisheye.initUndistortRectifyMap(K,D,None,Knew,(w,h),5)
img_undistorted = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)
cv2.imshow('undistorted', img_undistorted)
cv2.waitKey(0)
cv2.destroyAllWindows()
Now I have to crop the fisheye image like this:
These photos are only for demonstration purposes. I used the mapping function of a fisheye lens to determine the point where I crop at the right side. However, if I use the same code as above (and same camera matrix), the output is a really weird distorted picture.
I have also thought about first undistorting and then cropping, however I am not able to calculate exactly the point I have to crop in this way. So I have to crop the image first.
So how do I correctly undistort a not symmetrically cropped fisheye image?