Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Angle Measurement by Python+OpenCV

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "Python+OpenCV how to achieve angle measurement", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve angle measurement in Python+OpenCV" this article.

The final effect: confirm the three points on the picture with the mouse, and the program will display the angle determined by the three points, as shown in the following figure.

1. Mouse selection

#-*-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, (XMague y), 5, (0meme 0255), cv2.FILLED) print (XMague 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 ()

The cv2.setMouseCallback ('Image',mousePoints) in the while loop is the opening function of the mouse interrupt trigger event. When the mouse triggers the interrupt event on the Image picture, the program jumps to the mousePoints () interrupt service function and assigns values to the five entry parameters event,x,y,flags,params of mousePoints (). Among them, event is a variable of cv2_EVENT_* (MouseEventTypes) type, which is the type of interrupt event triggered by the mouse; x and y are the horizontal and vertical coordinates of the image image when the mouse triggers the interrupt event; flags is a variable of cv2_EVENT_FLAG_* (MouseEventFlags) type, which is the flag bit of a special interrupt event; and param is a user-defined parameter. The EVENT_LBUTTONDOW# left-click trigger event is used in the program in this paper. When the left mouse button is clicked, the point is marked and its coordinates are recorded.

Assignment of event:

EVENT_MOUSEMOVE # Slide

EVENT_LBUTTONDOWN # left click

EVENT_RBUTTONDOWN # right click

EVENT_MBUTTONDOWN # Middle Click

EVENT_LBUTTONUP # left button release

EVENT_RBUTTONUP # right let go

EVENT_MBUTTONUP # release the middle key

EVENT_LBUTTONDBLCLK # left double click

EVENT_RBUTTONDBLCLK # right and double click

EVENT_MBUTTONDBLCLK # Middle key double-click

2. Angle calculation

The coordinates of the mouse click location can be obtained from 1, and we put it in the pointList list. The getAngle () function is called when the number of coordinates in the list is a multiple of 3 to calculate the angle between the two lines determined by the three points.

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, (0meme0255))

From the two-point equation of a straight line, we can get that the tilt angle of the straight line is angle = arctan (y2mury1line x2murx1), then the angle between the two lines is angle0 = angle1-angle2 = arctan (y2mury1line x2murx1)-arctan (y2mury3line x2murx3). The above function can calculate the angle of the angle according to the coordinate values of the three points.

3. Complete program

#-*-coding: utf-8-* -''measure the angle formed by three points clicked by the mouse' 'import cv2import math path = "picture_mqa\\ angle_measure.bmp" # Image path img = cv2.imread (path) pointsList = [] # Mouse interrupt trigger function Trace the point at the location of the mouse trigger event and record the coordinate value of the point in the pointList list # connect the adjacent three points to form an angle def mousePoints (event,x,y,flags,params): if event = = cv2.EVENT_LBUTTONDOWN: size = len (pointsList) if size! = 0 and size%3! = 0: cv2.line (pointsList [round ((size-1) / 3) * 3]), (XMague y), (0Power0) ) cv2.circle (img, (XMague y), 5, (0Magne0255), cv2.FILLED) pointsList.append ([XMague y]) # calculate the slope def gradient (pt1) of the straight line on which the two points are located from the coordinate values of the two points Pt2): return ((pt2 [1]-pt1 [1]) / (pt2 [0]-pt1 [0])) # calculate the angle value def getAngle (pointsList): pt1,pt2,pt3 = pointsList [- 3:] M1 = gradient (pt1,pt2) m2 = gradient (pt1) based on the adjacent three points Pt3) angR = abs (math.atan ((m2-m1) / (1+m2*m1)) angD = round (math.degrees (angR)) cv2.putText (img,str (angD), (pt1 [0]-40 cv2.putText PT1 [1]-20), cv2.FONT_HERSHEY_COMPLEX, 1.5, (0pp0255) while True: cv2.imshow ('Image', img) # Picture display cv2.setMouseCallback (' Image') MousePoints) # Mouse trigger event open if len (pointsList)% 3 = 0 and len (pointsList)! = 0: # calculate the angle value of its formal angle getAngle (pointsList) key_scan = cv2.waitKey (1) & 0xff # Keyboard scan if key_scan = = ord ("Q"): # Refresh picture pointsList when entering'Q' for every 3 times of mouse trigger interrupt ] img = cv2.imread (path) elif key_scan = = ord ("s"): # exit the program break cv2.destroyAllWindows () when you enter's'. These are all the contents of the article "how to achieve Angle Measurement by Python+OpenCV". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report