Fixing lens distortion - homework help

Good morning people, everything fine ?
I have a photo and I need to make the gray rectangle exactly the same as the red one (without distortion).
Here’s the program I’m doing (I almost did it), here’s the image too and an example of what I need.
Thank you very much :slight_smile:

Codigo
def apply_distortionROI(image_content, k1, k2, k3, k4, roi):
imagem = Image.open(io.BytesIO(image_content))
img_np = np.array(imagem)
h, w = img_np.shape[:2]
camera_matrix = np.array([[w, 0, w / 2], [0, h, h / 2], [0, 0, 1]], dtype=np.float64)
dist_coefs = np.array([k1, k2, k3, k4], dtype=np.float64)
new_camera_matrix, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coefs, (w, h), 1, (w, h))
map1, map2 = cv2.fisheye.initUndistortRectifyMap(camera_matrix, dist_coefs, None, new_camera_matrix, (w, h), cv2.CV_16SC2)
img_undistorted = cv2.remap(img_np, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
imagem = Image.fromarray(img_undistorted)
image_buffer = io.BytesIO()
imagem.save(image_buffer, format=“BMP”)
return image_buffer.getvalue()

b1 = -0.1    
b2 = 0.2     
b3 = 0.02    
b4 = -0.02   
roi = (100, 50, 300, 200) 
image_content = apply_distortionROI(image_content, b1, b2, b3, b4, roi)

crosspost:

can help me ? tanks so much