I've been looking for a response in other posts and trying several ways to get it but I couldn't find anything solving my problem. I'm trying to correct optical distortion in an image and I get it, but now I want to crop my image in order to delete the curved black borders in the result image. So, in summary, this is my problem:
I want to crop like this:
I've tried to crop 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 roi
always takes the value (0,0,0,0)
.
Could anyone help me?
Thanks in advance.
As the void part has its maximum/minimum in the middle of the image, you could look for the middle lines and see when there is a change of color. The image below may clarify this:
You just need to find the
x1
,y1
,x2
andy2
shown in the image. This can be done as follows:What I did is a list of the pixel colors in the middle lines, and then loop them until the color is above a threshold (0 is Black and 255 is White). When the first pixel that changes of color is detected, the position is stored and the loop breaks. The output is the cropped image:
Note: I started looping from position 2 because of the border line, with the original image this is not needed.
Hope this helped!