Capturing mouse click events with Python and OpenCV

detect mouse click events with Python and OpenCV

This article is mouse click event OpenCV tutorial, we will use python to get coordinates of mouse click on image. Then using those coordinates we will draw rectangle on image with mouse OpenCV. After that, we will crop that area of interest from that image. In this way, we will check the mouse click event of the image and use this mouse click event OpenCV.

In this example, we will click and draw rectangle on image with mouse for a Region of Interest (ROI) and crop it from our image. Before reading this I will highly recommend you to read below articles:

Must Read:

Create blank matrix to store coordinates of mouse click on image

Let’s first create a blank matrix (2,2) to store coordinates of mouse click on image. Now by meaning coordinates I am trying to say pixel value or position.

# Create point matrix get coordinates of mouse click on image
point_matrix = np.zeros((2,2),np.int)

Store coordinates of mouse click in matrix

Now let’s write a function to store pixel values where we will do left mouse click.

counter = 0
def mousePoints(event,x,y,flags,params):
    global counter
    # Left button click
    if event == cv2.EVENT_LBUTTONDOWN:
        point_matrix[counter] = x,y
        counter = counter + 1

In line 11 we are checking if any left click (cv2.EVENT_LBUTTONDOWN) is happened or not. In line 12 we are storing pixel value of each point where we have done mouse click on image.

Draw circle on image with mouse click

Once we detect mouse click on image, let’s draw circle on that point.

    for x in range (0,2):
        cv2.circle(img,(point_matrix[x][0],point_matrix[x][1]),3,(0,255,0),cv2.FILLED)

Python draw rectangle on image with mouse click

Once we clicked two points on image, based on starting and ending pixel values we will draw rectangle on image for the area of interest.

opencv draw rectangle on image with mouse
Draw rectangle on image with mouse in python opencv
    if counter == 2:
        starting_x = point_matrix[0][0]
        starting_y = point_matrix[0][1]

        ending_x = point_matrix[1][0]
        ending_y = point_matrix[1][1]
        # Draw rectangle for area of interest
        cv2.rectangle(img, (starting_x, starting_y), (ending_x, ending_y), (0, 255, 0), 3)

Crop region of interest

So now we know all points, lets crop that Region of Interest (ROI) from our image.

detect mouse click events with Python and OpenCV

Note: You first need to click on upper portion of the ROI, we can resolve this problem by writing complex program, but our intention was to explain how to get coordinates of an image in opencv python, so not writing that complex code.

        # Cropping image
        img_cropped = img[starting_y:ending_y, starting_x:ending_x]
        cv2.imshow("ROI", img_cropped)

Full code

Full code to detect mouse click event OpenCV

Also Read:  Emotion Recognition from Facial Expressions in Python

Note: You first need to click on upper portion of the ROI, as shown in video.

In the same way frame by frame you can crop or draw rectangle on video.

import cv2
import numpy as np

# Create point matrix get coordinates of mouse click on image
point_matrix = np.zeros((2,2),np.int)

counter = 0
def mousePoints(event,x,y,flags,params):
    global counter
    # Left button mouse click event opencv
    if event == cv2.EVENT_LBUTTONDOWN:
        point_matrix[counter] = x,y
        counter = counter + 1

# Read image
img = cv2.imread('data/Cristiano_Ronaldo_256.jpg')

while True:
    for x in range (0,2):
        cv2.circle(img,(point_matrix[x][0],point_matrix[x][1]),3,(0,255,0),cv2.FILLED)

    if counter == 2:
        starting_x = point_matrix[0][0]
        starting_y = point_matrix[0][1]

        ending_x = point_matrix[1][0]
        ending_y = point_matrix[1][1]
        # Draw rectangle for area of interest
        cv2.rectangle(img, (starting_x, starting_y), (ending_x, ending_y), (0, 255, 0), 3)

        # Cropping image
        img_cropped = img[starting_y:ending_y, starting_x:ending_x]
        cv2.imshow("ROI", img_cropped)

    # Showing original image
    cv2.imshow("Original Image ", img)
    # Mouse click event on original image
    cv2.setMouseCallback("Original Image ", mousePoints)
    # Printing updated point matrix
    print(point_matrix)
    # Refreshing window all time
    cv2.waitKey(1)

If you have any question or suggestion regarding this topic see you in comment section. I will try my best to answer.

2 thoughts on “Capturing mouse click events with Python and OpenCV”

Leave a comment