import numpy as np
import cv2
import matplotlib.pyplot as plt
img = np.zeros((10,10))
plt.imshow(img)
rectangle = np.array([[1,1], [2, 1], [2,2], [1,2]])
poly = np.array([[4,4], [8, 5], [7, 9], [2,8], [3, 7]])
poly_list = [rectangle, poly] #note this is a list of numpy arrays, if only one poly, then its a list of one poly, i.e [poly]
cv2.fillPoly(img, poly_list, 255)
plt.imshow(img)
What if the image is 3 dimensional, i.e. color as a third dimension
a = np.zeros((4,5,3))
rectangle = np.array([2,2,3,2,3,3,2,3]).reshape((4,2))
cv2.fillPoly(a, [rectangle], 1)
Only the first element of the third dimension is filled.
array([[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[1., 0., 0.],
[1., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[1., 0., 0.],
[1., 0., 0.],
[0., 0., 0.]]])
However, it can fill an array as below then all elements of the third dimension are filled.
cv2.fillPoly(a, [rectangle], [1,1,1])
array([[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[1., 1., 1.],
[1., 1., 1.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 0., 0.],
[1., 1., 1.],
[1., 1., 1.],
[0., 0., 0.]]])