使用 OpenCV 在图像上绘制线条


在本程序中,我们将使用 OpenCV 函数 line() 在图像上绘制一条简单的线。

原始图像

算法

Step 1: Import cv2.
Step 2: Read the image using imread().
Step 3: Get the dimensions of the image using the image.shape method.
Step 4: Define starting point of the line.
Step 5: Define the end point of the line.
Step 6: Define the thickness of the line.
Step 7: Draw the line using the cv2.line() function and pass Step 3 to Step 4 as parameters.

示例代码

import cv2

image = cv2.imread('testimage.jpg')
height, width, channels = image.shape
startpoint = (0, 0)
endpoint = (height, width)
thickness = 9
color = (255, 0, 0)
image = cv2.line(image, startpoint, endpoint, color, thickness)
cv2.imshow('Line', image)

输出

解释

如你所见,图像上绘制了一条蓝线。

更新于:2021-3-17

1K+ 次浏览

启动您的职业生涯

通过完成该课程来获得认证

开始
广告