📜  Python OpenCV – cv2.polylines() 方法

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

Python OpenCV – cv2.polylines() 方法

OpenCV 是用于计算机视觉、机器学习和图像处理的大型开源库,现在它在实时操作中发挥着重要作用,这在当今的系统中非常重要。通过使用它,人们可以处理图像和视频以识别物体、面部,甚至是人类的笔迹。当它与 Numpuy 等各种库集成时, Python能够处理 OpenCV 数组结构进行分析。

注意:更多信息请参考 OpenCV Python教程

cv2.polylines()

cv2.polylines()方法用于在任何图像上绘制多边形。

用于以下所有示例的图像:

用于以下所有示例的图像:

示例 #1:

# Python program to explain 
# cv2.polylines() method 
  
import cv2
import numpy as np
  
# path
path = gfg.jpeg'
  
# Reading an image in default
# mode
image = cv2.imread(path)
  
# Window name in which image is
# displayed
window_name = 'Image'
  
# Polygon corner points coordinates
pts = np.array([[25, 70], [25, 160], 
                [110, 200], [200, 160], 
                [200, 70], [110, 20]],
               np.int32)
  
pts = pts.reshape((-1, 1, 2))
  
isClosed = True
  
# Blue color in BGR
color = (255, 0, 0)
  
# Line thickness of 2 px
thickness = 2
  
# Using cv2.polylines() method
# Draw a Blue polygon with 
# thickness of 1 px
image = cv2.polylines(image, [pts], 
                      isClosed, color, thickness)
  
# Displaying the image
while(1):
      
    cv2.imshow('image', image)
    if cv2.waitKey(20) & 0xFF == 27:
        break
          
cv2.destroyAllWindows()

输出:
cv2.polylines()

示例 #2:

# Python program to explain 
# cv2.polylines() method
  
import cv2
import numpy as np
  
# path
path = r'gfg.jpeg'
  
# Reading an image in default 
# mode
image = cv2.imread(path)
  
# Window name in which image is 
# displayed
window_name = 'Image'
  
# Polygon corner points coordinates
pts = np.array([[25, 70], [25, 145],
                [75, 190], [150, 190],
                [200, 145], [200, 70], 
                [150, 25], [75, 25]],
               np.int32)
  
pts = pts.reshape((-1, 1, 2))
  
isClosed = True
  
# Green color in BGR
color = (0, 255, 0)
  
# Line thickness of 8 px
thickness = 8
  
# Using cv2.polylines() method
# Draw a Green polygon with 
# thickness of 1 px
image = cv2.polylines(image, [pts], 
                      isClosed, color, 
                      thickness)
  
# Displaying the image
while(1):
      
    cv2.imshow('image', image)
    if cv2.waitKey(20) & 0xFF == 27:
          
        break
cv2.destroyAllWindows()

输出:
cv2.polylines()