- #include "cv.h"
- #include "highgui.h"
- bool check_line_state=false;
- IplImage* workImg;
- IplImage* imgshow;
- CvRect ROI_rect;
- void on_mouse4(int event, int x,int y,int flags,void* param)
- {
- int thickness=2;
- CvPoint p1,p2;
- if(event==CV_EVENT_LBUTTONDOWN)
- {
- //当前鼠标位置(x,y)
- ROI_rect.x=x;
- ROI_rect.y=y;
- check_line_state=true;
- }
- else if(check_line_state&&event==CV_EVENT_MOUSEMOVE)//鼠标状态
- {
- cvCopy(workImg,imgshow);//这一步很重要,不断更新显示图像,不然画出的图像如图2所示
- p1=cvPoint(ROI_rect.x,ROI_rect.y);
- p2=cvPoint(x,y);
- cvRectangle(imgshow,p1,p2,CV_RGB(0,255,150),thickness,CV_AA,0);
- cvShowImage("image",imgshow);
- }
- else if(check_line_state&&event==CV_EVENT_LBUTTONUP)
- {
- check_line_state=false;
- cvWaitKey(20);
- }
- }
- int main()
- {
- workImg=cvLoadImage("D:/Demo.jpg",1);
- imgshow=cvCreateImage(cvSize(workImg->width,workImg->height),workImg->depth,workImg->nChannels);
- cvCopy(workImg,imgshow);
- //cvFlip(imgshow);
- ROI_rect.x=ROI_rect.y=0;
- cvNamedWindow("image",CV_WINDOW_AUTOSIZE);
- cvShowImage("image",imgshow);
- cvSetMouseCallback("image",on_mouse4);
- cvWaitKey();
- cvDestroyWindow("image");
- }
图1: 图2:
from: http://blog.csdn.net/yangtrees/article/details/7573919