Undistort rotated images

I’ve performed calibration and have the camera matrix and distortion parameters. I apply them as:

img = cv2.imread(filename)
h,w = img.shape[:2]

mtx = np.matrix([[fx, 0, cx],[0, fy, cy],[0, 0, 1]])
dist = np.array([k1, k2, p1, p2, k3])
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w,h), 1, (w,h))

dest = cv2.undistort(img, mtx, dist, None, newcameramtx)
x,y,w,h = roi
dest = dest[y:y+h, x:x+w]
cv2.imwrite(outfilename, dest)

However, when capturing images, we will be applying rotations automatically, based on the camera orientation. For example, some will be rotated 90 degrees clockwise. I’m trying to figure out how to continue to make use of this undistort process. Of course I can rotate back into the standard orientation, undistort as above, and then re-rotate. But I thought I might be able to manipulate the parameters to apply directly. I think the fx, fy can simply be swapped, and the cx_new = w_new-cy_old, cy_new = cx_old. But I’m not sure about the k1,k2,k3,p1,p2 parameters, and the newcameramtx, roi. I’ve experimented a bunch, but haven’t yet got it to output a correctly undistorted image. Any insights? Thanks,

depends on how much trouble you want to go to. you should figure out ways to not rotate those pictures. and if you do, just stick with rotating them, and scolding the user.

you could multiply a rotation matrix onto the camera matrix. don’t dig into the matrices and try to figure out the closed form expressions, that only causes headache.

the distortion coefficients… the radial ones stay. the tangential ones you’d have to look at the equations and figure out what they mean… perhaps just set them to 0 (during calibration).