Today we will learn about Mouse Events in OpenCV Python. We will be creating a black background and on the left mouse click print X and Y Coordinates and on the right mouse click print RGB Value of that pixel.

Let’s Code Mouse Events in OpenCV!

Mouse Events OpenCV Algorithm

Import Packages

 import numpy as np 

NumPy for manipulation in Color matrix.

import cv2 as cv

Computer Vision for reading and showing images and image manipulation.

from matplotlib import pyplot as plt

Matplotlib for data visualization. (Optional)

Read and Show Image for Mouse Events OpenCV

img = cv.imread(‘./img/sea.jpg’)

cv.imread() function is an OpenCV function in Python that makes it easy for the computer to read images. It takes one argument i.e the image source in the form of Absolute or Relative Path.

cv.imshow(‘image’, img)

The image is shown in the window named ‘image‘.

Source | Divyanshu Shekhar

Set Mouse Callback in OpenCV

Next Step is to setMouseCallback() function. It takes two parameters:

  1. The Window name on which the image is being shown
  2. A Callback function that will process the image and will return output on some mouse events.

cv.setMouseCallback(‘image’, click_event)

Function for Mouse Click Event in OpenCV

The Callback function takes four parameters:

  1. The Mouse Event
  2. X coordinate where the mouse event happens
  3. Y coordinate where the mouse event happens
  4. flags
  5. params

Our AIM: Left Click to get the x, y coordinates. Right, Click to get the BGR/RGB color scheme at that position.

Firstly we have to declare some variables for text, text font, and text color. You Can choose it according to your choice.

The setMouseCallback() function sends the mouse event with it to the callback function.

If the mouse event in the click_event() function matches the Left Button Down then X and Y Coordinates are converted to string values.

def click_event(event, x, y, flags, params):
    Left Click to get the x, y coordinates.
    Right Click to get BGR color scheme at that position.
    text = ''
    color = (255, 0, 0)
    if event == cv.EVENT_LBUTTONDOWN:
        print(x, ",", y)
        text = str(x) + "," + str(y)
        color = (0, 255, 0)
    elif event == cv.EVENT_RBUTTONDOWN:
        b = img[y, x, 0]
        g = img[y, x, 1]
        r = img[y, x, 2]
        text = str(b) + ',' + str(g) + ',' + str(r)
        color = (0, 0, 255)
    cv.putText(img, text, (x, y), font, 0.5, color, 1, cv.LINE_AA)
    cv.imshow('image', img)

If the mouse event in the click_event() function matches the Right Button-Down then the BGR (Blue, Green, and Red) value of that pixel is recorded and converted to the string value.

After the condition check is done, the text is put on the image at the X and Y coordinate where the mouse events occurred.

After putting text on the image that the image is shown on the ‘image’ window and the mouse click event is shown in the form of text on the image.

Mouse Events GitHub

Full Source code of Mouse Events OpenCV at GitHub.

About Author
0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Scroll to Top