Displaying the coordinates of the points clicked on the image using PythonOpenCV (original) (raw)

Last Updated : 03 Jan, 2023

OpenCV helps us to control and manage different types of mouse events and gives us the flexibility to operate them. There are many types of mouse events. These events can be displayed by running the following code segment :

import cv2 [print(i) for i in dir(cv2) if 'EVENT' in i]

Output :

EVENT_FLAG_ALTKEY EVENT_FLAG_CTRLKEY EVENT_FLAG_LBUTTON EVENT_FLAG_MBUTTON EVENT_FLAG_RBUTTON EVENT_FLAG_SHIFTKEY EVENT_LBUTTONDBLCLK EVENT_LBUTTONDOWN EVENT_LBUTTONUP EVENT_MBUTTONDBLCLK EVENT_MBUTTONDOWN EVENT_MBUTTONUP EVENT_MOUSEHWHEEL EVENT_MOUSEMOVE EVENT_MOUSEWHEEL EVENT_RBUTTONDBLCLK EVENT_RBUTTONDOWN EVENT_RBUTTONUP

Now let us see how to display the coordinates of the points clicked on the image. We will be displaying both the points clicked by right-click as well as left-click.

Algorithm :

  1. Import the cv2 module.
  2. Import the image using the cv2.imread() function.
  3. Display the image the image using the cv2.imshow() function.
  4. Call the cv2.setMouseCallback() function and pass the image window and the user-defined function as parameters.
  5. In the user-defined function, check for left mouse clicks using the cv2.EVENT_LBUTTONDOWN attribute.
  6. Display the coordinates on the Shell.
  7. Display the coordinates on the created window.
  8. Do the same for right mouse clicks using the cv2.EVENT_RBUTTONDOWN attribute. Change the color while displaying the coordinates on the image to distinguish from left clicks.
  9. Outside the user-defined function, use the cv2.waitKey(0) and the cv2.destroyAllWindows() functions to close the window and terminate the program.

We will be using the colored version of the Lena image.

Python3

import cv2

def click_event(event, x, y, flags, params):

`` if event = = cv2.EVENT_LBUTTONDOWN:

`` print (x, ' ' , y)

`` font = cv2.FONT_HERSHEY_SIMPLEX

`` cv2.putText(img, str (x) + ',' +

`` str (y), (x,y), font,

`` 1 , ( 255 , 0 , 0 ), 2 )

`` cv2.imshow( 'image' , img)

`` if event = = cv2.EVENT_RBUTTONDOWN:

`` print (x, ' ' , y)

`` font = cv2.FONT_HERSHEY_SIMPLEX

`` b = img[y, x, 0 ]

`` g = img[y, x, 1 ]

`` r = img[y, x, 2 ]

`` cv2.putText(img, str (b) + ',' +

`` str (g) + ',' + str (r),

`` (x,y), font, 1 ,

`` ( 255 , 255 , 0 ), 2 )

`` cv2.imshow( 'image' , img)

if __name__ = = "__main__" :

`` img = cv2.imread( 'lena.jpg' , 1 )

`` cv2.imshow( 'image' , img)

`` cv2.setMouseCallback( 'image' , click_event)

`` cv2.waitKey( 0 )

`` cv2.destroyAllWindows()

Output :

https://media.geeksforgeeks.org/wp-content/uploads/20200714083457/cv2-clicking.mp4

Similar Reads

Projects for Beginners
























Projects for Intermediate


















Web Scraping












Automating boring Stuff Using Python

















Tkinter Projects



































Turtle Projects












OpenCV Projects

































Python Django Projects














Python Text to Speech and Vice-Versa