Life2Coding
Draw a Circle on Image using Python OpenCV

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply them in various aspects using some great examples. Then the output will be visualized along with the comparisons.

We will also discuss the basic of image processing and provide the detail explanation related to the OpenCV functions.

Requirements:

First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV with Python 3

Goals:

The goal is to make you understand how to draw a circle on image using Python OpenCV. We’re going to discuss how to draw opencv shape on images.

Documentation:

circle()

img=cv.circle(img, center, radius, color[, thickness[, lineType[, shift]]])

Draws a circle.

Parameters
img Image where the circle is drawn.
center Center of the circle.
radius Radius of the circle.
color Circle color.
thickness Thickness of the circle outline, if positive. Negative values, like FILLED, mean that a filled circle is to be drawn.
lineType Type of the circle boundary. See LineTypes
shift Number of fractional bits in the coordinates of the center and in the radius value.

imshow()

None=cv.imshow(winname, mat)

Displays an image in the specified window.

Parameters
winname Name of the window.
mat Image to be shown.

waitKey()

retval=cv.waitKey([, delay])

Waits for a pressed key.

Parameters
delay Delay in milliseconds. 0 is the special value that means “forever”.

destroyAllWindows()

None=cv.destroyAllWindows()

Destroys all of the HighGUI windows.

Steps:

  • First we will create a image array using np.zeros()
  • After that we will create a circle using cv2.circle()
  • Then display the image using cv2.imshow()
  • Wait for keyboard button press using cv2.waitKey()
  • Exit window and destroy all windows using cv2.destroyAllWindows()

Example Code:

import numpy as np
import cv2
#create a 512x512 black image
img=np.zeros((512,512,3),np.uint8)

#non filled circle
img1 = cv2.circle(img,(256,256),63, (0,255,0), 8)
#filled circle
img1 = cv2.circle(img,(256,256),63, (0,0,255), -1)
#now use a frame to show it just as displaying a image
cv2.imshow("Circle",img1)
cv2.waitKey(0)
cv2.destroyAllWindows()


Output:

5cb403f1b01a7 Draw a Circle on Image using Python OpenCV
life2coding_icon [] Draw a Circle on Image using Python OpenCV

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.