Advertisement
skip420

DisplayCoordsImage

Mar 7th, 2022
1,086
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. #Displaying the coordinates of the points clicked on the image using Python-OpenCV
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9. # importing the module
  10. import cv2
  11.  
  12. # function to display the coordinates of
  13. # of the points clicked on the image
  14. def click_event(event, x, y, flags, params):
  15.  
  16.     # checking for left mouse clicks
  17.     if event == cv2.EVENT_LBUTTONDOWN:
  18.  
  19.         # displaying the coordinates
  20.         # on the Shell
  21.         print(x, ' ', y)
  22.  
  23.         # displaying the coordinates
  24.         # on the image window
  25.         font = cv2.FONT_HERSHEY_SIMPLEX
  26.         cv2.putText(img, str(x) + ',' +
  27.                     str(y), (x,y), font,
  28.                     1, (255, 0, 0), 2)
  29.         cv2.imshow('image', img)
  30.  
  31.     # checking for right mouse clicks  
  32.     if event==cv2.EVENT_RBUTTONDOWN:
  33.  
  34.         # displaying the coordinates
  35.         # on the Shell
  36.         print(x, ' ', y)
  37.  
  38.         # displaying the coordinates
  39.         # on the image window
  40.         font = cv2.FONT_HERSHEY_SIMPLEX
  41.         b = img[y, x, 0]
  42.         g = img[y, x, 1]
  43.         r = img[y, x, 2]
  44.         cv2.putText(img, str(b) + ',' +
  45.                     str(g) + ',' + str(r),
  46.                     (x,y), font, 1,
  47.                     (255, 255, 0), 2)
  48.         cv2.imshow('image', img)
  49.  
  50. # driver function
  51. if __name__=="__main__":
  52.  
  53.     # reading the image
  54.     img = cv2.imread('lena.jpg', 1)
  55.  
  56.     # displaying the image
  57.     cv2.imshow('image', img)
  58.  
  59.     # setting mouse handler for the image
  60.     # and calling the click_event() function
  61.     cv2.setMouseCallback('image', click_event)
  62.  
  63.     # wait for a key to be pressed to exit
  64.     cv2.waitKey(0)
  65.  
  66.     # close the window
  67.     cv2.destroyAllWindows()
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement