我已经选择了“学习OpenCV”,并且一直在尝试一些代码示例/练习.在这段代码片段中,我想让滑块更新其每个视频帧更改的位置,但是由于某些原因,它将无法正常工作(图片使用以下代码冻结):
#include "cv.h" #include "highgui.h" int g_slider_position = 0; CvCapture* g_capture = NULL; void onTrackbarSlide(int pos) { cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos); } int main(int argc,char** argv) { cvNamedWindow("The Tom 'n Jerry Show",CV_WINDOW_AUTOSIZE); g_capture = cvCreateFileCapture(argv[1]); int frames = (int) cvGetCaptureProperty( g_capture,CV_CAP_PROP_FRAME_COUNT ); if (frames != 0) { cvCreateTrackbar( "Position","The Tom 'n Jerry Show",&g_slider_position,frames,onTrackbarSlide ); } IplImage* frame; while (1) { frame = cvQueryFrame(g_capture); if (!frame) break; cvSetTrackbarPos( "Position",++g_slider_position ); cvShowImage("The Tom 'n Jerry Show",frame); char c = cvWaitKey(33); if (c == 27) break; } cvReleaseCapture(&g_capture); cvDestroyWindow("The Tom 'n Jerry Show"); return 0; }
任何想法如何让滑块和视频按预期工作?
解决方法
This is the actual working code // PROGRAM TO ADD A UPDATING TRACKBAR TO A VIDEO #include <cv.h> #include <highgui.h> int g_slider_position = 0; CvCapture* video_capture = NULL; void onTrackbarSlide(int current_frame) { current_frame = g_slider_position; cvSetCaptureProperty(video_capture,current_frame); } int main( int argc,char** argv ) { cvNamedWindow( "Video",CV_WINDOW_AUTOSIZE ); video_capture = cvCreateFileCapture( "Crowdy.avi"); int no_of_frames = (int) cvGetCaptureProperty(video_capture,CV_CAP_PROP_FRAME_COUNT); if( no_of_frames!= 0 ) { cvCreateTrackbar("Slider","Video",no_of_frames,onTrackbarSlide); } IplImage* frame; while(1) { frame = cvQueryFrame( video_capture ); if( !frame ) break; cvShowImage( "Video",frame ); cvSetTrackbarPos("Slider",++g_slider_position); char c = cvWaitKey(33); if( c == 27 ) break; } cvReleaseCapture( &video_capture ); cvDestroyWindow( "Video" ); return(0); }