新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

PythonOpenCV实现鼠标绘制矩形框和多边形-创新互联

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:传给回调函数的参数

(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函数调用的参数。

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

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


新闻名称:PythonOpenCV实现鼠标绘制矩形框和多边形-创新互联
文章网址:http://wjwzjz.com/article/csgecg.html
在线咨询
服务热线
服务热线:028-86922220
TOP