使用 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)

輸出

更新時間:2021-03-17

4K+ 瀏覽次數

開始你的 職業

完成課程取證

開始
廣告