[学习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(滚动条名称,窗口名称,位置)

设置窗口中滚动条的位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#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\n", frames, tmpw, tmph);
     
    frames = getAVIFrames(argv[1]);
     
    printf("hacked frames %d w %d h %d\n", 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("\nFrame 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;
}

 

posted @   盛夏夜  阅读(272)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示