Draw a Circle Over an Image in Python

Highlight: Hi and welcome back to our Hacking OpenCV serial in Python. In this post we are going to explicate how to draw bones and more advanced shapes, as well as how to write text on images. Y'all volition learn to draw lines, rectangles, circles, ellipses, polylines and how to write a textual content on the image. This knowledge will be very useful afterward when we commencement to develop our facial recognition algorithms with OpenCV. For example, when we demand to describe a rectangle highlighting the detected faces in the processed image.

OpenCV provides a large number of functions to draw bones shapes in different colors. At present, let's see how to draw some lines, rectangles and circles.

Tutorial overview:

  1. How to depict lines, rectangles and circles?
  2. How to draw arrows, polygons and ellipses?
  3. How to write a text with OpenCV in Python?

1. How to describe lines, rectangles and circles?

How to draw lines?

The first function that we are going to check out is cv2.line(). This part draws a line on our image by connecting two coordinate points. Equally nosotros tin run into in the following example, in our lawmaking we need to define coordinates of these points and the thickness and color arguments.

Offset, we are going to create our image:

          

# Necessary imports import cv2 import numpy as np import matplotlib.pyplot as plt # Necessary import if we are working in Google Colab. Otherwise, # if will use cv2.imshow(), this import is non required. from google.colab.patches import cv2_imshow

Lawmaking language: PHP ( php )
          

# Creating our black image with coordinates (500,500) img = np.zeros((500, 500, 3), dtype="uint8") cv2_imshow(img)

Code linguistic communication: PHP ( php )

Output:

Now, we volition provide code for drawing multiple lines on the film with different coordinates, colors and widths.

          

# Changing the color of the image img[:] = (255,255,255) # Drawing a black line cv2.line(img, (0,0), (500,500), (0,0,0), (10)) # Drawing a bluish line cv2.line(img, (0,500), (500,0), (255,0,0), (twenty)) # Drawing a red line cv2.line(img, (0,250), (250,0), (0,0,255), (5)) # Drawing a light-green line cv2.line(img, (500,250), (0,250), (0,255,0), (xxx)) # Drawing a yellow line cv2.line(img, (500,250), (250,500), (0,200,200), (10)) # Cartoon a violet line cv2.line(img, (250,0), (500,250), (200,50,100), (10)) # Drawing a cyan line cv2.line(img, (0,250), (250,500), (255,255,0), (15)) # Drawing a orange line cv2.line(img, (250,0), (250,500), (0,100,255), (x)) cv2_imshow(img)

Code language: PHP ( php )

Output:

OpenCV, Python, line

How to describe rectangles?

The function that draws a rectangle is cv2.rectangle(). Our rectangle is defined with 2 points in two opposite corners. The first i is at the top left corner and the 2nd one is at the bottom right corner. Information technology is good to recollect that negative values for the thickness parameter, volition give united states of america a filled shape every bit our output.

          

# Cartoon a green rectangle cv2.rectangle(img, (100,100), (300,300), (0,255,0), (20)) # Drawing a blue rectangle cv2.rectangle(img, (200,100), (400,300), (255,0,0), (x)) # Drawing a red rectangle. Negative parameter -1 # indicates that we want to depict filled shape cv2.rectangle(img, (100,350), (400,400), (0,0,255), (-1)) # Drawing a yellow rectangle cv2.rectangle(img, (20,100), (fourscore,300), (0,200,200), (5)) # Cartoon a violet rectangle cv2.rectangle(img, (420,fifty), (450,450), (200,50,100), (-i)) cv2_imshow(img)

Code language: PHP ( php )

Output:

OpenCV, Python, rectangle

How to draw depict circles?

To draw a circle we apply a function cv2.circle(). This office draws a circle on the prototype and as parameters we need to ascertain the center and a radius of our circumvolve. Negative values for the thickness parameter, will give us a filled shape every bit our output.

          

# Drawing a red circle. Negative parameter -ane # indicates that we desire to draw filled shape cv2.circumvolve(img, (250,250), 150, (0,0,255), (-1)) # Drawing a blue circle cv2.circle(img, (70,70), 50, (255,0,0), (5)) # Drawing a green circle cv2.circumvolve(img, (430,430), 50, (0,255,0), (10)) cv2_imshow(img)

Code linguistic communication: PHP ( php )

Output:

OpenCV, Python, circle

2. How to describe arrows, polygons and ellipses?

In this affiliate, we are going to see how to depict some more than advanced shapes like arrows, ellipses, prune lines, and polygons.

How to draw an pointer?

For drawing arrows, nosotros are going to use the role cv2.arrowedLine() . This role is like to the previous 1 with the addition of a new argument that represents the tip length, as we can see in the following example.

          

# Creating our prototype img = np.zeros((500, 500, three), dtype="uint8") # Drawing a cherry-red arrow cv2.arrowedLine(img, (100,100), (400,100), (0,0,255), (10), 8,0,0.1) # Drawing a blue arrow cv2.arrowedLine(img, (100,200), (400,200), (255,0,0), (xx), 8,0,0.three) # Drawing a green arrow cv2.arrowedLine(img, (400,300), (100,300), (0,255,0), (v), eight,0,0.4) cv2_imshow(img)

Lawmaking language: PHP ( php )

Output:

OpenCV, Python, arrows

How to draw an ellipse?

Hither, we will utilize the office cv2.ellipse() which allows u.s. to draw an ellipse. Nosotros simply need to add a few arguments like axes, angle, start angle, and end angle. Parameter "angle" defines the bending of the ellipse relative to the coordinates. If an bending is 0 Parameters "startAngle" and "endAngle" define the elliptic arc. That ways that if "startAngle" is \(0^{\circ} \) and "endAngle" is \(360^{\circ} \), we will get a closed ellipse.

          

# Drawing a crimson ellipse. Negative parameter -one # indicates that we want to draw a filled shape. cv2.ellipse(img, (250,250), (90,50), 0, 0, 360, (0,0,255), (-i)) # Cartoon a blue ellipse. cv2.ellipse(img, (120,120), (100,seventy), 0,0,360, (255,0,0), (20)) # Drawing a green ellipse. cv2.ellipse(img, (400,300), (30,140),0,0,360, (0,255,0), (3)) # Drawing a yellow one. If we take same values for the axes # we can draw a circumvolve cv2.ellipse(img, (eighty,400), (60,60), 0, 0, 360, (0,200,200), (6)) # Drawing a white ellipse with an angle of 45 degrees cv2.ellipse(img, (250,380), (threescore,40), 45,0,360, (255,255,255), (10)) # Drawing a violet ellipse with an finish angle of 270 degrees cv2.ellipse(img, (300,80), (60,40),0,0,270, (200,50,100), (iii)) cv2_imshow(img)

Code language: PHP ( php )

Output:

OpenCV, Python, elipse

How to draw a prune line?

In some cases we want to draw a line painted with unlike colors for different parts of an image. In such a example it is convenient to use a line that is clipped against the rectangle. First thing that we need to practice is create our rectangle and line. Next, nosotros will use the office cv2.clipLine() . This function volition return the segment defined past the first and the second signal inside the rectangle. If both points are within the rectangle, function returns "True" and line volition change its colour. Nosotros can better understand this if we check out the lawmaking below.

          

# Drawing a line cv2.line(img, (0, 0), (500, 500), (255,0,0), three) # Drawing a rectangle cv2.rectangle(img, (0, 0), (150, 150), (0,0,255), 3) # Role cv2.clipLine clips the segment against the defined rectangle # We defining pt1 and pt2 (segment inside rectangle) # and starting and ending point of our line ret, p1, p2 = cv2.clipLine((0, 0, 150, 150), (0, 0), (500, 500)) if ret: cv2.line(img,p1,p2,(0,255,0),3)

Code linguistic communication: PHP ( php )
          

cv2_imshow(img)

Output:

OpenCV, Python, clip line

How to draw polygons?

Now nosotros will learn how to draw polygons. Hither we will use the role cv2.polylines() which allows us to create polygonal curves. The most important parameter that nosotros demand to create hither is an array of points which defines our polygonal curve. And then, the function cv2.polylines()will connect these points starting at the first point that we have defined. An important parameter here is isClosed. Information technology is a boolean data type which will optionally close the polygon. If we put "True", the function volition connect last dot to first. Otherwise the result will be an opened polygon.

          

# Creating a Numpy array of points pts = np.array([[fifty, 250], [150, 100], [250, 250],[100,400],[200,400]], np.int32) # Creating a yellowish polygon. Parameter "Fake" indicates # that our line is non closed cv2.polylines(img, [pts], Simulated, (0,200,200), 3) cv2_imshow(img)

Code linguistic communication: PHP ( php )

Output:

OpenCV, Python, polygon

At present lets create a few more polygons.

          

# Creating a Numpy array of points pts = np.array([[50, 250], [150, 100], [250, 250],[100,400],[200,400]], np.int32) # Creating a xanthous polygon. Parameter "Imitation" indicates # that our line is not airtight cv2.polylines(img, [pts], False, (0,200,200), iii) # Creating an array of points pts = np.assortment([[350, 200], [400, 150], [450, 200],[430,250],[370,250]], np.int32) # Creating a red pentagon cv2.polylines(img, [pts], True, (0,0,255), 5) # Creating an array of points pts = np.array([[350, 80], [400, 120], [450, 80]], np.int32) # Creating a blue triangle cv2.polylines(img, [pts], True, (255,0,0), 8) # Creating an array of points pts = np.array([[200,50], [300, 50], [300, 150],[200,150]], np.int32) # Creating a light-green rectangle cv2.polylines(img, [pts], True, (0,255,0), 10) # Creating an array of points pts = np.array([[300, 300], [400, 300], [300, 400],[400,400]], np.int32) # # Creating a violet polygon cv2.polylines(img, [pts], True, (200,50,100), 3) cv2_imshow(img)

Code language: PHP ( php )

Output:

OpenCV, Python, polygons

3. How to write text with OpenCV in Python?

Writing text on images is absolutely essential for image processing. So lets learn how to practice this.

First, we need to take several parameters. Second, we demand to define our font blazon. OpenCV uses several font types. Here, yous tin encounter all the available fonts in OpenCV.

  1. FONT_HERSHEY_SIMPLEX
  2. FONT_HERSHEY_PLAIN
  3. FONT_HERSHEY_DUPLEX
  4. FONT_HERSHEY_COMPLEX
  5. FONT_HERSHEY_TRIPLEX
  6. FONT_HERSHEY_COMPLEX_SMALL
  7. FONT_HERSHEY_SCRIPT_SIMPLEX
  8. FONT_HERSHEY_SCRIPT_COMPLEX

Once we pick our font type, we can move on. The role that we are going to apply is cv2.putText(). Afterwards adding some arguments like the starting point( provided text string stars at the upper-left corner), size, color and the line type of our text, nosotros are fix to become. Final provided parameter, is line type. There are three available line types in OpenCV (cv2.LINE_4, cv2.LINE_8, cv2.LINE_AA).

          

# Choosing our font font=cv2.FONT_ITALIC # Writing our text cv2.putText(img,"OpenCV with Python!", (90,250), font, 1, (255,255,255), 3, cv2.LINE_AA) cv2_imshow(img)

Lawmaking linguistic communication: PHP ( php )

Output:

OpenCV, Python, text

Now, allow's have a fiddling fun and try to recreate our Datahacker.rs logo.

          

# Creating our image img = np.zeros((400, 400, 3), dtype="uint8") img[:] = (255,255,255)

Code language: PHP ( php )
          

# Creating "Datahacker" logo cv2.rectangle(img, (76,76), (324,300), (0,0,0), (ii)) font=cv2.FONT_ITALIC font2=cv2.FONT_HERSHEY_PLAIN cv2.putText((img),"H A C K Due east R", (100,350), font, (1), (0,0,0), 2, cv2.LINE_AA) cv2.putText((img),"D", (120,162), font2, (5), (0,0,0), 5, cv2.LINE_AA) cv2.putText((img),"T", (120,264), font2, (v), (0,0,0), 5, cv2.LINE_AA) cv2.putText((img),"A", (240,164), font2, (five), (0,0,0), 5, cv2.LINE_AA) cv2.putText((img),"A", (240,264), font2, (v), (0,0,0), 5, cv2.LINE_AA) cv2_imshow(img)

Code language: PHP ( php )

Output:

If we want to create a GIF out of this logo, we volition use the following lawmaking:

          

# Creating the images img = np.zeros((400, 400, three), dtype="uint8") img[:] = (255,255,255) plt.imshow(img) cv2.rectangle(img, (76,76), (324,300), (0,0,0), (2)) font=cv2.FONT_ITALIC font2=cv2.FONT_HERSHEY_PLAIN cv2.putText((img),"D", (120,162), font2, (5), (0,0,0), 5, cv2.LINE_AA) plt.axis("off") # Ploting the image that we are created plt.imshow(img) # Saving an image that we are created # Dots per inch (dpi) is resolution that we chose plt.savefig("d_001.jpg", dpi = 800) cv2.putText((img),"A", (240,164), font2, (5), (0,0,0), five, cv2.LINE_AA) plt.axis("off") plt.imshow(img) plt.savefig("d_002.jpg", dpi = 800) cv2.putText((img),"T", (120,264), font2, (5), (0,0,0), v, cv2.LINE_AA) plt.axis("off") plt.imshow(img) plt.savefig("d_003.jpg", dpi = 800) cv2.putText((img),"A", (240,264), font2, (5), (0,0,0), v, cv2.LINE_AA) plt.axis("off") plt.imshow(img) plt.savefig("d_004.jpg", dpi = 800) cv2.putText((img),"H A C ", (100,350), font, (1), (0,0,0), 2, cv2.LINE_AA) plt.axis("off") plt.imshow(img) plt.savefig("d_005.jpg", dpi = 800) cv2.putText((img),"H A C K E R", (100,350), font, (i), (0,0,0), 2, cv2.LINE_AA) plt.centrality("off") plt.imshow(img) plt.savefig("d_006.jpg", dpi = 800)

Code linguistic communication: PHP ( php )
          

# This will load saved .jpg files and create a gif animation import bone import imageio png_dir = '../content' images = [] for file_name in sorted(os.listdir(png_dir)): print(file_name) if file_name.endswith('.jpg'): file_path = os.path.join(png_dir, file_name) images.suspend(imageio.imread(file_path)) imageio.mimsave('../content/movie1.gif', images, fps = 1)

Code linguistic communication: PHP ( php )

Output:

Summary

In this post, we learned how to depict shapes and write text with OpenCV in Python. Nosotros explained how to describe very basic shapes like lines, rectangles and circles and some more advanced shapes like ellipsis, arrows and polygons. In the adjacent post we are going to cover bones paradigm processing techniques like resizing, flipping, translating and rotating images with OpenCV in Python.

References:

[one] Cartoon and Writing on Image – OpenCV with Python for Epitome and Video Analysis 3

[2] Mastering OpenCV 4 with Python by Alberto Fernández Villán

harpergairly.blogspot.com

Source: https://datahacker.rs/draw-lines-rectangles-circles-write-text-on-images-with-opencv-in-python/

0 Response to "Draw a Circle Over an Image in Python"

Publicar un comentario

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel