OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. In this article, we will try to draw on images with the help of the mouse. Before learning how to draw on images using a mouse, we need to understand what is a callback.<
A callback in programming means to call this function (the callback) when a process completes. The same applies to event-oriented programming in general. When a mouse button is clicked (an event), call a function. We don’t know when the button will be clicked. All we can do is tell the button to “call me back” or call this function when the mouse button is clicked.
A callback can happen when a user performs an operation using the mouse; this operation is usually known as an event. Only one callback is present for a mouse, which is setMouseCallback(), all mouse operation will call this function only.
We can have conditional blocks to execute something based on the event/operation performed using the mouse. The mouse events/operations could be:
When should this callback occur :
We want to have this call back only when we use the mouse on the pop-up window, which has the title as “Title of Popup Window.”
cv2.namedWindow("Title of Popup Window")
Example 1: Draw Circle when we left-click on a popup with OpenCV :
Example 2: Drawing a Rectangle by dragging on Images with OpenCV
What does (cv2.waitKey(10) & 0xFF == 27) do ?
cv2.waitKey() returns a 32 Bit integer value (might be dependent on the platform). The key input is in ASCII which is an 8 Bit integer value. So you only care about these 8 bits and want all other bits to be 0. This you can achieve with:
cv2.waitKey(10) & 0xFF == 27
- Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
- Wand Drawing() function in Python
- Python | Creating a Simple Drawing App in kivy
- Python | Drawing different shapes on PyGame window
- Python - Drawing design using arrow keys in PyGame
- pgmagick - Add text in images
- Loading Images in Tkinter using PIL
- Working with Images in Python
- Reading images in Python
- Reading Images With Python - Tkinter
- Python | Working with PNG Images using Matplotlib
- Apply changes to all the images in given folder - Using Python PIL
- Working with Images in Python using Matplotlib
- Rename a folder of images using Tkinter
- Extract images from video in Python
- Python | Display images with PyGame
- Python | Uploading images in Django
- Python | Grayscaling of Images using OpenCV
- How to download Google Images using Python
- Addition and Blending of images using OpenCV in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.