角度检测

import cv2
import math
path = 'img_2.png'
img = cv2.imread(path)
pointList = []
def mousePoint(event,x,y,flags,params):
print(x,y)
if event == cv2.EVENT_LBUTTONDOWN:
size = len(pointList)
if size != 0 and size % 3 != 0:
cv2.line(img,tuple(pointList[round((size-1)/3)*3]),(x,y),(0,0,250),2)
cv2.circle(img,(x,y),2,(0,0,250),cv2.FILLED)
pointList.append([x,y])
def gradient(pt1,pt2):
return (pt2[1]-pt1[1])/(pt2[0]-pt1[0])
def getAngle(pointList):
pt1,pt2,pt3 = pointList[-3:]
m1 = gradient(pt1,pt2)
m2 = gradient(pt1,pt3)
angR = math.atan((m1-m2)/(1+m1*m2))
angD = round(math.degrees(angR))
cv2.putText(img,str(angD),(pt1[0]-40,pt1[1]-20),cv2.FONT_HERSHEY_COMPLEX,1.5,(0,0,250),1)

while True:
if len(pointList) % 3 == 0 and len(pointList) != 0:
getAngle(pointList)
cv2.imshow('result',img)
cv2.setMouseCallback('result',mousePoint)
if cv2.waitKey(1) & 0xFF == ord('q'):
pointList = []
img = cv2.imread(path)
posted @   记录——去繁就简  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示