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.

Draw On Image Sample
Depict On Prototype Sample

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:

  1. 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.
  2. 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.
  3. 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()                
Original Blank Canvas
Original Blank Sail

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()                
Straight Line Canvas OpenCV
Straight Line Canvas OpenCV

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:

  1. Sheet on which rectangle is being drawn
  2. Top left cordinates of the rectangle
  3. Lesser right coordinates of the rectangle
  4. Mention the color of the rectangle in RGB tuple grade
  5. 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()                
Rectangle Canvas OpenCV
Rectangle Canvas OpenCV

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()                
Filled Rectangle Canvas OpenCV
Filled Rectangle Sail OpenCV

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:

  1. Sheet on which circle is being drawn
  2. Center of the circle that needs to be drawn
  3. The radius of the circle
  4. Mention the color of the circumvolve in RGB tuple grade
  5. 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()                
Circle Canvas OpenCV
Circle Canvas OpenCV

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()                
Filled Circle Canvas OpenCV
Filled Circle Canvas OpenCV

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()                
Final Output Draw On Canvas
Final Output Draw On Sheet

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:

  1. Matplotlib Subplots – Plot Multiple Graphs Using Matplotlib
  2. Matplotlib Plotting Tips
  3. 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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel