I searched the answer in other posts and tried several ways to get it, but I did not find anything that could solve my problem. I am trying to correct the optical distortion of an image and get it, but now I want to crop the image to remove the curved black borders in the result image. So, in summary, this is my problem:

I want to crop like this:

I tried to trim with the following code:
h, w = corrected_img.shape[:2]
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(cameraMTX,distortionCoeffs,(w,h),1,(w,h))
x,y,w,h = roi
desired_result = corrected_img[y:y+h, x:x+w]
But, unfortunately, it roialways takes on value (0,0,0,0).
Can anyone help me?
Thanks in advance.
source
share