当前位置:网站首页>【OpenCV 例程200篇】217. 鼠标交互获取多边形区域(ROI)
【OpenCV 例程200篇】217. 鼠标交互获取多边形区域(ROI)
2022-07-03 15:35:00 【小白YouCans】
【youcans 的 OpenCV 例程200篇】217. 鼠标交互获取多边形区域
函数 cv.selectROI 可以通过鼠标在图像上选择感兴趣的矩形区域(ROI,region of interest)。
如果要通过鼠标在图像上选择感兴趣的多边形区域,可以利用鼠标交互来绘制。
函数原型:
函数 cv.polylines() 用来绘制多边形曲线或多段线。
函数 cv.fillPoly() 用来绘制一个或多个填充的多边形区域。
cv.polylines(img, pts, isClosed, color[, thickness=1, lineType=LINE_8, shift=0]) → img
cv.fillPoly(img, pts, color[, lineType=LINE_8, shift=0, offset=Point()]) → img
参数说明:
- img:输入输出图像,允许单通道灰度图像或多通道彩色图像
- pts:多边形顶点坐标, 二维 Numpy 数组的列表
- isClosed: 闭合标志,True 表示闭合多边形,False 表示多边形不闭合
特别注意多边形顶点坐标 pts 的格式:
- pts 是一个列表,列表中的元素是二维 Numpy 数组,每个元素表示一组顶点坐标。
- 二维 Numpy 数组的形状为 (m,2),每行表示多边形的一个顶点的坐标 (xi,yi),数据格式应为整型。
例如:
points1 = np.array([[200,100], [295,169], [259,281], [141,281], [105,169]], np.int)
points2 = np.array([[200,400], [259,581], [105,469], [295,469], [141,581]])
points1、points2 是形状为 (m,2) 的二维 Numpy 数组。
对于函数 cv.polylines 与 cv.fillPoly,不能直接把二维 Numpy 数组 points1 或 points2作为函数参数,而要将其作为列表的元素,如: [points1]、[points2] 或 [points1, points2]。
例程 A4.9:鼠标交互获取多边形区域(ROI)
函数 cv.selectROI 可以通过鼠标在图像上选择感兴趣的矩形区域(ROI,region of interest)。
如果要通过鼠标在图像上选择感兴趣的多边形区域,可以利用鼠标交互来绘制。
# A4.9 鼠标交互获取多边形区域
def mouseHandler(event, x, y, flags, param): # 鼠标交互 (左键选点右键完成)
global drawing
setpoint = (x, y)
if event == cv.EVENT_LBUTTONDOWN: # 鼠标左键点击事件
drawing = True # 开启绘图状态
pts.append(setpoint) # 选择多边形顶点
print("选择顶点 {}:{}".format(len(pts), setpoint))
elif event == cv.EVENT_RBUTTONDOWN: # 鼠标右键点击事件
drawing = False # 结束绘图状态
print("结束绘制。\n ROI 顶点坐标:")
print(pts)
# 鼠标交互绘制多边形 ROI
img = cv.imread("../images/imgLena.tif") # 读取彩色图像(BGR)
# 鼠标交互 ROI
print("单击鼠标左键:选择 ROI 顶点")
print("单击鼠标右键:结束 ROI 选择")
print("按 ESC 退出")
pts = [] # ROI 顶点坐标向量
drawing = True # 开启绘图状态
cv.namedWindow('origin') # 创建图像窗口
cv.setMouseCallback('origin', mouseHandler) # 窗口与回调函数绑定
while True:
imgCopy = img.copy()
if len(pts) > 0:
cv.circle(imgCopy, pts[-1], 5, (0,0,255), -1) # 绘制最近一个顶点
if len(pts) > 1:
for i in range(len(pts)-1):
cv.circle(imgCopy, pts[i], 5, (0,0,255), -1) # 绘制顶点
cv.line(imgCopy, pts[i], pts[i+1], (255,0,0), 2) # 绘制边界线段
if drawing==False:
cv.line(imgCopy, pts[0], pts[-1], (255,0,0), 2) # 完成最后一段线段
cv.imshow('origin', imgCopy)
key = 0xFF & cv.waitKey(1) # 按 ESC 退出
if key == 27: # Esc 退出
break
cv.destroyAllWindows() # 图像窗口
points = np.array(pts, np.int) # ROI 多边形顶点坐标集
cv.polylines(img, [points], True, (255,255,255), 2) # 在 img 绘制 ROI 多边形
mask = np.zeros(img.shape[:2], np.uint8) # 黑色掩模,单通道
cv.fillPoly(mask, [points], (255,255,255)) # 多边形 ROI 为白色窗口
imgROI = cv.bitwise_and(img, img, mask=mask) # 按位与,从 img 中提取 ROI
plt.figure(figsize=(9, 6))
plt.subplot(131), plt.title("origin image"), plt.axis('off')
plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB))
plt.subplot(132), plt.title("ROI mask"), plt.axis('off')
plt.imshow(mask, cmap='gray')
plt.subplot(133), plt.title("ROI cropped"), plt.axis('off')
plt.imshow(cv.cvtColor(imgROI, cv.COLOR_BGR2RGB))
plt.tight_layout()
plt.show()
【本节完】
版权声明:
[email protected] 原创作品,转载必须标注原文链接:(https://blog.csdn.net/youcans/article/details/125490992)
Copyright 2022 youcans, XUPT
Crated:2022-6-26
欢迎关注 『youcans 的 OpenCV 例程 200 篇』 系列,持续更新中
欢迎关注 『youcans 的 OpenCV学习课』 系列,持续更新中
210. 绘制直线也会有这么多坑?
211. 绘制垂直矩形
215. 基于多段线绘制近似椭圆
216. 绘制多段线和多边形
217. 鼠标交互获取多边形区域
边栏推荐
- CString在多线程中的问题
- 需要知道的字符串函数
- VS2017通过IP调试驱动(双机调试)
- Kubernetes - yaml file interpretation
- Popular understanding of linear regression (I)
- Halcon and WinForm study section 1
- leetcode_ Power of Four
- Calibre LVL
- [cloud native training camp] module VIII kubernetes life cycle management and service discovery
- Jvm-04-runtime data area heap, method area
猜你喜欢
整形和浮点型是如何在内存中的存储
[cloud native training camp] module VIII kubernetes life cycle management and service discovery
Jvm-02-class loading subsystem
CString getbuffer and releasebuffer instructions
【云原生训练营】模块七 Kubernetes 控制平面组件:调度器与控制器
Summary of JVM knowledge points
Redis在Windows以及Linux系统下的安装
nifi从入门到实战(保姆级教程)——flow
Kubernetes will show you from beginning to end
《微服务设计》读书笔记(下)
随机推荐
找映射关系
CString getbuffer and releasebuffer instructions
Jvm-04-runtime data area heap, method area
从 flask 服务端代码自动生成客户端代码 -- flask-native-stubs 库介绍
Popular understanding of linear regression (I)
App移动端测试【4】apk的操纵
How to use annotations such as @notnull to verify and handle global exceptions
视觉上位系统设计开发(halcon-winform)-6.节点与宫格
Seckill system 2 redis solves the problem of distributed session
Visual upper system design and development (Halcon WinForm) -2 Global variable design
视觉上位系统设计开发(halcon-winform)-2.全局变量设计
PyTorch crop images differentiablly
Construction of operation and maintenance system
整形和浮点型是如何在内存中的存储
Subclass hides the function with the same name of the parent class
The difference between mutually exclusive objects and critical areas
Analysis of development mode process based on SVN branch
Find mapping relationship
Win10 enterprise 2016 long term service activation tutorial
Kubernetes advanced training camp pod Foundation