def estimate_skew(image):
edges = auto_canny(image)
lines = cv2.HoughLines(edges, 1, np.pi / 90, 200)
new = edges.copy()
thetas = []
for line in lines:
for rho, theta in line:
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))
if theta > np.pi / 3 and theta < np.pi * 2 / 3:
thetas.append(theta)
new = cv2.line(new, (x1, y1), (x2, y2), (255, 255, 255), 1)
theta_mean = np.mean(thetas)
theta = rad_to_deg(theta_mean) if len(thetas) > 0 else 0
return theta
评论列表
文章目录