使用 OpenCV 在图像上绘制矩形
在本程序中,我们将使用 OpenCV 函数 rectangle() 绘制一个矩形。此函数需要一些参数,如起始坐标、结束坐标、颜色和厚度以及图像本身。
原图
算法
Step 1: Import cv2. Step 2: Read the image using imread(). Step 3: Define the starting coordinates. Step 5: Define the ending coordinates. Step 6: Define the color and the thickness. Step 7: Draw the rectangle using the cv2.reactangle() function. Step 8: Display the rectangle.
示例代码
import cv2 image = cv2.imread('testimage.jpg') height, width, channels = image.shape start_point = (0,0) end_point = (width, height) color = (0,0,255) thickness = 5 image = cv2.rectangle(image, start_point, end_point, color, thickness) cv2.imshow('Rectangle',image)
输出
广告