下面是一个简单的例子,检测鼠标左键点击事件和它(X,Y)坐标。
#include "stdafx.h" #include <cv.h> #include <highgui.h> //callback function void mouseEvent(int evt, int x, int y, int flags, void* param){ if(evt==CV_EVENT_LBUTTONDOWN){ printf("%d %d\n",x,y); } } int main() { cvNamedWindow("MyWindow"); //assigning the callback function for mouse events cvSetMouseCallback("MyWindow", mouseEvent, 0); //load and display an image IplImage* img = cvLoadImage("C:/MyPic.jpg"); cvShowImage("MyWindow", img); //wait for key press cvWaitKey(0); //cleaning up cvDestroyWindow("MyWindow"); cvReleaseImage(&img); return 0; }
总结
#include "stdafx.h" #include <cv.h> #include <highgui.h> //callback function void mouseEvent(int evt, int x, int y, int flags, void* param){ if(evt==CV_EVENT_MOUSEMOVE && flags==CV_EVENT_FLAG_CTRLKEY){ printf("%d %d\n",x,y); } } int main() { cvNamedWindow("MyWindow"); //assigning the callback function for mouse events cvSetMouseCallback("MyWindow", mouseEvent, 0); //load and display an image IplImage* img = cvLoadImage("C:/MyPic.jpg"); cvShowImage("MyWindow", img); //wait for key press cvWaitKey(0); //cleaning up cvDestroyWindow("MyWindow"); cvReleaseImage(&img); return 0; }