Image rotation has to be after image undistortion. This is because:
* Camera calibration is performed before any image rotation. Resulting camera parameters are associated with the image orientation.
* If image rotation, e.g. 90 degree, is applied before undistortion, undistortion using original camera parameters is no longer valid. This can cause severe error in the resulting images.