Python OpenCV: Drawing circles

In this tutorial, we will check how to draw circles in an image with OpenCV and Python.



In this tutorial, we will check how to draw circles in an image with OpenCV and Python.

One common task when using OpenCV is detecting regions of interest with some computer algorithm vision. So, it makes sense for the programmer to be able to highlight those regions of interest in some way.

Thus, OpenCV offers a decent amount of drawing functions, which we can leverage to draw forms in images. So, as already mentioned, in this tutorial we will check how to draw circles in images.

One important thing to highlight is that when drawing in the image the coordinates referential works as shown in figure 1. The origin is at the top left corner of the image and we specify the x coordinate from left to right and the y coordinate from the top to the bottom. Also, the coordinates are specified in pixels.

OpenCV referential.png

Figure 1 – Referential when drawing in OpenCV.

This tutorial was tested on version 3.2.0 of OpenCV.


The code

As usual, we start by importing the cv2 module, so we have access to all the OpenCV functionalities we will need.

import cv2

Then, we need to read the image in which we want to draw some circles. To do it, we simple need to use the imread function, passing as input the path of the image in the file system.

image = cv2.imread('C:/Users/N/Desktop/Test.jpg') 

Now, to draw a circle on the image, we simply need to call the circle function of the cv2 module. Note that this function will not return a new image but rather draw the circle on the image we will pass as input.

But, as long as we don’t save this edited image, the circle will be drawn in the image we have in memory and not in the original file we have read, so these functions can be used safely.

As first input, the circle function receives the image on which we want to draw the circle. As second, it receives a tuple with the x and y coordinates of the center of the circle.

As third argument, we need to pass the radius of the circle and as fourth, we need to specify another tuple with the color of the circle, in BGR (Blue, Green and Red) format.

These are the mandatory parameters we need to pass in order to draw the circle, but there are some optional ones that assume default values if not specified.

So, we will first draw a green circle at coordinates x = 100 and y = 0, and with a radius of 25 pixels.,(100, 0), 25, (0,255,0))

Next, we will draw a circle at coordinates x= 0 and y = 100, also with a radius of 25 pixels. Its color will be red.,(0, 100), 25, (0,0,255)) 

Note that in the previous calls, we did not specified what will be the thickness of the circle outline. But this value is actually one of the optional parameters we can pass to the circle function, which defaults to 1 pixel. So, the previous two circles will have a thickness of 1 pixel.

Just to illustrate the use of this parameter, we will now draw a circle at coordinates x = 100 and y = 100, a radius of 50 and with a blue color. Additionally, we will pass a fifth parameter, which will correspond to the thickness of the circle outline, with a value of 3 pixels.,(100, 100), 50, (255,0,0), 3)

Finally, we will display the image with the drawn circles and wait for a key event. When that event happens, we will destroy the window where the image was being displayed and finish the execution. The final source code is shown below and already includes these last calls.

import cv2

image = cv2.imread('C:/Users/N/Desktop/Test.jpg'),(100, 0), 25, (0,255,0)),(0, 100), 25, (0,0,255)),(100, 100), 50, (255,0,0), 3)

cv2.imshow('Test image',image)

Testing the code

To test the code, simply run the previous program in your Python environment of choice, point to an existing image in your file system. You should get an output similar to figure 2.

Python OpenCV draw circle in image.png

Figure 2 – Drawing circles in an image with OpenCV.

As can be seen, all the three circles were drawn in the specified coordinates, with the corresponding radius, colors and thicknesses.

Note that for the first two circles we specified the top and left halves, respectively, would be drawn outside the image borders, which is why the circles are cut.

2 thoughts on “Python OpenCV: Drawing circles”

  1. how to crop the portion of circle drawn i.e, ROI and how to use that cropped image for further use and i have used the above code for circle detection

Leave a Reply