Draw a Circle Using the Opencv on an Image
Hey in that location! Have you ever wished to describe on the matplotlib plots that y'all plot every other day? Well, the wish gets fulfilled in this tutorial correct here! Today, we will be learning how to draw various objects on the plots.
Allow'south Brainstorm!
Also read: Alive Sketch Using Webcam with Python OpenCV [Like shooting fish in a barrel Guide]
Introduction to OpenCV
Firstly, OpenCV comes with many drawing functions to draw geometric shapes and even write text on images.
Before anything else, let'south beginning past introducing the drawing functions that we are going to utilize in the tutorial correct here.
The functions are listed below:
-
cv2.line
: This function is used to draw a straight line on the image which starts at a specified (x, y) pair of coordinates and finish at another (10, y) pair of coordinates. -
cv2.circle
: This function is used to describe a circle on an epitome specified by the centre given past (x, y) co-ordinates and also the length of radius. -
cv2.rectangle
: This function is helpful to depict a rectangle on an image specified by the top-left corner and lesser-right corner in form of (ten, y)-coordinates.
Drawing Shapes in Python with OpenCV
Now that we are clear with what magic is going to happen past terminate of this tutorial, permit's piece of work on our magic!
Step 1: Import Modules/Libraries
In this step, we need to import all the necessary modules and libraries needed for drawing on the images using OpenCV. The obvious module is cv2
and along with this, we have 2 supporting modules, i.due east. numpy
and matoplotlib
modules.
Finally, we are going to change the plot style to seaborn
to go cleaner plots.
import cv2 import numpy every bit np from matplotlib import pyplot as plt plt.manner.use('seaborn')
Stride 2: Creating a Black Canvas to Draw on
In order to depict anything, nosotros need a canvas to describe on. In this case, we volition utilise an paradigm of size 512 ten 512
filled with a single solid color (black in this case).
In order to achieve this, We brand use of the numpy. zeroes and set all pixel values to goose egg in society to have them all black in colour.
canvas = np.zeros((512,512,three), np.uint8) plt.axis('off') plt.championship("Bare Sail") plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB)) plt.bear witness()
Step 3: Drawing a line on the Canvas
In order to draw a line, we will exist using cv2.line
part which requires a number of backdrop which include the proper noun of the canvass object created, starting and catastrophe coordinates of the straight line, the color of the line using the RGB tuples.
Accept a look at the code mentioned below to get a diagonal green line on your canvas.
canvas = np.zeros((512,512,iii), np.uint8) cv2.line(canvass, (0,0), (511,511), (0,255,0),50) plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB)) plt.championship('Greenish Straight Linear line') plt.centrality('off') plt.show()
Step iv: Drawing a Rectangle on the Canvas
In order to draw the rectangle, nosotros make use of the cv2.rectangle
method. The method is identical to the cv2.line
method and takes the following properties of the rectangle:
- Sheet on which rectangle is being drawn
- Top left cordinates of the rectangle
- Lesser right coordinates of the rectangle
- Mention the color of the rectangle in RGB tuple grade
- The last argument is the thickness of the border of the rectangle
The code and output for the aforementioned is shown below.
canvass = np.zeros((512,512,3), np.uint8) cv2.rectangle(canvas, (100,100), (300,250), (255,0,0), 20) plt.imshow(cv2.cvtColor(canvass, cv2.COLOR_BGR2RGB)) plt.title('Blue Rectangle on the Canvas') plt.axis('off') plt.testify()
At present, what if nosotros desire a completely filled rectangle. In guild to reach that we make the thickness negative or using cv2.FILLED
keyword.
canvas = np.zeros((512,512,3), np.uint8) cv2.rectangle(sheet, (100,100), (300,250), (0,0,255), -1) plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB)) plt.title('Red Filled Rectangle on the Canvas') plt.axis('off') plt.evidence()
Step v: Drawing a Circle on the Canvas
In guild to draw a circumvolve, we make use of the cv2.circle
method. The method needs the post-obit properties:
- Sheet on which circle is being drawn
- Center of the circle that needs to be drawn
- The radius of the circle
- Mention the color of the circumvolve in RGB tuple grade
- The last argument is the thickness of the border of the circle
The lawmaking and output for the same are shown below.
canvas = np.zeros((512,512,3), np.uint8) cv2.circumvolve(canvas, (350, 350), 100, (15,75,50), 20) plt.imshow(cv2.cvtColor(canvass, cv2.COLOR_BGR2RGB)) plt.title('Olive Color Circumvolve Fatigued') plt.axis('off') plt.show()
Drawing a filled circle is like to cartoon a filled rectangle on the canvas. Look at the code and output below.
sail = np.zeros((512,512,iii), np.uint8) cv2.circle(canvas, (350, 350), 100, (155,175,250), -1) plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB)) plt.title('Peach Colour Filled Circumvolve Drawn') plt.axis('off') plt.prove()
Consummate Lawmaking to Draw Shapes in Python Using OpenCV
At present that we take learned to draw basic shapes on the canvas. Let's visualize all the plots with the assistance of subplots using the code mentioned below.
import cv2 import numpy as np from matplotlib import pyplot as plt plt.style.apply('seaborn') plt.figure(figsize=(10,ten)) canvas = np.zeros((512,512,3), np.uint8) plt.subplot(3,3,i) plt.axis('off') plt.title("Blank Canvas") plt.imshow(cv2.cvtColor(sail, cv2.COLOR_BGR2RGB)) plt.subplot(iii,3,2) canvass = np.zeros((512,512,three), np.uint8) cv2.line(canvas, (0,0), (511,511), (0,255,0),50) plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB)) plt.title('Dark-green Straight Linear line') plt.axis('off') plt.subplot(iii,three,iii) canvas = np.zeros((512,512,3), np.uint8) cv2.rectangle(canvas, (100,100), (300,250), (255,0,0), xx) plt.imshow(cv2.cvtColor(canvass, cv2.COLOR_BGR2RGB)) plt.title('Blue Rectangle on the Canvas') plt.centrality('off') plt.subplot(three,3,four) canvas = np.zeros((512,512,3), np.uint8) cv2.rectangle(canvass, (100,100), (300,250), (0,0,255), -1) plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB)) plt.title('Red Filled Rectangle on the Canvas') plt.axis('off') plt.subplot(3,3,5) sheet = np.zeros((512,512,3), np.uint8) cv2.circumvolve(sail, (350, 350), 100, (xv,75,50), twenty) plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB)) plt.title('Olive Color Circumvolve Fatigued') plt.axis('off') plt.subplot(3,3,6) canvas = np.zeros((512,512,3), np.uint8) cv2.circle(canvas, (350, 350), 100, (155,175,250), -i) plt.imshow(cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB)) plt.title('Peach Color Filled Circle Drawn') plt.axis('off') plt.tight_layout() plt.show()
Conclusion
I hope you understood the concept and loved the outputs. Endeavor making scenery or a cartoon character using the same basic shapes and go amazed with the results.
Happy Coding! 😇
Want to learn more? Cheque out the tutorials mentioned beneath:
- Matplotlib Subplots – Plot Multiple Graphs Using Matplotlib
- Matplotlib Plotting Tips
- Pygame: Creating Interactive Shapes
bowieseliestionce.blogspot.com
Source: https://www.askpython.com/python/examples/draw-shapes-using-opencv
0 Response to "Draw a Circle Using the Opencv on an Image"
Post a Comment