本文介绍如何使用python语言实现角度测量,程序包括鼠标选点、直线斜率计算、角度计算三个子程序和一个主程序。最终实现效果:在图片上用鼠标确认三点,程序将会显示由此三点确定的角度,如下图所示。
1、鼠标选点
# -*- coding: utf-8 -*- import cv2 path = "picture_mqa\\angle_measure.bmp" img = cv2.imread(path) pointsList = [] def mousePoints(event,x,y,flags,params): if event == cv2.EVENT_LBUTTONDOWN: cv2.circle(img,(x,y),5,(0,0,255),cv2.FILLED) print(x,y) while True: cv2.imshow('Image', img) cv2.setMouseCallback('Image',mousePoints) key_scan = cv2.waitKey(1) & 0xff if key_scan == ord("q"): pointsList = [] img = cv2.imread(path) elif key_scan == ord("s"): break cv2.destroyAllWindows()
while循环内cv2.setMouseCallback('Image',mousePoints)为鼠标中断触发事件的开启函数,作用是当在Image图片上鼠标触发中断事件时,程序跳转到mousePoints()中断服务函数内,并给mousePoints()的五个入口参数event,x,y,flags,params赋值。其中, event是cv2_EVENT_* (MouseEventTypes)类型的变量,为鼠标触发中断事件的类型;x和y为鼠标触发中断事件时在image图像的横纵坐标;flags是cv2_EVENT_FLAG_* (MouseEventFlags)类型的变量,为特殊中断事件的标志位;param是用户自定义的参数。本文的程序中使用 EVENT_LBUTTONDOW#左键点击触发事件,当鼠标左键点击时,标注该点并记录其坐标。
event的赋值:
EVENT_MOUSEMOVE #滑动
EVENT_LBUTTONDOWN #左键点击
EVENT_RBUTTONDOWN #右键点击
EVENT_MBUTTONDOWN #中键点击
EVENT_LBUTTONUP #左键放开
EVENT_RBUTTONUP #右键放开
EVENT_MBUTTONUP #中键放开
EVENT_LBUTTONDBLCLK #左键双击
EVENT_RBUTTONDBLCLK #右键双击
EVENT_MBUTTONDBLCLK #中键双击
2、角度计算
由1可以得到鼠标点击位置处的坐标,我们将其放入pointList列表内。当列表内的坐标数目为3的倍数时调用getAngle()函数,计算出三点确定的两条直线的夹角。
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 = abs(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))
由直线的两点式方程可得直线的倾斜角为angle = arctan(y2-y1,x2-x1),则两条直线的夹角为angle0 =angle1-angle2 = arctan(y2-y1,x2-x1) - arctan(y2-y3,x2-x3)。以上函数便可根据三点的坐标值求其形成夹角的角度。
3、完整程序
# -*- coding: utf-8 -*- ''' 测量鼠标点击过的三点形成的角度 ''' import cv2 import math path = "picture_mqa\\angle_measure.bmp" #图片路径 img = cv2.imread(path) pointsList = [] #鼠标中断触发函数,将鼠标触发事件位置处描点并将该点的坐标值纪录入pointList列表内 #连接相邻三点使其形成一个夹角 def mousePoints(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)) 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 = abs(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)) while True: cv2.imshow('Image', img) #图片显示 cv2.setMouseCallback('Image',mousePoints) #鼠标触发事件开启 if len(pointsList) % 3 ==0 and len(pointsList)!=0: #鼠标每触发中断3次计算一次其形式夹角的角度值 getAngle(pointsList) key_scan = cv2.waitKey(1) & 0xff #键盘扫描 if key_scan == ord("q"): #输入'q'时图片刷新 pointsList = [] img = cv2.imread(path) elif key_scan == ord("s"): #输入's'时退出程序 break cv2.destroyAllWindows()