1. 程式人生 > >OpenCV畫線、矩形、圓形

OpenCV畫線、矩形、圓形

程式碼位置:9-DrawingLineRectangleCircle.py

import numpy as np
import cv2
import matplotlib.pyplot as plt

def show(image):
    plt.imshow(image)
    plt.axis('off')
    plt.show()

image = np.zeros((400, 400,3), dtype='uint8')

red = (255, 0, 0)
cv2.line(image, (0,0), (400, 400), red)

green = (0, 255, 0)
cv2.line(image, (400, 0), (0,400), green, 5)

blue = (0, 0, 255)
cv2.rectangle(image, (200, 200), (250, 250), blue, 2)

white = (255, 255, 255)
cv2.rectangle(image, (20, 300), (40, 350), white, -1)

(cX, cY) = image.shape[1]//2, image.shape[0]//2

cv2.circle(image, (cX, cY - 100), 40, white, 2)

show(image)
  • 這裡通過np.zeros生成了一個黑色的底圖。通過line、rectangle、circle進行繪製。
  • 特別說明的是繪製函式的最後一個引數,是線的粗細,如果是‘-1’代表進行顏色填充
    在這裡插入圖片描述