我用霍夫变换可以识别出来分针和时针, 但是我需要知道那条线更长, 这样才能知道那个是分针那个是时针。
现在的问题是, 霍夫变换只是标注直线而不是线段, 所以这边没法知道那个指针更长, 这个应该怎么做?
我用的测试图片 https://imgtu.com/i/c7ocLQ
我现在用的代码
import cv2
import numpy as np
img = cv2.imread('clock.png')
h, w = img.shape[:2]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150)
lines = cv2.HoughLines(edges, 1, np.pi / 180, 100)
for i in range(len(lines)):
for rho, theta in lines[i]:
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + w * (-b))
y1 = int(y0 + w * (a))
x2 = int(x0 - w * (-b))
y2 = int(y0 - w * (a))
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv2.imshow("edges", edges)
cv2.imshow("lines", img)
cv2.waitKey()
cv2.destroyAllWindows()
1
vincentV2 2021-04-20 16:30:09 +08:00
HoughLinesP
|