img = cv2.imread('Undistorted.jpg', 0)
h, w = img.shape
x1, x2, y1, y2 = 0, 0, 0, 0
void_th = 10
vertical = [img[i, int(w / 2)] for i in range(h)]
horizontal = [img[int(h / 2), i] for i in range(w)]
vertical_rev = vertical[::-1]
horizontal_rev = horizontal[::-1]
for i in range(2, h):
    if vertical[i] > void_th and y1 == 0:
        y1 = i
if vertical_rev[i] > void_th and y2 == 0:
    y2 = i
if y1 != 0 and y2 != 0:
    break
for i in range(2, w):
    if horizontal[i] > void_th and x1 == 0:
        x1 = i
if horizontal_rev[i] > void_th and x2 == 0:
    x2 = i
if x1 != 0 and x2 != 0:
    break
desired_result = img[y1:h - y2, x1:w - x2]
cv2.imshow('Crop', desired_result)
Crop image with corrected distortion in OpenCV (Python)