当前位置: 移动技术网 > IT编程>脚本编程>Python > OpenCV 画多边形 — cv.polylines()函数使用

OpenCV 画多边形 — cv.polylines()函数使用

2020年09月29日  | 移动技术网IT编程  | 我要评论
摘要在图像img上画若干多边形函数使用polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]])

1、格式

  • polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]])

2、功能

  • 在图像 img 上画若干多边形

3、输入

  • img:要在上面画多边形的图像
  • pts:包含多边形上点的数组
  • isClosed:标志,决定所绘制的多边形是否闭合。若为 True ,则画若干个闭合多边形;若为 False ,则画一条连接所有点的折线
  • color:多边形颜色
  • thickness:多边形线的粗细
  • lineType:多边形线的类型
  • shift:坐标精确到小数点后第几位

4、输出

  • img:画完多边形的输入图像

5、示例

  • 代码
# 导入 OpenCV import cv2 as cv import numpy as np import random # 读取图像 imgBgr = cv.imread(r'/home/work/0/OpenCV/0/img/Ta152.jpg') print('\nimgBgr.shape:', imgBgr.shape) # (768, 1024, 3) # 随机获取多边形坐标点列表 numPt = 5 # 每个多边形上坐标点的个数 listPt = [] for j in range(numPt): x = random.randrange(imgBgr.shape[1]) y = random.randrange(imgBgr.shape[0]) listPt.append([x, y]) # 多边形坐标点列表格式转换 arrPt = np.array(listPt, np.int32).reshape((-1, 1, 2)) # 画多边形 imgRet = cv.polylines(imgBgr, [arrPt], True, (0, 255, 0), 3) # 比较输入图像和返回图像的内存地址 print('\nid(imgBgr) == id(imgRet):', id(imgBgr) == id(imgRet)) # True # 显示图像 cv.imshow('imgBgr', imgBgr) idKey = cv.waitKey(0) if idKey == '27': # 27 为 ESC 键对应的 ASCII 码 cv.destroyAllWindows() 
  • 效果

在这里插入图片描述

6、备注

  • pts 参数外面要用 [] 括起来
  • pts 参数中坐标的顺序为 (x, y)

本文地址:https://blog.csdn.net/zhanling1007/article/details/108865590

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网