您的位置:

OpenCV fillPoly详解

一、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()