Python OpenCV: Drawing lines on image

In this tutorial we are going to learn how to draw lines in an image, using Python and OpenCV.

Introduction

In this tutorial we are going to learn how to draw lines in an image, using Python and OpenCV.

Being able to draw lines on an image might be useful to mark, for example, regions of interest on an image.

This tutorial was tested with version 4.0.0 of OpenCV and version 3.7.2 of Python.

The code

We will start our code by importing the cv2 module.

import cv2

After that we are going to read an image, so we can later draw some lines on it. We can read the image with a call to the imread function, passing as input a string with the path of the image file in the file system.

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

Next we are going obtain both the height and the width of the image, since we are going to use these values to draw one of the lines. You can check here a more detailed tutorial on how to obtain the dimensions of an image.

In short, we can obtain these dimensions by accessing the shape attribute of the image.

height = image.shape[0]
width = image.shape[1]

Then, to draw a line, we need to use the line function of the cv2 module. This function receives as input the following parameters:

  • image: the image on which we want to draw the line.
  • point 1: first point of the line segment. This is specified as a tuple with the x and y coordinates.
  • point 2: second point of the line segment. This is specified as a tuple with the x and y coordinates.
  • color: color of the line. This is specified as a tuple with the 3 colors in the BGR format (assuming that we are working with colored images).
  • thickness: thickness of the line, in pixels.

Note that this function supports other parameters that, when not specified, have default values. Nonetheless, we are not going to make use of them on this tutorial.

We will draw a first line with a blue color (B=255, G=0, R=0) between points (x=20, y=10) and (x=100, y=10) and with a thickness of 2 pixels.

cv2.line(image, (20,10), (100,10), (255,0,0), 2)

Additionally, we are also going to draw a diagonal line from the top left corner to the bottom right corner. In terms of coordinates, it means the line will start on point (x=0, y=0) and end on point (x=width, y=height). We are going to draw this line in red (B=0, G=0, R=255) and with a thickness of 12 pixels.

cv2.line(image, (0,0), (width, height), (0,0,255), 12)

To finalize, we are going to display the image with the drawn lines.

cv2.imshow("Image", image)
   
cv2.waitKey(0)
cv2.destroyAllWindows()

The final code can be seen below.

import cv2

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

height = image.shape[0]
width = image.shape[1]

cv2.line(image, (20,10), (100,10), (255,0,0), 2)
cv2.line(image, (0,0), (width, height), (0,0,255), 12)

cv2.imshow("Image", image)
   
cv2.waitKey(0)
cv2.destroyAllWindows()

Testing the code

To test the code, simply run it using a tool of your choice. In my case I’ve used IDLE, a Python IDE.

The image used for testing is the one from figure 1.

Original image used in the test.
Figure 1 – Original image used in the test.

After running the code, you should get a result similar to figure 2. Naturally you can use any image of your choice.

Output of the program, with the lines drawn.
Figure 2 – Output of the program, with the lines drawn.

Leave a Reply