📜  使用 OpenCV函数fillPoly() 绘制填充多边形

📅  最后修改于: 2022-05-13 01:55:28.064000             🧑  作者: Mango

使用 OpenCV函数fillPoly() 绘制填充多边形

OpenCV 的 fillPoly()函数用于在图像上绘制填充的多边形,如矩形、三角形、五边形。此函数接受输入图像和多边形和颜色的端点。

示例 1:画一个三角形

在此示例中,我们将通过将 [160,130]、[350,130]、[250,300] 等 3 个端点提供给 fillPoly()函数来绘制填充多边形三角形。

输入图像:

代码:

Python3
# Import necessary libraries
import cv2
import numpy as np
  
# Read an image
img = cv2.imread("image.png")
  
# Define an array of endpoints of triangle
points = np.array([[160, 130], [350, 130], [250, 300]])
  
# Use fillPoly() function and give input as
# image, end points,color of polygon
# Here color of polygon will blue
cv2.fillPoly(img, pts=[points], color=(255, 0, 0))
  
# Displaying the image
cv2.imshow("Triangle", img)
  
# wait for the user to press any key to 
# exit window
cv2.waitKey(0)
  
# Closing all open windows
cv2.destroyAllWindows()


Python3
# Import necessary libraries
import cv2
import numpy as np
  
# Read an image
img = cv2.imread("image.png")
  
# Define an array of endpoints of Hexagon
points = np.array([[220, 120], [130, 200], [130, 300],
                   [220, 380], [310, 300], [310, 200]])
  
# Use fillPoly() function and give input as image,
# end points,color of polygon
# Here color of polygon will be green
cv2.fillPoly(img, pts=[points], color=(0, 255, 0))
  
# Displaying the image
cv2.imshow("Hexagon", img)
  
# wait for the user to press any key to 
# exit window
cv2.waitKey(0)
  
# Closing all open windows
cv2.destroyAllWindows()


Python3
# Import necessary libraries
import cv2
import numpy as np
  
# Read an image
img = cv2.imread("Documents/Person_Image.jpg", 
                 cv2.IMREAD_COLOR)
  
# Define an array of endpoints of Rectangle
points = np.array([[300, 180], [400, 180], 
                   [400, 280], [300, 280]])
  
# Use fillPoly() function and give input as image,
# end points,color of polygon
# Here color of polygon will be red
cv2.fillPoly(img, pts=[points], color=(0, 0, 255))
  
# Displaying the image
cv2.imshow("Rectangle", img)
  
# wait for the user to press any key to exit window
cv2.waitKey(0)
  
# Closing all open windows
cv2.destroyAllWindows()


输出:

示例 2:绘制六边形

在此示例中,我们将通过将 [220,120]、[130,200]、[130,300]、[220,380]、[310,300]、[310,200] 等 6 个端点提供给 fillPoly()函数来绘制一个六边形。

输入:

代码:

Python3

# Import necessary libraries
import cv2
import numpy as np
  
# Read an image
img = cv2.imread("image.png")
  
# Define an array of endpoints of Hexagon
points = np.array([[220, 120], [130, 200], [130, 300],
                   [220, 380], [310, 300], [310, 200]])
  
# Use fillPoly() function and give input as image,
# end points,color of polygon
# Here color of polygon will be green
cv2.fillPoly(img, pts=[points], color=(0, 255, 0))
  
# Displaying the image
cv2.imshow("Hexagon", img)
  
# wait for the user to press any key to 
# exit window
cv2.waitKey(0)
  
# Closing all open windows
cv2.destroyAllWindows()

输出:

示例 3:绘制一个矩形

有时我们需要通过隐藏他们的脸来显示某人的照片。在这种情况下,我们可以使用这个函数来隐藏一个人的脸。

输入:

代码:

Python3

# Import necessary libraries
import cv2
import numpy as np
  
# Read an image
img = cv2.imread("Documents/Person_Image.jpg", 
                 cv2.IMREAD_COLOR)
  
# Define an array of endpoints of Rectangle
points = np.array([[300, 180], [400, 180], 
                   [400, 280], [300, 280]])
  
# Use fillPoly() function and give input as image,
# end points,color of polygon
# Here color of polygon will be red
cv2.fillPoly(img, pts=[points], color=(0, 0, 255))
  
# Displaying the image
cv2.imshow("Rectangle", img)
  
# wait for the user to press any key to exit window
cv2.waitKey(0)
  
# Closing all open windows
cv2.destroyAllWindows()

输出: