使用open cv进行角度测量
用了一点初中数学的知识,准确度,跟鼠标点的准不准有关系,话不多说直接上代码
import cv2
import math
path = "test.jpg"
img = cv2.imread(path)
pointsList = []
def mousePoint(event, x, y, flags, params):
# 如果鼠标左键按下
if event == cv2.EVENT_LBUTTONDOWN:
size = len(pointsList)
if size != 0 and size % 3 != 0:
cv2.line(img, tuple(pointsList[round((size - 1)/3) * 3]), (x, y), (0, 0, 255), 2)
cv2.circle(img, (x, y), 5, (0, 0, 255), cv2.FILLED)
pointsList.append([x, y])
# 计算斜率
def gradient(pt1, pt2):
return (pt2[1] - pt1[1])/(pt2[0] - pt1[0])
def getAngle(pointsList):
# 最后三个点
pt1, pt2, pt3 = pointsList[-3:]
# 计算两条直线的斜率
m1 = gradient(pt1, pt2)
m2 = gradient(pt1, pt3)
angR = math.atan((m2 - m1)/(1 + (m2 * m1)))
# 去掉小数,将弧度转换成角度
angD = round(math.degrees(angR))
cv2.putText(img, str(angD), ((pt1[0] - 40), pt1[1] - 20), cv2.FONT_HERSHEY_COMPLEX, 1.5, (0, 0, 255), 2)
while True:
if len(pointsList) % 3 == 0 and len(pointsList) != 0:
getAngle(pointsList)
cv2.imshow("Image", img)
# 设置鼠标回调函数
cv2.setMouseCallback("Image", mousePoint)
# 是否在1毫秒内按下了'q'键
if cv2.waitKey(1) & 0xFF == ord('q'):
pointsList = []
img = cv2.imread(path)