Python OpenCV – cv2.polylines() 方法
OpenCV 是用于计算机视觉、机器学习和图像处理的大型开源库,现在它在实时操作中发挥着重要作用,这在当今的系统中非常重要。通过使用它,人们可以处理图像和视频以识别物体、面部,甚至是人类的笔迹。当它与 Numpuy 等各种库集成时, Python能够处理 OpenCV 数组结构进行分析。
注意:更多信息请参考 OpenCV Python教程
cv2.polylines()
cv2.polylines()方法用于在任何图像上绘制多边形。
Syntax: cv2.polylines(image, [pts], isClosed, color, thickness)
Parameters:
image: It is the image on which circle is to be drawn.
pts: Array of polygonal curves.
npts: Array of polygon vertex counters.
ncontours: Number of curves.
isClosed: Flag indicating whether the drawn polylines are closed or not. If they are closed, the function draws a line from the last vertex of each curve to its first
vertex.
color: It is the color of polyline to be drawn. For BGR, we pass a tuple.
thickness: It is thickness of the polyline edges.
Return Value: It returns an image.
用于以下所有示例的图像:
示例 #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()
输出:
示例 #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()
输出: