OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Drawing a line on an image using OpenCV is a straightforward process. In this tutorial, I'll guide you through the steps to draw a line on an image using the line()
function provided by OpenCV.
Ensure you have OpenCV installed:
pip install opencv-python
Import necessary libraries:
import cv2 import numpy as np
For the purpose of this tutorial, let's create a blank image. You can also load an existing image if you prefer:
# Create a blank image (white background) image = np.ones((500, 500, 3), np.uint8) * 255
Or load an existing image:
# image = cv2.imread('path_to_image.jpg')
Use the cv2.line()
function:
cv2.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
Where:
img
: The image on which you want to draw the line.pt1
: Starting point of the line.pt2
: Ending point of the line.color
: Color of the line in (B, G, R) format.thickness
(optional): Thickness of the line (default is 1).lineType
(optional): Type of the line, e.g., 8-connected, anti-aliased line etc.shift
(optional): Number of fractional bits in the point coordinates.Here's an example:
start_point = (100, 100) end_point = (400, 400) color = (0, 0, 255) # Red color in BGR format thickness = 2 cv2.line(image, start_point, end_point, color, thickness)
cv2.imshow('Line', image) cv2.waitKey(0) cv2.destroyAllWindows()
When you run the code, you'll see an image with a red line drawn from the point (100, 100) to the point (400, 400).
Combining all the steps, here's the complete script to draw a line:
import cv2 import numpy as np # Create a blank image image = np.ones((500, 500, 3), np.uint8) * 255 # Specify the start and end points, color, and thickness of the line start_point = (100, 100) end_point = (400, 400) color = (0, 0, 255) # Red in BGR thickness = 2 # Draw the line on the image cv2.line(image, start_point, end_point, color, thickness) # Display the image cv2.imshow('Line', image) cv2.waitKey(0) cv2.destroyAllWindows()
This will show a white image with a diagonal red line. Adjust the start and end points, color, and thickness as needed to draw different lines.
Python code for drawing lines in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Draw a line (start_point, end_point, color, thickness) cv2.line(img, (50, 50), (350, 250), (0, 255, 0), 2) # Display the image with the drawn line cv2.imshow('Line Drawing', img) cv2.waitKey(0) cv2.destroyAllWindows()
Line drawing functions and parameters in OpenCV:
# OpenCV line function signature cv2.line(img, pt1, pt2, color, thickness, lineType)
pt1
: Starting point of the line.pt2
: Ending point of the line.color
: Color of the line.thickness
: Thickness of the line.lineType
: Type of line (e.g., cv2.LINE_8
, cv2.LINE_AA
).Drawing lines with specified color and thickness in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Draw a blue line with thickness 3 cv2.line(img, (50, 50), (350, 250), (255, 0, 0), 3) # Display the image with the drawn line cv2.imshow('Colored Line Drawing', img) cv2.waitKey(0) cv2.destroyAllWindows()
Real-time line drawing in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Callback function for mouse events def draw_line(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: cv2.line(img, (x, y), (x + 50, y + 50), (0, 255, 0), 2) cv2.imshow('Real-time Line Drawing', img) # Create a named window cv2.namedWindow('Real-time Line Drawing') # Set the callback function for mouse events cv2.setMouseCallback('Real-time Line Drawing', draw_line) while True: # Break the loop on 'Esc' key press if cv2.waitKey(1) & 0xFF == 27: break cv2.destroyAllWindows()
Drawing lines on images and videos in OpenCV:
import cv2 # Load an image img = cv2.imread('image.jpg') # Draw a line on the image cv2.line(img, (50, 50), (350, 250), (0, 255, 0), 2) # Display the image with the drawn line cv2.imshow('Line on Image', img) cv2.waitKey(0) cv2.destroyAllWindows()
For videos, use the same cv2.line()
function within a video processing loop.
Combining line drawing with other OpenCV functions:
import cv2 import numpy as np # Load an image img = cv2.imread('image.jpg') # Draw a line and a rectangle on the image cv2.line(img, (50, 50), (350, 250), (0, 255, 0), 2) cv2.rectangle(img, (100, 100), (300, 200), (0, 0, 255), 2) # Display the image with the drawn line and rectangle cv2.imshow('Line and Rectangle Drawing', img) cv2.waitKey(0) cv2.destroyAllWindows()
Line annotations for object detection in OpenCV:
import cv2 import numpy as np # Load an image img = cv2.imread('object_detection_image.jpg') # Annotate object with a line cv2.line(img, (100, 50), (250, 150), (0, 255, 0), 2) # Display the image with the line annotation cv2.imshow('Object Detection with Line Annotation', img) cv2.waitKey(0) cv2.destroyAllWindows()
Drawing lines with specified endpoints in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Draw a line with specified endpoints pt1 = (50, 50) pt2 = (350, 250) cv2.line(img, pt1, pt2, (0, 255, 0), 2) # Display the image with the drawn line cv2.imshow('Line with Specified Endpoints', img) cv2.waitKey(0) cv2.destroyAllWindows()
Drawing dashed and dotted lines in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Draw a dashed line cv2.line(img, (50, 50), (350, 250), (0, 255, 0), 2, lineType=cv2.LINE_AA) # Draw a dotted line cv2.line(img, (50, 150), (350, 350), (0, 0, 255), 2, lineType=cv2.LINE_AA) # Display the image with the drawn lines cv2.imshow('Dashed and Dotted Lines', img) cv2.waitKey(0) cv2.destroyAllWindows()
Adding transparency to drawn lines in OpenCV:
import cv2 import numpy as np # Create a transparent image img = np.zeros((300, 400, 4), dtype=np.uint8) img[:, :, 3] = 255 # Set alpha channel to 255 (fully opaque) # Draw a transparent line cv2.line(img, (50, 50), (350, 250), (0, 255, 0, 128), 2) # Display the image with the drawn transparent line cv2.imshow('Transparent Line Drawing', img) cv2.waitKey(0) cv2.destroyAllWindows()
Code examples for drawing lines with different styles in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Draw a solid line cv2.line(img, (50, 50), (350, 250), (0, 255, 0), 2) # Draw a dashed line cv2.line(img, (50, 150), (350, 350), (0, 0, 255), 2, lineType=cv2.LINE_AA) # Draw a dotted line cv2.line(img, (50, 250), (350, 450), (255, 0, 0), 2, lineType=cv2.LINE_AA) # Display the image with the drawn lines cv2.imshow('Different Line Styles', img) cv2.waitKey(0) cv2.destroyAllWindows()