Python OpenCV – cv2.polylines()方法使用示例

2021年3月16日15:39:41 发表评论 3,683 次浏览

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

注意:有关更多信息, 请参阅OpenCV Python教程

cv2.polylines()

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

语法:cv2.polylines(image, [pts], isClosed, color, thickness)参数:image:这是要在其上绘制圆的图像。 pts:多边形曲线数组。 npts:多边形顶点计数器的数组。 ncontours:曲线数。 isClosed:指示绘制的折线是否关闭的标志。如果它们是闭合的, 则该函数将从每个曲线的最后一个顶点到其第一个顶点绘制一条直线。 color:它是要绘制的折线的颜色。对于BGR, 我们传递一个元组。厚度:是折线边缘的厚度。返回值:返回图像。

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

Python OpenCV – cv2.polylines()方法1

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

范例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()

注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: