今天实现了视频播放以后暂停获取鼠标点击处坐标的功能。
#include <iostream> #include <opencv2/highgui/highgui.hpp> #include <opencv2/opencv.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace cv; using namespace std; Mat frame; void on_mouse( int event, int x, int y, int flags, void* ustc) { CvFont font; cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA); if( event == CV_EVENT_LBUTTONDOWN ) { CvPoint pt = cvPoint(x,y); char temp[16]; sprintf(temp,"(%d,%d)",pt.x,pt.y); putText(frame,temp, pt, CV_FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255,0)); circle( frame, pt, 2,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 ); imshow( "Movie", frame ); } } int main() { VideoCapture capture("D:\\test.wmv"); double rate = capture.get(CV_CAP_PROP_FPS); cout<<"帧率为:"<<rate<<endl; namedWindow("Movie"); int delay = 1000/rate; bool isStart = 0; while(1) { if(!capture.read(frame)) { cout<<"读取视频失败"<<endl; return -1; } imshow("Movie",frame); if (!isStart) { waitKey(0); isStart = 1; } int c = waitKey(delay); if((char) c == 27) { break; } else if( c >= 0) { cvSetMouseCallback( "Movie", on_mouse, 0 ); imshow("Movie",frame); int controlkey = waitKey(0); if((char) controlkey == 27) { break; } } } capture.release(); return 0; }