• 在窗口中绘画


    // 鼠标绘图
    #include <opencv2/opencv.hpp>
    using namespace std;
    //#pragma comment(linker, "/subsystem:"windows" /entry:"mainCRTStartup"")
    const char *pstrWindowsMouseDrawTitle = "鼠标绘图窗口";
    
    /**    
    鼠标事件响应采用回调函数的方式处理,即为了响应鼠标点击事件,首先必须创建一个回调函数,使鼠标点击事件发生时,opencv可以调用这个函数
    创建这个回调函数后,需要在opencv中注册这个函数(用cvSetMouseCallback(回调函数需要注册的窗口,要调用的回调函数,要传递的额外信息)来注册),以便特定窗口被触发鼠标事件后,opencv可以正确调用这个函数。
    回调函数的格式:void CvMouseCallback(int event(鼠标事件类型{}),int x(鼠标事件发生时的x坐标),int y(鼠标事件发生时的y坐标), int flags(鼠标事件的标志{}),void* param(用于传递额外的参数信息给前面所创建的回调函数的void * param));
    event鼠标事件类型:
        CV_EVENT_MOUSEMOVE(0)鼠标滑动
        CV_EVENT_LBUTTONDOWN(1)左键点击
        CV_EVENT_RBUTTONDOWN(2)右键点击
        CV_EVENT_MBUTTONDOWN(3)中键点击
        CV_EVENT_LBUTTONUP(4)左键放开
        CV_EVENT_RBUTTONUP(5)右键放开
        CV_EVENT_MBUTTONUP(6)中键放开
        CV_EVENT_LBUTTONDBLCLK(7)左键双击
        CV_EVENT_RBUTTONDBLCLK(8)右键双击
        CV_EVENT_MLBUTTONDBLCLK(9)中键双击
    flags鼠标事件标记
        CV_EVENT_FLAG_LBUTTON(1)左键拖拽
        CV_EVENT_FLAG_RBUTTON(2)右键拖拽
        CV_EVENT_FLAG_MBUTTON(3)中键拖拽
        CV_EVENT_FLAG_CTRLKEY(8)[8-15]按Ctrl键不放
        CV_EVENT_FLAG_SHIFTKEY(16)[16-31]按shift键不放
        CV_EVENT_FLAG_ALTKEY(32)[32-39]按Alt键不放
    **/
    
    
    // 鼠标消息的回调函数
    void on_mouse(int event, int x, int y, int flags, void* param)
    {
        static bool s_bMouseLButtonDown = false;
        static CvPoint s_cvPrePoint = cvPoint(0, 0);//坐标二维点
    
        switch (event)//鼠标消息类型
        {
        case CV_EVENT_LBUTTONDOWN:
            s_bMouseLButtonDown = true;
            s_cvPrePoint = cvPoint(x, y);
            break;
    
        case  CV_EVENT_LBUTTONUP:
            s_bMouseLButtonDown = false;
            break;
    
        case CV_EVENT_MOUSEMOVE:
            if (s_bMouseLButtonDown)
            {
                CvPoint cvCurrPoint = cvPoint(x, y);
                //void cvLine( CvArr* img(图像), CvPoint pt1(线段的第一个端点), CvPoint pt2(线段的第二个端点), CvScalar color(线段颜色), int thickness=1(线段粗细), int line_type=8(线段类型), int shift=0(坐标点的小数位数) );
                cvLine((IplImage*)param, s_cvPrePoint, cvCurrPoint, CV_RGB(255, 255, 255), 3);//这里的 CV_RGB()用来定义画笔的颜色
                s_cvPrePoint = cvCurrPoint;
                cvShowImage(pstrWindowsMouseDrawTitle, (IplImage*)param);
            }
            break;
        }
    }
    int main()
    {    
        const int MAX_WIDTH = 400, MAX_HEIGHT = 400;//设定画布大小
        const char *pstrSaveImageName = "MouseDraw.jpg";//要保存的这幅画的文件名
    
        IplImage *pSrcImage =  cvCreateImage(cvSize(MAX_WIDTH, MAX_HEIGHT), IPL_DEPTH_8U, 3);//创建一副空的图像
        cvSet(pSrcImage, CV_RGB(25, 100, 125)); //可以用cvSet()将图像填充成白色(255,255,255),或是其他的颜色,只要改变CV_RGB(, ,)的三个参数即可。
        //显示这幅空白图片
        cvNamedWindow(pstrWindowsMouseDrawTitle, CV_WINDOW_AUTOSIZE);//cvNamedWindow(视窗名称,0(视窗大小可以手动调整)或1(又可以用CV_WINDOW_AUTOSIZE来代替,表示视窗大小随载入图片的大小而自动调整并且不能手动调整))
        cvShowImage(pstrWindowsMouseDrawTitle, pSrcImage);//cvShowImage(指向显示图像的窗口,指向要显示的图像);
    
    
    
    
        //实现注册的函数
        cvSetMouseCallback(pstrWindowsMouseDrawTitle, on_mouse, (void*)pSrcImage);
    
        int c;
        do{
            c = cvWaitKey(0);//cvWaitKey(0)表示opencv无期限的等待,直到用户触发一个按键。cvWaitKey(100)表示等待100ms
            switch ((char)c)
            {
            case 'r'://‘r’键可以重新绘图【在无期限的等待后,用户触发了'r'键,则执行这个case操作】
                cvSet(pSrcImage, CV_RGB(255, 255, 255));
                cvShowImage(pstrWindowsMouseDrawTitle, pSrcImage);
                break;
    
            case 's'://按下‘s’键则保存图片到磁盘中
                cvSaveImage(pstrSaveImageName, pSrcImage);//cvSaveImage(将要保存的图片的名称,要存储的图像的数据),它只能存储8位单通道或是8位3个通道格式的图像
                break;
            }
        }while (c > 0 && c != 27);//Esc的ASCII=27
        //释放空间
        cvDestroyWindow(pstrWindowsMouseDrawTitle);
        cvReleaseImage(&pSrcImage);
        return 0;
    } 

    参考博文:http://blog.csdn.net/morewindows/article/details/8426283。

  • 相关阅读:
    在没有备份的情况下重新创建丢失的数据文件 (Doc ID 1149946.1)
    How to recover a skipped tablespace after an incomplete recovery? (Doc ID 1561645.1)
    RMAN DUPLICATE DATABASE with SET NEWNAME failed: RMAN-05501 RMAN-05517 (Doc ID 387093.1)
    Rman Enhancements(增强) In Oracle 11g. (Doc ID 1115423.1)
    Resolving RMAN-06023 or RMAN-06025 (Doc ID 2038119.1)
    Checklist for an RMAN Restore (Doc ID 1554636.1)
    Recover With Until Time fails With RMAN-20207 When Time Is Before Last RESETLOGS (Doc ID 159405.1)
    ODA: After Apply ODA 12.2.1.2.0 Patch, Unable to Create TableSpace Due to [ORA-15001: diskgroup "DATA" does not exist or is not mounted | ORA-15040: diskgroup is incomplete] (Doc ID 2375553.1)
    How to restore and recover a database from an RMAN backup. (Doc ID 881395.1)
    How To Restore Rman Backups On A Different Node When The Directory Structures Are Different (Doc ID 419137.1)
  • 原文地址:https://www.cnblogs.com/beihaidao/p/4870105.html
Copyright © 2020-2023  润新知