zoukankan      html  css  js  c++  java
  • opencv鼠标事件

    #include <opencv2opencv.hpp>
    
    using namespace cv;
    
    struct mouse_para
    {
    	cv::Mat org;
    	cv::Mat img;
    
    	std::string winName = "";
    
    	// todo: you can add your own members here.
    };
    
    
    void on_mouse(int event, int x, int y, int flags, void *_ustc) // event鼠标事件代号,x,y鼠标坐标,flags拖拽和键盘操作的代号  
    {
    	mouse_para *ustc = static_cast<mouse_para*>(_ustc);
    
    	static Point pre_pt = (-1, -1);//初始坐标  
    	static Point cur_pt = (-1, -1);//实时坐标  
    
    	char temp[16];
    	if (event == CV_EVENT_LBUTTONDOWN)
    	{
    		std::cout << "Left buttom down !" << std::endl;
    		// todo: you can add your own code here.
    	}
    	else if (event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))//左键没有按下的情况下鼠标移动的处理函数  
    	{
    		std::cout << "(" << x << ", " << y << ")" << std::endl;
    		// todo: you can add your own code here.
    	}
    	else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))//左键按下时,鼠标移动
    	{
    		std::cout << "Left buttom down and mouse move!" << std::endl;
    		// todo: you can add your own code here.
    	}
    	else if (event == CV_EVENT_LBUTTONUP)//左键松开
    	{
    		std::cout << "Left buttom up!" << std::endl;
    		// todo: you can add your own code here.
    	}
    
    	//ustc->org.copyTo(ustc->img);
    	//imshow("img", ustc->img);
    
    }
    
    
    int main()
    {
    	mouse_para mp;
    	mp.org = cv::Mat::ones(500, 500, CV_8UC1);
    	mp.org.copyTo(mp.img);
    	mp.winName = "img";
    
    	namedWindow(mp.winName);//定义一个img窗口  
    	setMouseCallback(mp.winName, on_mouse, &mp);//调用回调函数  
    	
    	
    	for (int i=0; i<255;++i)
    	{
    		i = i % 250;
    		mp.org = i*cv::Mat::ones(500,500, CV_8UC1);
    		mp.org.copyTo(mp.img);
    
    		imshow(mp.winName, mp.img);
    		cv::waitKey(10);
    	}
    	
    
    	return 0;
    }
    

      

  • 相关阅读:
    python-面向对象-类
    python- collections-time-datetime
    python-json-pickle-shelve-random-os-sys-hashlib
    python-递归函数
    python-迭代器
    python-内置函数
    python-易错问题
    python-闭包函数和装饰器
    pytho-函数作用域
    List 拆分集合与 读写XML配置文件
  • 原文地址:https://www.cnblogs.com/alexYuin/p/9232481.html
Copyright © 2011-2022 走看看