Mouse Events OpenCV Python

Mouse Events opencv python

Today we will learn about Mouse Events in OpenCV Python. We will be creating a black background and on left mouse click print X and Y Co-ordinates and on 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‘.

mouse events opencv
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 = ''
    font = cv.FONT_HERSHEY_COMPLEX
    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 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.

OpenCV Projects

Lanes Detection

Self Driving Cars are the latest technology and it uses lane detection for autonomous cars. Learn How you can create a lane detection program using OpenCV Python.

Motion Detection

CCTV Cameras use Motion Detection to detect any kind of motion in the video frame. This helps in the security of the home and to detect any kind of small movement in the image from the previous image received in the form of the video source.

Live Sketch

This OpenCV live sketch program/app will create a real-time sketch of a person/object. It uses image manipulation techniques to Detect Edges in the video frame and image thresholding for making it look like a sketch.

OpenCV Documentation

Check the OpenCV Official Documentation for more information about the functions used.

How to set Mouse Callback in OpenCV Python?

setMouseCallback() function in OpenCV is used to bind mouse events with windows.
Example:
cv.setMouseCallback(‘image’, click_event)

Reading images in OpenCV Python

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.

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

Divyanshu Shekhar

Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *