Python OpenCV 畫多邊形 polylines

本篇將介紹如何使用 OpenCV 與 Python 來畫多邊形 polylines,在寫 Python 影像處理程式時常會用到 OpenCV cv2.polylines 畫多邊形的功能,接下來介紹怎麼使用 Python 搭配 OpenCV 模組來畫多邊形 polylines。

使用範例

詳細程式碼如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np

# Create a blank 640x480 black image
image = np.zeros((480, 640, 3), np.uint8)
# Fill image with gray color(set each pixel to gray)
image[:] = (128, 128, 128)

points = np.array([[200, 200], [300, 100], [400, 200], [400, 400], [200, 400]], np.int32)
red_color = (0, 0, 255) # BGR
cv2.polylines(image, pts=[points], isClosed=True, color=red_color, thickness=3)

cv2.imshow('Result', image)
cv2.waitKey(0)

結果如下圖所示:

cv2.polylines 參數的詳細細節請參考這裡

其它相關文章推薦
Python OpenCV 畫線條 line
Python OpenCV 畫圓 circle
Python OpenCV 畫橢圓 ellipse
Python OpenCV 畫矩形 rectangle
Python OpenCV 繪製文字 putText