[学习OpenCV攻略][005][视频播放控制]

cvSetCaptureProperty(视频,属性,属性值)

设置视频的属性,属性可以是宏CV_CAP_PROP_POS_FRAMES 视频帧的位置

cvGetCaptureProperty(视频,属性)

得到视频的属性值,属性可以是宏CV_CAP_PROP_FRAMES_COUNT视频帧数,CV_CAP_PROP_FRAME_WIDTH视频的宽度,CV_CAP_PROP_FRAME_HEIGHT

cvCreateTrackbar(滚动条名称,窗口名称,滑动条位置,总帧数,回调函数)

在窗口中创建滚动条,并设置位置和总帧数,当滚动条被拖动时,触发回调函数

cvSetTrackbarPos(滚动条名称,窗口名称,位置)

设置窗口中滚动条的位置

#include <stdio.h>
#include <iostream>
#include <fstream>
#include "cv.h"
#include "highgui.h"

using namespace std;

int g_slider_position = 0;
CvCapture* g_capture = NULL;

void onTrackbarSlide(int pos){
	cvSetCaptureProperty(
		g_capture,
		CV_CAP_PROP_POS_FRAMES,
		pos
	);
}

int getAVIFrames(char * fname) { 
    char tempSize[4];
    // Trying to open the video file
    ifstream  videoFile( fname , ios::in | ios::binary );
    // Checking the availablity of the file
    if ( !videoFile ) {
      cout << "Couldn’t open the input file " << fname << endl;
      exit( 1 );
    }
    // get the number of frames
    videoFile.seekg( 0x30 , ios::beg );
    videoFile.read( tempSize , 4 );
    int frames = (unsigned char ) tempSize[0] + 0x100*(unsigned char ) tempSize[1] + 0x10000*(unsigned char ) tempSize[2] +    0x1000000*(unsigned char ) tempSize[3];
    videoFile.close(  );
    return frames;
}

int main(int argc, char** argv){
	cvNamedWindow("hello", CV_WINDOW_AUTOSIZE);
	g_capture = cvCreateFileCapture(argv[1]);
	IplImage *foo = cvQueryFrame(g_capture);
	
	int frames = (int)cvGetCaptureProperty(
		g_capture,
		CV_CAP_PROP_FRAME_COUNT
	);
	
	int tmpw = (int)cvGetCaptureProperty(
		g_capture,
		CV_CAP_PROP_FRAME_WIDTH
	);
	
	int tmph = (int)cvGetCaptureProperty(
		g_capture,
		CV_CAP_PROP_FRAME_HEIGHT
	);
	
	printf("opencv frames %d w %d h %d
", frames, tmpw, tmph);
	
	frames = getAVIFrames(argv[1]);
	
	printf("hacked frames %d w %d h %d
", frames, tmpw, tmph);
	
	cvCreateTrackbar(
		"position",
		"hello",
		&g_slider_position,
		frames,
		onTrackbarSlide
	);
	
	IplImage *frame;
	frames = 0;
	while(1){
		frame = cvQueryFrame(g_capture);
		if(!frame){
			break;
		}
		
		frames++;
		printf("
Frame number=%d", frames);
		cvSetTrackbarPos("position", "hello", frames);
		
		cvShowImage("hello", frame);
		
		char c = (char)cvWaitKey(10);
		if(c == 27){
			break;
		}
	}
	
	cvReleaseCapture(&g_capture);
	cvDestroyWindow("hello");
	
	return 0;
}
原文地址:https://www.cnblogs.com/d442130165/p/4918494.html