小言_互联网的博客

Python OpenCV实现鼠标绘制矩形框和多边形

608人阅读  评论(0)

Python OpenCV实现鼠标绘制矩形框和多边形

目录

Python OpenCV实现鼠标绘制矩形框和多边形

1. OpenCV鼠标事件操作说明

(1)setMouseCallback函数说明

(2)回调函数onMouse说明

(3)event 具体说明:

(4)flags 具体说明

2. OpenCV实现鼠标绘制矩形框和多边形框

(1)绘制矩形框

(2)绘制多边形

(3)键盘控制

3. 完整的代码


本篇将使用OpenCV开发一个简易的绘图工具,可以实现鼠标绘制矩形框和多边形,先看一下Demo效果

 源码已经开源在GitHub, 开源不易,麻烦给个【Star】:

GitHub - PanJinquan/base-utils: 集成C/C++ OpenCV 常用的算法和工具

使用PIP安装:

pip install pybaseutils

尊重原则,转载请注明出处https://blog.csdn.net/guyuealian/article/details/128019461

绘制矩形框 绘制多边形

1. OpenCV鼠标事件操作说明

OpenCV支持鼠标事件操作,通过setMouseCallback函数来设置鼠标事件的回调函数,使用方法可见官方文档说明

(1)setMouseCallback函数说明


  
  1. void setMousecallback(const string& winname, MouseCallback onMouse, void* userdata=0);
  2. //winname:窗口的名字
  3. //onMouse:鼠标响应函数,回调函数。指定窗口里每次鼠标时间发生的时候,被调用的函数指针。
  4. //这个函数的原型应该为void on_Mouse(int event, int x, int y, int flags, void* param);
  5. //userdate:传给回调函数的参数

(2)回调函数onMouse说明


  
  1. void onMouse(int event, int x, int y, int flags, void* param);
  2. //event是 CV_EVENT_*变量之一
  3. //x和y是鼠标指针在图像坐标系的坐标(不是窗口坐标系)
  4. //flags是CV_EVENT_FLAG的组合, param是用户定义的传递到setMouseCallback函数调用的参数。

 

(3)event 具体说明:

EVENT_MOUSEMOVE 0         //滑动
EVENT_LBUTTONDOWN 1  //左键点击
EVENT_RBUTTONDOWN 2  //右键点击
EVENT_MBUTTONDOWN 3  //中键点击
EVENT_LBUTTONUP 4          //左键放开
EVENT_RBUTTONUP 5         //右键放开
EVENT_MBUTTONUP 6          //中键放开
EVENT_LBUTTONDBLCLK 7 //左键双击
EVENT_RBUTTONDBLCLK 8 //右键双击
EVENT_MBUTTONDBLCLK 9 //中键双击

(4)flags 具体说明

EVENT_FLAG_LBUTTON 1   //左键拖曳
EVENT_FLAG_RBUTTON 2   //右键拖曳
EVENT_FLAG_MBUTTON 4   //中键拖曳
EVENT_FLAG_CTRLKEY 8     //(8~15)按 Ctrl 不放
EVENT_FLAG_SHIFTKEY 16 //(16~31)按 Shift 不放
EVENT_FLAG_ALTKEY 32      //(32~39)按 Alt 不放


2. OpenCV实现鼠标绘制矩形框和多边形

(1)绘制矩形框

这是实现绘制矩形框的关键代码


  
  1. def event_draw_rectangle( self, event, x, y, flags, param):
  2. """绘制矩形框"""
  3. if len(self.polygons) == 0: self.polygons = np.zeros(shape=( 2, 2), dtype=np.int32) # 多边形轮廓
  4. point = (x, y)
  5. if event == cv2.EVENT_LBUTTONDOWN: # 左键点击,则在原图打点
  6. print( "1-EVENT_LBUTTONDOWN")
  7. self. next = self.last.copy()
  8. self.polygons[ 0, :] = point
  9. cv2.circle(self. next, point, radius= 5, color=self.focus_color, thickness=self.thickness)
  10. elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON): # 按住左键拖曳,画框
  11. print( "2-EVENT_FLAG_LBUTTON")
  12. self. next = self.last.copy()
  13. cv2.circle(self. next, self.polygons[ 0, :], radius= 4, color=self.focus_color, thickness=self.thickness)
  14. cv2.circle(self. next, point, radius= 4, color=self.focus_color, thickness=self.thickness)
  15. cv2.rectangle(self. next, self.polygons[ 0, :], point, color=self.line_color, thickness=self.thickness)
  16. elif event == cv2.EVENT_LBUTTONUP: # 左键释放,显示
  17. print( "3-EVENT_LBUTTONUP")
  18. self. next = self.last.copy()
  19. self.polygons[ 1, :] = point
  20. cv2.rectangle(self. next, self.polygons[ 0, :], point, color=self.line_color, thickness=self.thickness)
  21. print( "location:{},have:{}". format(point, len(self.polygons)))

(2)绘制多边形

这是实现绘制多边形的关键代码


  
  1. def event_draw_polygon( self, event, x, y, flags, param):
  2. """绘制多边形"""
  3. exceed = self.max_point > 0 and len(self.polygons) >= self.max_point
  4. self. next = self.last.copy()
  5. point = (x, y)
  6. text = str( len(self.polygons))
  7. if event == cv2.EVENT_LBUTTONDOWN: # 左键点击,则在原图打点
  8. print( "1-EVENT_LBUTTONDOWN")
  9. cv2.circle(self. next, point, radius= 5, color=self.focus_color, thickness=self.thickness)
  10. cv2.putText(self. next, text, point, cv2.FONT_HERSHEY_SIMPLEX, 0.5, self.text_color, 2)
  11. if len(self.polygons) > 0:
  12. cv2.line(self. next, self.polygons[- 1, :], point, color=self.line_color, thickness=self.thickness)
  13. if not exceed:
  14. self.last = self. next
  15. self.polygons = np.concatenate([self.polygons, np.array(point).reshape( 1, 2)])
  16. else:
  17. cv2.circle(self. next, point, radius= 5, color=self.focus_color, thickness=self.thickness)
  18. if len(self.polygons) > 0:
  19. cv2.line(self. next, self.polygons[- 1, :], point, color=self.line_color, thickness=self.thickness)
  20. print( "location:{},have:{}". format(point, len(self.polygons)))

(3)键盘控制

为了方便用户操作,这里定义几个常用的按键:

  1. 按空格和回车键表示完成绘制
  2. 按ESC退出程序
  3. 按键盘c重新绘制

  
  1. def task( self, image, callback: Callable, winname="winname"):
  2. """
  3. 鼠标监听任务
  4. :param image: 图像
  5. :param callback: 鼠标回调函数
  6. :param winname: 窗口名称
  7. :return:
  8. """
  9. self.orig = image.copy()
  10. self.last = image.copy()
  11. self. next = image.copy()
  12. cv2.namedWindow(winname, flags=cv2.WINDOW_NORMAL)
  13. cv2.setMouseCallback(winname, callback, param={ "winname": winname})
  14. while True:
  15. self.key = self.show_image(winname, self. next, delay= 25)
  16. print( "key={}". format(self.key))
  17. if (self.key == 13 or self.key == 32) and len(self.polygons) > 0: # 按空格32和回车键13表示完成绘制
  18. break
  19. elif self.key == 27: # ESC退出程序
  20. exit( 0)
  21. elif self.key == 99: # 按键盘c重新绘制
  22. self.clear()
  23. # cv2.destroyAllWindows()

3. 完整的代码

 源码已经开源在GitHub, 开源不易,麻烦给个【Star】:

GitHub - PanJinquan/base-utils: 集成C/C++ OpenCV 常用的算法和工具

使用PIP安装:

pip install pybaseutils

demo测试:base-utils/mouse_utils.py at master · PanJinquan/base-utils · GitHub


  
  1. # -*-coding: utf-8 -*-
  2. """
  3. @Author : panjq
  4. @E-mail : pan_jinquan@163.com
  5. @Date : 2022-07-27 15:23:24
  6. @Brief :
  7. """
  8. import cv2
  9. import numpy as np
  10. from typing import Callable
  11. from pybaseutils import image_utils
  12. class DrawImageMouse( object):
  13. """使用鼠标绘图"""
  14. def __init__( self, max_point=-1, line_color=(0, 0, 255), text_color=(255, 0, 0), thickness=2):
  15. """
  16. :param max_point: 最多绘图的点数,超过后将绘制无效;默认-1表示无限制
  17. :param line_color: 线条的颜色
  18. :param text_color: 文本的颜色
  19. :param thickness: 线条粗细
  20. """
  21. self.max_point = max_point
  22. self.line_color = line_color
  23. self.text_color = text_color
  24. self.focus_color = ( 0, 255, 0) # 鼠标焦点的颜色
  25. self.thickness = thickness
  26. self.key = - 1 # 键盘值
  27. self.orig = None # 原始图像
  28. self.last = None # 上一帧
  29. self. next = None # 下一帧或当前帧
  30. self.polygons = np.zeros(shape=( 0, 2), dtype=np.int32) # 鼠标绘制点集合
  31. def clear( self):
  32. self.key = - 1
  33. self.polygons = np.zeros(shape=( 0, 2), dtype=np.int32)
  34. if self.orig is not None: self.last = self.orig.copy()
  35. if self.orig is not None: self. next = self.orig.copy()
  36. def get_polygons( self):
  37. """获得多边形数据"""
  38. return self.polygons
  39. def task( self, image, callback: Callable, winname="winname"):
  40. """
  41. 鼠标监听任务
  42. :param image: 图像
  43. :param callback: 鼠标回调函数
  44. :param winname: 窗口名称
  45. :return:
  46. """
  47. self.orig = image.copy()
  48. self.last = image.copy()
  49. self. next = image.copy()
  50. cv2.namedWindow(winname, flags=cv2.WINDOW_NORMAL)
  51. cv2.setMouseCallback(winname, callback, param={ "winname": winname})
  52. while True:
  53. self.key = self.show_image(winname, self. next, delay= 25)
  54. print( "key={}". format(self.key))
  55. if (self.key == 13 or self.key == 32) and len(self.polygons) > 0: # 按空格32和回车键13表示完成绘制
  56. break
  57. elif self.key == 27: # 按ESC退出程序
  58. exit( 0)
  59. elif self.key == 99: # 按键盘c重新绘制
  60. self.clear()
  61. # cv2.destroyAllWindows()
  62. cv2.setMouseCallback(winname, self.event_default)
  63. def event_default( self, event, x, y, flags, param):
  64. pass
  65. def event_draw_rectangle( self, event, x, y, flags, param):
  66. """绘制矩形框"""
  67. if len(self.polygons) == 0: self.polygons = np.zeros(shape=( 2, 2), dtype=np.int32) # 多边形轮廓
  68. point = (x, y)
  69. if event == cv2.EVENT_LBUTTONDOWN: # 左键点击,则在原图打点
  70. print( "1-EVENT_LBUTTONDOWN")
  71. self. next = self.last.copy()
  72. self.polygons[ 0, :] = point
  73. cv2.circle(self. next, point, radius= 5, color=self.focus_color, thickness=self.thickness)
  74. elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON): # 按住左键拖曳,画框
  75. print( "2-EVENT_FLAG_LBUTTON")
  76. self. next = self.last.copy()
  77. cv2.circle(self. next, self.polygons[ 0, :], radius= 4, color=self.focus_color, thickness=self.thickness)
  78. cv2.circle(self. next, point, radius= 4, color=self.focus_color, thickness=self.thickness)
  79. cv2.rectangle(self. next, self.polygons[ 0, :], point, color=self.line_color, thickness=self.thickness)
  80. elif event == cv2.EVENT_LBUTTONUP: # 左键释放,显示
  81. print( "3-EVENT_LBUTTONUP")
  82. self. next = self.last.copy()
  83. self.polygons[ 1, :] = point
  84. cv2.rectangle(self. next, self.polygons[ 0, :], point, color=self.line_color, thickness=self.thickness)
  85. print( "location:{},have:{}". format(point, len(self.polygons)))
  86. def event_draw_polygon( self, event, x, y, flags, param):
  87. """绘制多边形"""
  88. exceed = self.max_point > 0 and len(self.polygons) >= self.max_point
  89. self. next = self.last.copy()
  90. point = (x, y)
  91. text = str( len(self.polygons))
  92. if event == cv2.EVENT_LBUTTONDOWN: # 左键点击,则在原图打点
  93. print( "1-EVENT_LBUTTONDOWN")
  94. cv2.circle(self. next, point, radius= 5, color=self.focus_color, thickness=self.thickness)
  95. cv2.putText(self. next, text, point, cv2.FONT_HERSHEY_SIMPLEX, 0.5, self.text_color, 2)
  96. if len(self.polygons) > 0:
  97. cv2.line(self. next, self.polygons[- 1, :], point, color=self.line_color, thickness=self.thickness)
  98. if not exceed:
  99. self.last = self. next
  100. self.polygons = np.concatenate([self.polygons, np.array(point).reshape( 1, 2)])
  101. else:
  102. cv2.circle(self. next, point, radius= 5, color=self.focus_color, thickness=self.thickness)
  103. if len(self.polygons) > 0:
  104. cv2.line(self. next, self.polygons[- 1, :], point, color=self.line_color, thickness=self.thickness)
  105. print( "location:{},have:{}". format(point, len(self.polygons)))
  106. @staticmethod
  107. def polygons2box( polygons):
  108. """将多边形转换为矩形框"""
  109. xmin = min(polygons[:, 0])
  110. ymin = min(polygons[:, 1])
  111. xmax = max(polygons[:, 0])
  112. ymax = max(polygons[:, 1])
  113. return [xmin, ymin, xmax, ymax]
  114. def show_image( self, title, image, delay=5):
  115. """显示图像"""
  116. cv2.imshow(title, image)
  117. key = cv2.waitKey(delay=delay) if delay >= 0 else - 1
  118. return key
  119. def draw_image_rectangle_on_mouse( self, image, winname="draw_rectangle"):
  120. """
  121. 获得鼠标绘制的矩形框box=[xmin,ymin,xmax,ymax]
  122. :param image:
  123. :param winname: 窗口名称
  124. :return: box is[xmin,ymin,xmax,ymax]
  125. """
  126. self.task(image, callback=self.event_draw_rectangle, winname=winname)
  127. polygons = self.get_polygons()
  128. box = self.polygons2box(polygons)
  129. return box
  130. def draw_image_polygon_on_mouse( self, image, winname="draw_polygon"):
  131. """
  132. 获得鼠标绘制的矩形框box=[xmin,ymin,xmax,ymax]
  133. :param image:
  134. :param winname: 窗口名称
  135. :return: polygons is (N,2)
  136. """
  137. self.task(image, callback=self.event_draw_polygon, winname=winname)
  138. polygons = self.get_polygons()
  139. return polygons
  140. def draw_image_rectangle_on_mouse_example( image_file, winname="draw_rectangle"):
  141. """
  142. 获得鼠标绘制的矩形框
  143. :param image_file:
  144. :param winname: 窗口名称
  145. :return: box=[xmin,ymin,xmax,ymax]
  146. """
  147. image = cv2.imread(image_file)
  148. # 通过鼠标绘制矩形框rect
  149. mouse = DrawImageMouse()
  150. box = mouse.draw_image_rectangle_on_mouse(image, winname=winname)
  151. # 裁剪矩形区域,并绘制最终的矩形框
  152. roi: np.ndarray = image[box[ 1]:box[ 3], box[ 0]:box[ 2]]
  153. if roi.size > 0: mouse.show_image( "Image ROI", roi)
  154. image = image_utils.draw_image_boxes(image, [box], color=( 0, 0, 255), thickness= 2)
  155. mouse.show_image(winname, image, delay= 0)
  156. return box
  157. def draw_image_polygon_on_mouse_example( image_file, winname="draw_polygon"):
  158. """
  159. 获得鼠标绘制的多边形
  160. :param image_file:
  161. :param winname: 窗口名称
  162. :return: polygons is (N,2)
  163. """
  164. image = cv2.imread(image_file)
  165. # 通过鼠标绘制多边形
  166. mouse = DrawImageMouse(max_point=- 1)
  167. polygons = mouse.draw_image_polygon_on_mouse(image, winname=winname)
  168. image = image_utils.draw_image_points_lines(image, polygons, thickness= 2)
  169. mouse.show_image(winname, image, delay= 0)
  170. return polygons
  171. if __name__ == '__main__':
  172. image_file = "../data/test.png"
  173. # 绘制矩形框
  174. out = draw_image_rectangle_on_mouse_example(image_file)
  175. # 绘制多边形
  176. out = draw_image_polygon_on_mouse_example(image_file)
  177. print(out)
 绘制矩形框 绘制多边形


转载:https://blog.csdn.net/guyuealian/article/details/128019461
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场