0009-利用setMouseCallback函数实现鼠标对图片窗口的操作!

本程序实现鼠标对图片窗口的操作。
本程序利用setMouseCallback函数将图像窗口与对应的鼠标处理函数映射起来!
在这个程序中
①当鼠标左键在图像上某点点击时,显示这个点的坐标,并在图像上该点处划圆; 
②当鼠标左键没有按下时,鼠标移动时,则会显示鼠标所在点的实时坐标;
③当鼠标左键按下不放并且鼠标移动时,则在图像上划矩形,当鼠标放开后,则会把相应的矩形区域提取出来显示在另一个窗口中。程序比较简单,这里就不用文字说明怎么实现了,大家一看代码便知怎么用了!
代码如下
代码中用到的图像下载链接:http://pan.baidu.com/s/1miSMDmg 密码:rhs5

//opencv版本:OpenCV3.0
//VS版本:VS2013
//Author:qxsf321.net

#include <opencv2/imgproc/imgproc.hpp>    
#include <opencv2/imgproc/types_c.h>   
#include <opencv2/core/core.hpp>          
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <iostream>

using namespace cv;

cv::Mat org, dst, img, tmp;
void on_mouse(int event, int x, int y, int flags, void *ustc)//event鼠标事件代号,x,y鼠标坐标,flags拖拽和键盘操作的代号 


{
        //on_mouse函数的代码请搜索公众号"qxsf321",关注后回复0009即可获取
        //on_mouse函数的代码请搜索公众号"qxsf321",关注后回复0009即可获取
        //on_mouse函数的代码请搜索公众号"qxsf321",关注后回复0009即可获取
}
void main()
{
        org = imread("1.jpg");
        org.copyTo(img);
        org.copyTo(tmp);
        namedWindow("img");//定义一个img窗口  
        setMouseCallback("img", on_mouse, 0);//调用回调函数  
        imshow("img", img);
        cv::waitKey(0);
}


代码运行结果我这里录了个视频,视频在线观看链接

https://v.youku.com/v_show/id_XMjk3MTYyMjM3Ng==.html?spm=a2h3j.8428770.3416059.1
大家在看视频的时候记得选择高清模式哦
不方便在线看的我给大家这个视频的网盘下载链接
链接:http://pan.baidu.com/s/1geYgrlx 密码:db75

猜你喜欢

转载自blog.csdn.net/lehuoziyuan/article/details/84031257