介绍该文致力于处理神经网络中的过度拟合。过度拟合将是你主要担心的问题,因为你仅使用 2000 个数据样本训练模型。存在一些有助于克服过度拟合的方法,即 dropout 和权重衰减(L2 正则化)。我们将讨论数据增强,这是独有的,在使用深度学习模型解释图像时,数据增强在任何地方都会用到。数据增强学习示例不足会阻止你训练可以泛化到新数据的模型,从而导致过度拟合。如果你有无限的数据,你的模型将暴露于当前数据分布的所有特征,从而防止过度拟合。通过增加具有不同随机变化的样本来产生逼真的图像,数据增强使用现有的训练样本来生成更多的训练数据。你的模型不应在训练期间两次查看同一图像。这使模型更加通用并暴露了数...... Last article READ

Python OpenCV实现鼠标绘制矩形框和多边形_AI吃大瓜的博客

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函数说明

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

Python OpenCV实现鼠标绘制矩形框和多边形_AI吃大瓜的博客

(2)回调函数onMouse说明

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

 Python OpenCV实现鼠标绘制矩形框和多边形_AI吃大瓜的博客

(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)绘制矩形框

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

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

(2)绘制多边形

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

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

(3)键盘控制

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

  1. 按空格和回车键表示完成绘制
  2. 按ESC退出程序
  3. 按键盘c重新绘制
    def task(self, image, callback: Callable, winname="winname"):         """         鼠标监听任务         :param image: 图像         :param callback: 鼠标回调函数         :param winname: 窗口名称         :return:         """         self.orig = image.copy()         self.last = image.copy()         self.next = image.copy()         cv2.namedWindow(winname, flags=cv2.WINDOW_NORMAL)         cv2.setMouseCallback(winname, callback, param={"winname": winname})         while True:             self.key = self.show_image(winname, self.next, delay=25)             print("key={}".format(self.key))             if (self.key == 13 or self.key == 32) and len(self.polygons) > 0:  # 按空格32和回车键13表示完成绘制                 break             elif self.key == 27:  # ESC退出程序                 exit(0)             elif self.key == 99:  # 按键盘c重新绘制                 self.clear()         # 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 

# -*-coding: utf-8 -*- """     @Author : panjq     @E-mail : pan_jinquan@163.com     @Date   : 2022-07-27 15:23:24     @Brief  : """ import cv2 import numpy as np from typing import Callable from pybaseutils import image_utils   class DrawImageMouse(object):     """使用鼠标绘图"""      def __init__(self, max_point=-1, line_color=(0, 0, 255), text_color=(255, 0, 0), thickness=2):         """         :param max_point: 最多绘图的点数,超过后将绘制无效;默认-1表示无限制         :param line_color: 线条的颜色         :param text_color: 文本的颜色         :param thickness: 线条粗细         """         self.max_point = max_point         self.line_color = line_color         self.text_color = text_color         self.focus_color = (0, 255, 0)  # 鼠标焦点的颜色         self.thickness = thickness         self.key = -1  # 键盘值         self.orig = None  # 原始图像         self.last = None  # 上一帧         self.next = None  # 下一帧或当前帧         self.polygons = np.zeros(shape=(0, 2), dtype=np.int32)  # 鼠标绘制点集合      def clear(self):         self.key = -1         self.polygons = np.zeros(shape=(0, 2), dtype=np.int32)         if self.orig is not None: self.last = self.orig.copy()         if self.orig is not None: self.next = self.orig.copy()      def get_polygons(self):         """获得多边形数据"""         return self.polygons      def task(self, image, callback: Callable, winname="winname"):         """         鼠标监听任务         :param image: 图像         :param callback: 鼠标回调函数         :param winname: 窗口名称         :return:         """         self.orig = image.copy()         self.last = image.copy()         self.next = image.copy()         cv2.namedWindow(winname, flags=cv2.WINDOW_NORMAL)         cv2.setMouseCallback(winname, callback, param={"winname": winname})         while True:             self.key = self.show_image(winname, self.next, delay=25)             print("key={}".format(self.key))             if (self.key == 13 or self.key == 32) and len(self.polygons) > 0:  # 按空格32和回车键13表示完成绘制                 break             elif self.key == 27:  # 按ESC退出程序                 exit(0)             elif self.key == 99:  # 按键盘c重新绘制                 self.clear()         # cv2.destroyAllWindows()         cv2.setMouseCallback(winname, self.event_default)      def event_default(self, event, x, y, flags, param):         pass      def event_draw_rectangle(self, event, x, y, flags, param):         """绘制矩形框"""         if len(self.polygons) == 0: self.polygons = np.zeros(shape=(2, 2), dtype=np.int32)  # 多边形轮廓         point = (x, y)         if event == cv2.EVENT_LBUTTONDOWN:  # 左键点击,则在原图打点             print("1-EVENT_LBUTTONDOWN")             self.next = self.last.copy()             self.polygons[0, :] = point             cv2.circle(self.next, point, radius=5, color=self.focus_color, thickness=self.thickness)         elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON):  # 按住左键拖曳,画框             print("2-EVENT_FLAG_LBUTTON")             self.next = self.last.copy()             cv2.circle(self.next, self.polygons[0, :], radius=4, color=self.focus_color, thickness=self.thickness)             cv2.circle(self.next, point, radius=4, color=self.focus_color, thickness=self.thickness)             cv2.rectangle(self.next, self.polygons[0, :], point, color=self.line_color, thickness=self.thickness)         elif event == cv2.EVENT_LBUTTONUP:  # 左键释放,显示             print("3-EVENT_LBUTTONUP")             self.next = self.last.copy()             self.polygons[1, :] = point             cv2.rectangle(self.next, self.polygons[0, :], point, color=self.line_color, thickness=self.thickness)         print("location:{},have:{}".format(point, len(self.polygons)))      def event_draw_polygon(self, event, x, y, flags, param):         """绘制多边形"""         exceed = self.max_point > 0 and len(self.polygons) >= self.max_point         self.next = self.last.copy()         point = (x, y)         text = str(len(self.polygons))         if event == cv2.EVENT_LBUTTONDOWN:  # 左键点击,则在原图打点             print("1-EVENT_LBUTTONDOWN")             cv2.circle(self.next, point, radius=5, color=self.focus_color, thickness=self.thickness)             cv2.putText(self.next, text, point, cv2.FONT_HERSHEY_SIMPLEX, 0.5, self.text_color, 2)             if len(self.polygons) > 0:                 cv2.line(self.next, self.polygons[-1, :], point, color=self.line_color, thickness=self.thickness)             if not exceed:                 self.last = self.next                 self.polygons = np.concatenate([self.polygons, np.array(point).reshape(1, 2)])         else:             cv2.circle(self.next, point, radius=5, color=self.focus_color, thickness=self.thickness)             if len(self.polygons) > 0:                 cv2.line(self.next, self.polygons[-1, :], point, color=self.line_color, thickness=self.thickness)         print("location:{},have:{}".format(point, len(self.polygons)))      @staticmethod     def polygons2box(polygons):         """将多边形转换为矩形框"""         xmin = min(polygons[:, 0])         ymin = min(polygons[:, 1])         xmax = max(polygons[:, 0])         ymax = max(polygons[:, 1])         return [xmin, ymin, xmax, ymax]      def show_image(self, title, image, delay=5):         """显示图像"""         cv2.imshow(title, image)         key = cv2.waitKey(delay=delay) if delay >= 0 else -1         return key      def draw_image_rectangle_on_mouse(self, image, winname="draw_rectangle"):         """         获得鼠标绘制的矩形框box=[xmin,ymin,xmax,ymax]         :param image:         :param winname: 窗口名称         :return: box is[xmin,ymin,xmax,ymax]         """         self.task(image, callback=self.event_draw_rectangle, winname=winname)         polygons = self.get_polygons()         box = self.polygons2box(polygons)         return box      def draw_image_polygon_on_mouse(self, image, winname="draw_polygon"):         """         获得鼠标绘制的矩形框box=[xmin,ymin,xmax,ymax]         :param image:         :param winname: 窗口名称         :return: polygons is (N,2)         """         self.task(image, callback=self.event_draw_polygon, winname=winname)         polygons = self.get_polygons()         return polygons   def draw_image_rectangle_on_mouse_example(image_file, winname="draw_rectangle"):     """     获得鼠标绘制的矩形框     :param image_file:     :param winname: 窗口名称     :return: box=[xmin,ymin,xmax,ymax]     """     image = cv2.imread(image_file)     # 通过鼠标绘制矩形框rect     mouse = DrawImageMouse()     box = mouse.draw_image_rectangle_on_mouse(image, winname=winname)     # 裁剪矩形区域,并绘制最终的矩形框     roi: np.ndarray = image[box[1]:box[3], box[0]:box[2]]     if roi.size > 0: mouse.show_image("Image ROI", roi)     image = image_utils.draw_image_boxes(image, [box], color=(0, 0, 255), thickness=2)     mouse.show_image(winname, image, delay=0)     return box   def draw_image_polygon_on_mouse_example(image_file, winname="draw_polygon"):     """     获得鼠标绘制的多边形     :param image_file:     :param winname: 窗口名称     :return: polygons is (N,2)     """     image = cv2.imread(image_file)     # 通过鼠标绘制多边形     mouse = DrawImageMouse(max_point=-1)     polygons = mouse.draw_image_polygon_on_mouse(image, winname=winname)     image = image_utils.draw_image_points_lines(image, polygons, thickness=2)     mouse.show_image(winname, image, delay=0)     return polygons   if __name__ == '__main__':     image_file = "../data/test.png"     # 绘制矩形框     out = draw_image_rectangle_on_mouse_example(image_file)     # 绘制多边形     out = draw_image_polygon_on_mouse_example(image_file)     print(out) 

支付宝支付&内网穿透 一 沙箱环境二 python第三方模块python-alipay-sdk三 python-alipay-sdk二次封装四 支付接口五 内网穿透5.1 cpolar软件5.2 测试支付宝post回调 一 沙箱环境 注册认证沙箱环境:https://openhome.alipay.com/platform/appDaily.htm?tab=info 下载支付宝开放平台密钥工具:https://opendocs.alipay.com/common/02kipk 在开发中心的沙箱应用下设置应用公钥:填入生成的公钥文件中的内......Next article READ