《学习opencv》第四章课后习题4时间:2022-06-08 00:11:12首先在项目属性-》配置属性-》链接器-》输入-》附加依赖项 中加入如下lib文件: opencv_imgproc220d.lib opencv_legacy220d.lib opencv_ml220d.lib opencv_core220d.lib opencv_highgui220d.lib opencv_video220d.lib // 4-4.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<opencv/cv.h>#include<opencv/highgui.h>/*int g_switch_value = 1;void switch_off_function() {printf("Pause\n");}; //YOU COULD DO SOMETHING WITH THESE FUNCTIONS TOOvoid switch_on_function() {printf("Run\n");}; void switch_callback( int position ) { if( position == 0 ) { switch_off_function(); } else { switch_on_function(); }} int main(){ IplImage *frame; CvCapture* g_capture = NULL; if(!(g_capture = cvCreateFileCapture("C:\\test.avi"))) { printf("Failed to open %s\n","C:\\test.avi"); return -1; } cvNamedWindow( "SliderControl", 1 ); cvCreateTrackbar( "Switch", "SliderControl", &g_switch_value, 1, switch_callback ); while( 1 ) { if(g_switch_value) { frame = cvQueryFrame( g_capture ); if( !frame ) break; } cvShowImage( "SliderControl", frame); if(cvWaitKey(10)==27 ) break; } cvReleaseCapture( &g_capture ); cvDestroyWindow( "SliderControl" ); return(0);}*/int g_switch_value = 1;int g_slider_position = 0;CvCapture* g_capture = NULL;void onTrackbarSlide(int pos){ cvSetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES,pos);}void switch_callback( int position ) { if( position == 0 ) { printf("Pause\n"); } else { printf("on\n"); }}int main(){ cvNamedWindow("Test Vedio Slide",CV_WINDOW_AUTOSIZE); g_capture = cvCreateFileCapture("C:\\test.avi"); int frames = (int)cvGetCaptureProperty(g_capture,CV_CAP_PROP_FRAME_COUNT); int frameRate = (int)cvGetCaptureProperty(g_capture,CV_CAP_PROP_FPS); if(frames!=0) { cvCreateTrackbar( "Switch","Test Vedio Slide",&g_switch_value,1,switch_callback); cvCreateTrackbar("position","Test Vedio Slide", &g_slider_position, frames, onTrackbarSlide); } IplImage* frame; while(g_slider_position<frames) { if(g_switch_value==1) { frame = cvQueryFrame(g_capture); if(!frame) break; cvShowImage("Test Vedio Slide", frame); char c = cvWaitKey(frameRate); cvSetTrackbarPos("position","Test Vedio Slide", g_slider_position); onTrackbarSlide(g_slider_position); if(c == 27) break; g_slider_position++; } else { if(cvWaitKey(10)==27 ) break; } } cvReleaseCapture(&g_capture); cvDestroyWindow("Test Vedio Slide");}