首先導入我們所需要的庫:
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((300,300,3),dtype='uint8')
show(image)#果然顯示出來的是黑色的圖片
#畫線(直線) green=(0,255,0) cv2.line(image,(0,0),(300,300),green) show(image)
blue=(0,0,255) cv2.line(image,(300,0),(150,150),blue,5) show(image)#不知道為什么左下角還有一條線呢?
red=(255,0,0) cv2.line(image,(0,300),(150,150),red,5) show(image)#不知道為什么左下角還有一條線呢?
#現在開始畫矩形 cv2.rectangle(image,(90,90),(220,220),green,-1)#-1表示的是填充矩形的意思 show(image)
#現在開始畫圓 image2=np.zeros((300,300,3),dtype='uint8')
在畫圓的時候重新繪制一個畫布
green=(0,255,0) cv2.circle(image2,(150,150),50,green,3) show(image2