俺最近在做个不正式的测试程序,实在不想折腾MFC。List、Control等等控件,还有种种消息响应处理,实在有点繁琐。
于是就用OpenCV自带的消息处理程序试了一下,马马虎虎能够解决问题,但还要用到命令行输入,非“傻瓜”程序。
OpenCV 键盘、鼠标响应函数都比较简单。
键盘:
cvWaitKey()
你没有看错,这个居家旅行人手必备的货,原型居然是带返回值的。用它就能相应键盘输入!
鼠标:
cvSetMouseCallBack()
能响应鼠标移动,左中右点击放开。还带记录此时鼠标各键的状态
不多解释,大家在例程里面体会用法吧:
#include "cv.h" #include "cxcore.h" #include "highgui.h" CvPoint point1; CvPoint point2; void onMouse(int event, int x, int y, int flags, void* param ) { IplImage* img = (IplImage*) param; switch(event) { case CV_EVENT_LBUTTONDOWN: { printf("( %d, %d) ",x,y); printf("The Event is : %d ",event); printf("The flags is : %d \n",flags); point1.x = x; point1.y = y; } break; case CV_EVENT_LBUTTONUP: { printf("( %d, %d) ",x,y); printf("The Event is : %d ",event); printf("The flags is : %d \n",flags); point2.x = x; point2.y = y; cvRectangle(img, point1, point2, cvScalar(0, 0, 0), 2, 8, 0); cvShowImage("disp", img); } break; } } int _tmain(int argc, _TCHAR* argv[]) { char * filename = "D:\\pictures\\60078_a.jpg"; IplImage* img0; img0 = cvLoadImage(filename, -1); cvNamedWindow("disp",1); cvSetMouseCallback("disp", onMouse, (void*)img0); while(true) { cvShowImage("disp",img0); //按下Esc if(cvWaitKey(100)==27) { break; } } cvReleaseImage(&img0); cvDestroyWindow("disp"); return 0; }
最后,用kana Uemura测试的结果。ピカピカ~