c – OpenCV – 在视频播放过程中让滑块更新其位置

前端之家收集整理的这篇文章主要介绍了c – OpenCV – 在视频播放过程中让滑块更新其位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经选择了“学习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);
}
原文链接:https://www.f2er.com/c/114740.html

猜你在找的C&C++相关文章