import cv2 import numpy as np defcalcAng(img): edges = cv2.Canny(img, 100, 200) w, h, _ = image.shape y, x = np.where(edges) X = np.stack((x,), axis=-1) A = np.hstack((np.ones((X.shape[0], 1)), X)) w = np.linalg.lstsq(A, y, rcond=None)[0]
theta = np.arctan(-w[::-1][0]) * 180 / np.pi if h>w and theta<30: return90 return theta