程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python mouse clicks the picture in real time to output the coordinates of pixel points with OpenCV

編輯:Python

python Click the mouse to output the coordinates of pixels in real time With the help of opencv


Click on the image , Real time output of pixel coordinates at the terminal

import cv2
img = cv2.imread("./edge1.png") # Read the picture 
a = []
b = []
def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
a.append(x)
b.append(y)
cv2.circle(img, (x, y), 3, (255, 0, 0), thickness=-1)
cv2.imshow("image", img)
print("[{},{}]".format(a[-1], b[-1])) # Output the coordinates of the last click 
cv2.namedWindow("image")
cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN)
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

design sketch 1 as follows :


There is still room for revision , I modified it in passing
Make it directly mark the coordinates in the picture
The code is as follows :

import cv2
img = cv2.imread("./edge1.png") # Path to picture 
a = []
b = []
def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
xy = "[%d,%d]" % (x, y)
a.append(x)
b.append(y)
cv2.circle(img, (x, y), 3, (255, 0, 0), thickness=-1)
cv2.putText(img, xy, (x, y), cv2.FONT_HERSHEY_PLAIN,
1.0, (107, 73, 251), thickness=1) # If you do not want to display the coordinates on the picture, you can annotate the line 
cv2.imshow("image", img)
print("[{},{}]".format(a[-1], b[-1])) # Output coordinates in the terminal 
cv2.namedWindow("image")
cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN)
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

design sketch 2 as follows :

Reference resources here.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved