Opencv Python – How to display the coordinates of points clicked on an image?
When working on computer vision tasks, it is often useful to be able to select and label specific points or regions of an image. In the OpenCV Python library, this can be accomplished using the cv2.setMouseCallback() function. In this article, we will discuss how to use this function to display the coordinates of points clicked on an image.
Required Libraries
Before we start, we need to make sure we have all the required libraries installed on our system. We will be using the OpenCV and NumPy libraries in this tutorial. We can install these libraries using pip.
!pip install opencv-python
!pip install numpy
Loading Image
First, we need to load the image that we want to work with. We will be using the cv2.imread() function to load the image.
import cv2
import numpy as np
# Load the image
image = cv2.imread('image.jpg')
Defining the Click Event
The cv2.setMouseCallback() function allows us to define a custom click event. Here, we will define a function that will display the coordinates of the point clicked on the image.
# Define the click event
def click_event(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
# Display the coordinates
print("X:", x, " Y:", y)
In this function, we have used the cv2.EVENT_LBUTTONDOWN event, which is triggered when the left mouse button is clicked. We have also used the x and y coordinates of the mouse click to display the position of the point on the image.
Binding the Click Event
Now that we have defined our click event, we need to bind it to the image. We can do this using the cv2.setMouseCallback() function.
# Bind the click event to the image
cv2.namedWindow("image")
cv2.setMouseCallback("image", click_event)
Here, we have used the cv2.namedWindow() function to create a window to display our image. We have then used the cv2.setMouseCallback() function to bind our click event to this window.
Displaying the Image
Finally, we can display our image using the cv2.imshow() function.
# Display the image
cv2.imshow("image", image)
# Wait for the user to close the window
cv2.waitKey(0)
# Destroy the window
cv2.destroyAllWindows()
Here, we have used the cv2.imshow() function to display our image. We have then used the cv2.waitKey() function to wait for the user to close the window. Finally, we have used the cv2.destroyAllWindows() function to destroy the window.
Complete Code
Here is the complete code to display the coordinates of points clicked on an image.
import cv2
import numpy as np
# Define the click event
def click_event(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
# Display the coordinates
print("X:", x, " Y:", y)
# Load the image
image = cv2.imread('image.jpg')
# Bind the click event to the image
cv2.namedWindow("image")
cv2.setMouseCallback("image", click_event)
# Display the image
cv2.imshow("image", image)
# Wait for the user to close the window
cv2.waitKey(0)
# Destroy the window
cv2.destroyAllWindows()
Conclusion
In this tutorial, we have discussed how to use the cv2.setMouseCallback() function to display the coordinates of points clicked on an image using OpenCV in Python. By creating a custom click event and binding it to the image, we can easily select and label specific points or regions of an image.