opencv窗口鼠标绘图

// 鼠标绘图  
#include "stdafx.h"
#include  
#include
using namespace std;  
using namespace cv;
const char *srcWindowsName = "鼠标绘图程序";  
const char *saveWindowsName = "鼠标绘图图像";  
//CvPoint lastprePoint = cvPoint(0,0);  
//CvPoint lastcurrPoint = cvPoint(0,0);  
int colornum = 0;  
int resize = 0;  
//CvMat *sizematrix = {{100, 100, 100}};  
//int thickness = 0;  
//鼠标响应事件  
Mat srcImage;


void on_mouse(int event, int x, int y, int flag, void *param)  
{  

Mat srcImage1=*(Mat*)param;
    static bool press = false;  
    static CvPoint prePoint = cvPoint(0, 0);  
    switch(event)  
    {  
    case CV_EVENT_LBUTTONDOWN:  
        press = true;  
        prePoint = cvPoint(x, y);  
        //lastprePoint= prePoint;  
        break;  
    case CV_EVENT_LBUTTONUP:  
        //lastcurrPoint = cvPoint(x, y);  
        press = false;  
        break;  
    //case CV_EVENT_MBUTTONDOWN:  
    //  cvLine((IplImage*)param, lastprePoint, lastcurrPoint, CV_RGB(255,255,255), 20);  
    //  cvShowImage(srcWindowsName, (IplImage*)param);  
    //  break;  
    case CV_EVENT_MOUSEMOVE:  
        if(press)  
        {  
            CvPoint currPoint = cvPoint(x, y);  
            if(colornum == 0)  
{ line(srcImage1,prePoint,currPoint,CV_RGB(0,0,0),20,8,0);
  imshow(srcWindowsName, srcImage1); 
} //  cvLine((IplImage*)param, prePoint, currPoint, CV_RGB(0,0,0), 20);//画线函数,指定了线条的颜色,起始点,厚度等    hei  
            else if(colornum == 1)  
               line(srcImage,prePoint,currPoint,CV_RGB(0,0,0),20,8,0);// cvLine((IplImage*)param, prePoint, currPoint, CV_RGB(255,255,255), 20);//画线函数,指定了线条的颜色,起始点,厚度等 bai  
            else if(colornum == 2)  
               line(srcImage,prePoint,currPoint,CV_RGB(0,0,0),20,8,0);
// cvLine((IplImage*)param, prePoint, currPoint, CV_RGB(0,255,255), 20);//画线函数,指定了线条的颜色,起始点,厚度等    lv  
            prePoint = currPoint;  
           //cvRectangle((IplImage*)param, cvPoint(300, 300), cvPoint(325, 325), CV_RGB(0,255,255), CV_FILLED);  
         // cvShowImage(srcWindowsName, (IplImage*)param);  
        }  
        break;  
    }  
}  
int main()  
{  
    const int WIDTH = 700, HEIGHT = 500;  
    //创建图像  
    srcImage = cvCreateImage(cvSize(WIDTH, HEIGHT), IPL_DEPTH_8U, 3);  
    //cvSet(srcImage, CV_RGB(255,255,255));//图像背景颜色  
srcImage.setTo(255);
    //创建窗口  
    cvNamedWindow(srcWindowsName, CV_WINDOW_AUTOSIZE);  
    rectangle(srcImage, cvPoint(300, 300), cvPoint(325, 325), CV_RGB(0, 255, 255), CV_FILLED);  
    //显示图像  
imshow(srcWindowsName, srcImage);  
    //设置鼠标回调函数  
   // cvSetMouseCallback(srcWindowsName, on_mouse, 0);  
setMouseCallback(srcWindowsName,on_mouse,(void*)&srcImage);//---------传递的参数为Mat类型;
    //键盘响应事件  
    while(1)  
    {  
        char c = cvWaitKey(33);  
        if(c == 27) break;  
        switch(char(c))  
        {  
        case 'r':  
            //cvSet(srcImage, CV_RGB(255,255,255));//设置背景           
            //cvShowImage(srcWindowsName, srcImage);  
            break;  
        case 'g':  
            colornum = 2;  
            break;  
        case 'b':  
            colornum = 0;  
            break;  
        case 'w':  
            colornum = 1;  
            break;  
        case 's':  
           // cvSaveImage(srcWindowsName, srcImage);  
            break;  
        }  
    }  
    //销毁窗口,释放资源  
    cvDestroyWindow(srcWindowsName);  
//    cvReleaseImage(&srcImage);  
    return 0;  
}  

你可能感兴趣的:(opencv)