DEV Community

Divyanshu Shekhar
Divyanshu Shekhar

Posted on • Updated on

Mouse Events OpenCV Python

Mouse Events OpenCV Algorithm

Import Packages

 import numpy as np 
Enter fullscreen mode Exit fullscreen mode

NumPy for manipulation in Color matrix.

import cv2 as cv
Enter fullscreen mode Exit fullscreen mode

Computer Vision for reading and showing images and image manipulation.

from matplotlib import pyplot as plt
Enter fullscreen mode Exit fullscreen mode

Matplotlib for data visualization. (Optional)

Read and Show Image for Mouse Events OpenCV

img = cv.imread(‘./img/sea.jpg’)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Set Mouse Callback in OpenCV

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

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

cv.setMouseCallback(‘image’, click_event)
Enter fullscreen mode Exit fullscreen mode

Read the whole post Mouse Events OpenCV python from the original Post.

Top comments (0)