opencv MFC中的鼠标响应函数

来源:互联网 发布:数据可视化d3 编辑:程序博客网 时间:2024/05/01 11:00

方法一:

bool check_line_state=false;IplImage* imgshow;void on_mouse4(int event, int x,int y,int flags,void* param){int thickness=1;CvPoint p1,p2;if(event==CV_EVENT_LBUTTONDOWN){ROI_rect.x=x;ROI_rect.y=y;check_line_state=true;}else if(check_line_state&&event==CV_EVENT_MOUSEMOVE){p1=cvPoint(ROI_rect.x,ROI_rect.y);p2=cvPoint(x,y);cvLine(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);}}void CCVMFCView::OnDemarcateLine(){imageClone(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:


方法二:

只能画一条直线

bool check_line_state=false;IplImage* imgshow;void on_mouse4(int event, int x,int y,int flags,void* param){int thickness=1;if(event==CV_EVENT_LBUTTONDOWN){ROI_rect.x=x;ROI_rect.y=y;check_line_state=true;}else if(check_line_state&&event==CV_EVENT_MOUSEMOVE){cvCopy(srcimg,imgshow);pt1=cvPoint(ROI_rect.x,ROI_rect.y);pt2=cvPoint(x,y);cvLine(imgshow,pt1,pt2,CV_RGB(0,255,150),thickness,CV_AA,0);cvShowImage("image",imgshow);cvWaitKey(20);}else if(check_line_state&&event==CV_EVENT_LBUTTONUP){check_line_state=false;cvWaitKey(20);}}void CCVMFCView::OnDemarcateLine(){imageClone(workImg,&srcimg);cvFlip(srcimg);imageClone(srcimg,&imgshow);pt1.x=pt1.y=pt2.x=pt2.y=ROI_rect.x=ROI_rect.y=0;cvNamedWindow("image",CV_WINDOW_AUTOSIZE);cvShowImage("image",imgshow);cvSetMouseCallback("image",on_mouse4);cvWaitKey();cvDestroyWindow("image");}

效果图:


方法三:

可以画多条直线,但是划线的时候看不见线

void on_mouse4(int event, int x,int y,int flags,void* param){int thickness=1;if(event==CV_EVENT_LBUTTONDOWN){ROI_rect.x=x;ROI_rect.y=y;check_line_state=true;}else if(check_line_state&&event==CV_EVENT_MOUSEMOVE){pt1=cvPoint(ROI_rect.x,ROI_rect.y);pt2=cvPoint(x,y);}else if(check_line_state&&event==CV_EVENT_LBUTTONUP){check_line_state=false;//cvWaitKey(20);cvLine(imgshow,pt1,pt2,CV_RGB(0,255,150),thickness,CV_AA,0);cvShowImage("image",imgshow);}}void CCVMFCView::OnDemarcateLine(){imageClone(workImg,&imgshow);cvFlip(imgshow);pt1.x=pt1.y=pt2.x=pt2.y=ROI_rect.x=ROI_rect.y=0;cvNamedWindow("image",CV_WINDOW_AUTOSIZE);cvShowImage("image",imgshow);cvSetMouseCallback("image",on_mouse4);cvWaitKey();cvDestroyWindow("image");}

效果图3: