import cv2
import math
path = './Angle Finder/background.png'
image = cv2.imread(path)
pointsList = []
def mousePoints(event, x, y, flags, params):
if event == cv2.EVENT_LBUTTONDOWN:
size = len(pointsList)
if size != 0 and size % 3 != 0:
cv2.line(image, tuple(pointsList[round((size-1)/3)*3]), (x, y), (0, 0, 255), 1)
print(pointsList)
cv2.circle(image, (x, y), 2, (0, 0, 255), cv2.FILLED)
pointsList.append([x, y])
def slope(point1, point2):
return (point2[1]-point1[1])/(point2[0]-point1[0])
def getAngle(pointsList):
point1, point2, point3 = pointsList[-3:]
slope1 = slope(point1, point2)
slope2 = slope(point1, point3)
angR = math.atan((slope2-slope1)/(1+(slope2*slope1)))
angD = round(math.degrees(angR))
cv2.putText(image, str(angD), (point1[0]-40, point1[1]-20), cv2.FONT_HERSHEY_COMPLEX, 1.5, (255, 0, 0), 2)
while True:
if len(pointsList) % 3 == 0 and len(pointsList) != 0:
getAngle(pointsList)
cv2.imshow('Image', image)
cv2.setMouseCallback('Image', mousePoints)
if cv2.waitKey(1) & 0xFF == ord('q'):
pointsList = []
image = cv2.imread(path)
运行结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/9ef9c6519b46413fa1511d990e07855d.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5Y2K5aOV5pil5rC0,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)