一、fillPoly的基本概念
fillPoly是OpenCV庫中的一個函數,用於在圖像上繪製一個填充的多邊形。它的語法如下:
cv2.fillPoly(img, pts, color[, lineType[, shift[, offset]]])
參數解釋:
- img: 需要被繪製的圖像
- pts: 需要繪製的多邊形各個定點的坐標數組,數據類型為numpy.int32,大小為 [npts, 1, 2]
- color: 繪製的顏色,可以為單個BGR值或BGR數組
- lineType: 多邊形的線條類型,默認為cv2.LINE_8
- shift: 繪製精度,默認為0(單精度)
- offset: 繪製的偏移量,默認為(0,0)
二、fillPoly常用參數詳解
1. 多邊形的頂點坐標數組
pts是表示多邊形各個定點的坐標數組,它的數據類型為numpy.int32,大小為[npts, 1, 2]。其中npts代表多邊形的頂點數,1代表維度,2代表x和y坐標。
代碼示例:
import numpy as np
import cv2
# 定義多邊形的頂點坐標數組
pts = np.array([[(200, 150), (300, 50), (400, 150), (300, 250)]], dtype=np.int32)
# 創建一個黑色圖像
img = np.zeros((512, 512, 3), dtype=np.uint8)
# 繪製多邊形
cv2.fillPoly(img, pts, (0, 255, 0))
# 顯示圖像
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
2. 繪製多個多邊形
可以通過傳入多個pts數組,實現繪製多個多邊形,如下所示:
# 定義兩個多邊形頂點坐標數組
pts1 = np.array([[(200, 150), (300, 50), (400, 150), (300, 250)]], dtype=np.int32)
pts2 = np.array([[(120, 240), (190, 240), (190, 310), (120, 310)],
[(240, 240), (310, 240), (310, 310), (240, 310)]], dtype=np.int32)
# 創建一個黑色圖像
img = np.zeros((512, 512, 3), dtype=np.uint8)
# 繪製多邊形
cv2.fillPoly(img, [pts1, pts2], (0, 255, 0))
# 顯示圖像
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
3. 繪製填充有孔的多邊形
對於具有鏤空效果的多邊形,需要繪製多個多邊形。內部的多邊形使用相反的方向繪製,在繪製時,需要在outer的pts數組之前添加inner的pts數組。
代碼示例:
# 定義多個多邊形頂點坐標數組,後面的內部多邊形需要繪製相反的方向
pts1 = np.array([[(200, 150), (300, 50), (400, 150), (300, 250)]], dtype=np.int32)
pts2 = np.array([[(120, 240), (190, 240), (190, 310), (120, 310)],
[(240, 240), (310, 240), (310, 310), (240, 310)]], dtype=np.int32)
# 創建一個黑色圖像
img = np.zeros((512, 512, 3), dtype=np.uint8)
# 繪製多邊形
cv2.fillPoly(img, [pts1, pts2], (0, 255, 0))
# 顯示圖像
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
三、fillPoly實例應用
1. 繪製有界區域
可以使用fillPoly函數繪製一個由直線和弧線圍成的有界區域,如下所示:
# 創建一張黑色的圖像
img = np.zeros((300, 300, 3), dtype=np.uint8)
# 畫整個圖形,以便用lineType參數畫出虛線
pts = np.array([[(151, 71), (253, 63), (282, 126), (240, 245), (102, 245), (60, 126)]], dtype=np.int32)
cv2.fillPoly(img, pts, (255, 255, 255))
# 繪製虛線
pts = np.array([[(151, 71), (253, 63), (282, 126), (240, 245), (102, 245), (60, 126)],
[(120, 189), (180, 192)],
[(118, 211), (182, 209)],
[(244, 205), (262, 213), (276, 226)],
[(114, 170), (162, 167), (163, 171), (138, 176)],
[(162, 167), (179, 168), (181, 172), (163, 171)],
[(139, 151), (186, 157), (170, 164)],
[(186, 157), (214, 196), (202, 203), (170, 164)],
[(109, 217), (79, 255), (80, 227)],
[(78, 254), (158, 207), (152, 224), (80, 227)],
[(241, 194), (221, 253), (248, 218)],
[(222, 251), (241, 194), (254, 214)]])
for i in range(0, pts.shape[0]):
cv2.polylines(img, [pts[i]], isClosed=True, color=(255, 0, 0), thickness=2, lineType=cv2.LINE_AA)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
2. 生成隨機多邊形
可以使用fillPoly函數生成隨機多邊形。首先生成隨機多邊形的頂點數和點坐標,然後繪製多邊形。
代碼示例:
import random
# 隨機生成多邊形頂點數目和坐標
npts = random.randint(3, 10)
pts = [(random.randint(50, 200), random.randint(50, 200)) for i in range(npts)]
pts_array = np.array([pts], np.int32)
# 創建一個黑色圖像
img = np.zeros((300, 300, 3), dtype=np.uint8)
# 繪製多邊形
cv2.fillPoly(img, [pts_array], (0, 255, 0))
# 顯示圖像
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
3. 二值圖像中繪製多邊形
可以使用fillPoly將多邊形繪製在二值圖像上。
代碼示例:
# 讀取一張灰度圖
img = cv2.imread('test.png', cv2.IMREAD_GRAYSCALE)
# 二值化圖像
ret, binary = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
# 找到輪廓
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
# 創建一張黑色圖像
h, w = img.shape
img = np.zeros((h, w, 3), dtype=np.uint8)
# 繪製輪廓
for i in range(len(contours)):
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
cv2.fillPoly(img, pts=[contours[i]], color=color)
# 顯示圖像
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
原創文章,作者:PUHW,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/136729.html