[CPP]View PlainCopyprint?
- #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)
- {
- //Current mouse position (x, y)
- Roi_rect.x=x;
- Roi_rect.y=y;
- Check_line_state=true;
- }
- Else if (check_line_state&&event==cv_event_mousemove)//mouse state
- {
- Cvcopy (workimg,imgshow); //This step is important to constantly update the display image, otherwise the picture shown in Figure 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");
- }
Figure 1: Figure 2:
from:http://blog.csdn.net/yangtrees/article/details/7573919
Learning opencv--mouse events (picture frames)