Draw a line with cv2.line

Tutorial

You can draw lines on images with the cv2 Python package.

To do so, use the following code:


import cv2 

image = cv2.imread("image.jpeg")

# start and end points are (x, y)
start = (0, 0)
end = (250, 250)

# line colour is RGB
line_colour = (0, 255, 0)
thickness = 3

image = cv2.line(image, start, end, line_colour, thickness)

Above, replace:

  • start with the start position for the line.
  • end with the end position for the line.
  • line_colour with the colour you want to use for the line.
  • thickness  with the line thickness.

Next Steps

OpenCV can be used with the open source supervision Python package.

supervision provides an extensive range of functionalities for working with computer vision models. With supervision, you can:

1. Process and filter detections and segmentation masks from a range of popular models (YOLOv5, Ultralytics YOLOv8, MMDetection, and more).
2. Process and filter classifications.
3. Compute confusion matrices.

And more! To learn about the full range of functionality in supervision, check out the supervision documentation.