In this post we are going to learn how to draw rectangles on an image, using Python and OpenCV.
OpenCV offers us a couple of functions to draw shapes on images, as can be seen here. These may be interesting to highlight regions of interest. For example, if we are applying an algorithm to detect faces, a simple way to signal them can be drawing a rectangle around them.
This tutorial was tested on Windows 8.1, with version 4.1.2 of OpenCV. The Python version used was 3.7.2.
Drawing the Rectangles
The first line of our code will be for importing the cv2 module, so we have access to the function we need to draw rectangles on images.
Next, we will take care of reading an image from the file system. We do so with a call to the imread function. As input we pass the path to the image and, as output, the function returns the image as a ndarray. When testing the code, don’t forget to update the path to an image in your file system.
img = cv2.imread('C:/Users/N/Desktop/monument.png')
Now that we have our image in memory, we will draw some rectangles on it. To draw a rectangle with OpenCV, we simply need to use the rectangle function. This function receives the following arguments:
- The image where to draw the rectangle. We will pass the image we just read;
- A tuple with the x and y coordinates of one of the vertices of the rectangle. To simplify the drawing of the shape, it is easier to always think about this vertex as the top left corner of the rectangle;
- A tuple with the x and y coordinates of the opposite vertex of the rectangle, regarding the previous one. Also to simplify, we can consider this the bottom right corner of the rectangle;
- A tuple with the BGR color of the rectangle;
- Thickness of the lines of the rectangle. It is an optional parameter that, when not specified, defaults to 1. Also, negative numbers will result in a filled rectangle being drawn.
It’s important to consider that the rectangle function alters the image we pass as input. This means that, if we want to keep the original image unaltered, we should do a copy of it and draw the rectangle on that copy. To learn how to perform a copy of an image in memory with OpenCV, please consult this post.
Now that we have analyzed all the arguments of the rectangle function of the cv2 module, we will draw the first shape. We will draw it between (x=10, y=10) and (x=100, y=100), in green color. Also, to illustrate that the thickness is optional, we won’t set this value.
cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0))
We will draw a second rectangle, this time between (x=120, y=120) and (x=150, y=150). The color will be blue this time and we will set the thickness of the lines of the rectangle to 5 pixels.
cv2.rectangle(img, (120, 120), (150, 150), (255, 0, 0), 5)
Our final rectangle will be between the coordinates (x=200, y=200) and (x=300, y=400) and drawn in red. We will also set the thickness to a negative number, so it is drawn as a filled shape.
cv2.rectangle(img, (200, 200), (300, 400), (0, 0, 255), -1)
Important: The coordinates I’ve used to draw the previous three rectangles are adequate for the size of the image I’m using. Depending on the image you will be using for your tests, you may need to use different coordinates for the rectangles to fit inside.
Now that we have finished drawing over our image, we will display it in an OpenCV window. This is done with a call to the imshow function, passing as first input the name of the window and as second our image.
To finalize, we will wait for the user to press any key on the keyboard and, when that happens, destroy the window and end the program.
The complete Python code can be seen below.
import cv2 img = cv2.imread('C:/Users/N/Desktop/monument.png') cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0)) cv2.rectangle(img, (120, 120), (150, 150), (255, 0, 0), 5) cv2.rectangle(img, (200, 200), (300, 400), (0, 0, 255), -1) cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows()
Testing the code
To test the previous Python script, simply run it in a tool of your choice. I’ll be using PyCharm, a Python IDE.
Upon running the code, you should get a result similar to the one illustrated in figure 1. As can be seen, we got three rectangles in the image. The first one is drawn in green, with a width of 1 pixel. The second one is drawn in blue and with thicker lines. The third one is drawn in red and it is a filled rectangle. The three are drawn between the coordinates we specified in our code, as expected.