opencv鼠标画矩形

// ConsoleApplication5.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<opencv2\highgui\highgui.hpp>
#include<opencv\cv.h>
#include<opencv2\core\core.hpp>
#include<opencv2\opencv.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include<iostream>
#include "imgproc/imgproc.hpp"
using namespace cv;
using namespace std;

void on_MouseHandle(int evev, int x, int y, int flags, void* param);
void DrawRectangle(Mat& img, Rect box);
bool g_bDrawingBox = false;
Rect g_rectangle;
int main()
{
    string name = "原图";
    Mat qq = imread("D:\\test11.jpg");
    Mat qq1,qq2;
    namedWindow(name);
    setMouseCallback(name, on_MouseHandle, (void*)&qq);//鼠标回调函数
    while (1) 
    {
        qq.copyTo(qq1);//复制源图到临时变量    
        if (g_bDrawingBox)//如果进行绘制的标识符为真时,则进行绘制        
        {
            DrawRectangle(qq1, g_rectangle);
            qq2.create(g_rectangle.size(), qq.type());//分割出图像    
            qq(g_rectangle).copyTo(qq2);
        
        }
        imshow(name, qq1);

        if (qq2.cols!=0)
        {
            imshow("QQ2", qq2);
            qq2.cols = 0;
        }
        
        if (waitKey(10) == 27)
            break;//按下ESC键程序退出    }

    }

    waitKey(0);
    return 0;
}

void on_MouseHandle(int event, int x, int y, int flags, void* param)
{
    Mat& image = *(Mat*)param;    switch (event)
    {        //鼠标移动消息        
    case EVENT_MOUSEMOVE:    
        {            if (g_bDrawingBox)//如果是否进行绘制的标识符为真,则记录下长和框到Rect类型变量中        
        {                g_rectangle.width = x - g_rectangle.x;        
        g_rectangle.height = y - g_rectangle.y;        
        }    
        }    
        break;                //左键按下消息        
    case EVENT_LBUTTONDOWN:    
    {        
        g_bDrawingBox = true;    
        g_rectangle = Rect(x, y, 0, 0);        }    
    break;         //左键抬起消息        
    case EVENT_LBUTTONUP:        
    {            
        g_bDrawingBox = false;//置标识符为false            //对宽和高小于0的处理        
        if (g_rectangle.width < 0)        
        {            
            g_rectangle.x += g_rectangle.width;        
            g_rectangle.width *= -1;    
        }            
        if (g_rectangle.height < 0)    
        {            
            g_rectangle.y += g_rectangle.height;
            g_rectangle.height *= -1;    
        }            //调用函数进行绘制        
        DrawRectangle(image, g_rectangle);    
    }    
    break;     
    }
}

void DrawRectangle(Mat& img, Rect box) 
{
    rectangle(img, box.tl(), box.br(), Scalar(0,255,255)); 
}

    

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xutingjie168

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值