Tuesday 21 November 2023

Draw a line with OpenCV

Using ‘cv2.line’ method, we can draw a line.

Signature

cv2.line(image, pt1, pt2, color, thickness, lineType, shift)

 

Following table summarizes the parameters of line method.

 

Parameter

Description

image

Specifies the image on which you want to draw the line.

pt1

It is a tuple (x1, y1), define the starting point of the line.

pt2

It is a tuple (x2 y2), define the ending point of the line

color

Specifies the line color in BGR (Blue, Gree, Red) format.

thickness

Specify the thickness of the line.

shift

The number of fractional bits in the pt1 and pt2 parameters. It is typically set to 0.

 

line.py

import cv2 as cv
import numpy as np

# Create a black image
image = np.zeros((300, 300, 3), dtype=np.uint8)
image[:] = [255, 255, 255]

# Define the starting and ending points of the line
pt1 = (50, 100)
pt2 = (250, 100)


pt3 = (50, 150)
pt4 = (250, 150)

# Define the color (blue in BGR format)
color1 = (255, 0, 0)
color2 = (0, 255, 0)

# Draw the line
cv.line(image, pt1, pt2, color1, thickness=2)
cv.line(image, pt3, pt4, color2, thickness=10)

# Display the image
cv.imshow("Line Example", image)
cv.waitKey(0)
cv.destroyAllWindows()

 

Output


 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment