How to use Mouse Clicks to draw Circles in Python using OpenCV — Easy Project
In today’s very short blog we will see how we can use mouse clicks to draw circles in Python using OpenCV. This is going to be a fun application. So let’s tighten our belts and dive straight into it.
Read the full article with source code here — https://machinelearningprojects.net/draw-circles/
Let’s do it…
Step 1 — Import the necessary libraries to draw circles.
import numpy as np
Step 2 — Let’s create a black image.
black_image = np.zeros((512,512,3),np.uint8)
cv2.imshow(‘Black Image’, black_image)
NOTE — Read more about it here.
Step 3 — Create a draw circles function.
- event here refers to ay of the mouse operation.
- (x,y) are the coordinates of the point where we have clicked.
- Then we are checking if the event is equal to the left button click then draw a white colored circle centered at (x,y) with a radius of 40 pixels and filled with white color (because the thickness is passed as -1).
Step 4 — Run an infinite loop…
# draw circles
- In the first line, we have used cv2.setMouseCallback() to bind the draw circles() function with the ‘win’ window. It means that as soon as something is done using the mouse on the ‘win’ window, immediately go to the draw_circles function and run that function.
- After that, we are just showing our black image with white circles drawn on it.
- And at last, if the ESC key(ASCII 27) is pressed break the code.
Do let me know if there’s any query regarding how to draw circles by contacting me on email or LinkedIn.
So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time 👋…
For further code explanation and source code visit here — https://machinelearningprojects.net/draw-circles/
Read my previous post: HOW TO DENOISE AN IMAGE USING MEDIAN BLUR IN PYTHON USING OPENCV‘