Ask Your Question
0

Displaying video using vector<Mat> not working proplerly

asked 2013-11-16 23:04:40 -0600

zulfiqar gravatar image

I am trying to display video for in a separate function for this i am using vector<mat> i push_back each frame in and then pass this vector to function but my function displays a single frame repetitively. My code is below. Please tell me what i am doing wrong. // newproject.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include "highgui.h"
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <conio.h>
#include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur
#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <conio.h>

using namespace cv;
using namespace std;

class frameprocessing{

Mat hsv_base;
MatND hist_base;

public:
    void hsv_histogram(Mat Frame)
    {
        cvtColor( Frame, hsv_base, CV_BGR2HSV );
        int h_bins = 50; 
        int s_bins = 32;
        int histSize[] = { h_bins, s_bins };

        float h_ranges[] = { 0, 256 };
        float s_ranges[] = { 0, 180 };

        const float* ranges[] = { h_ranges, s_ranges };
        int channels[] = { 0, 1 };
        calcHist( &hsv_base, 1, channels, Mat(), hist_base, 2, histSize, ranges, true, false );
    }
};

class video{    

    Mat frame;
    string filename;
    double dWidth;
    double dHeight;

public:
    video()
    {

    }

    video(string videoname)
    {

        vector<Mat> videoframes;
        std::vector<Mat>::iterator it;
        it = videoframes.begin();
        filename = videoname;
        VideoCapture capture(filename); 
        if( !capture.isOpened() )
        {
            exit(0);
        }
        dWidth   = capture.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
        dHeight = capture.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

        frameprocessing obj;

        for( ; ; )
        {
            capture >> frame;
            if(frame.empty())
                break;

            obj.hsv_histogram(frame);
            videoframes.push_back(frame);
        }
        displayvideo(videoframes);
    //  waitKey(0); // key press to close window
    }

    void writer()
    {
        Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
        VideoWriter oVideoWriter ("D:/MyVideo.avi", CV_FOURCC('P','I','M','1'), 20, frameSize, true); //initialize the VideoWriter object 
        if ( !oVideoWriter.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
        {
            cout << "ERROR: Failed to write the video" << endl;
            exit(0);
        }
    }
    void displayvideo(vector<Mat> videoframe)
    {
        Mat tempframe;
        while(!videoframe.empty()) //Show the image captured in the window and repeat
        {
            tempframe = videoframe.back();
            imshow("video", tempframe);
            videoframe.pop_back();
            waitKey(20); // waits to display frame
        }
    //  waitKey(0);
    }

};

int _tmain(int argc, _TCHAR* argv[])
{
    video obj("video.avi");
    //obj.readvideo();
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-11-19 00:20:18 -0600

zulfiqar gravatar image

we need to clone our frame otherwise it will point to same frame

     for( ; ; )
     {    
        capture >> frame;
        if(frame.empty())
            break;

        Mat tmp=frame.clone();
        obj.hsv_histogram(tmp);
        videoframes.push_back(tmp);
       }
edit flag offensive delete link more

Comments

Agreed, but I am wondering why you would actually push all frames to a vector, so that you can process them and then display them seperately. It is wiser to use a workflow that reads 1, then processes it, then displays it, then reads a new frame. This workflow will make sure you do not run into the problems you are having now.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-11-20 03:55:36 -0600 )edit

If that is the correct workflow, is there any way to deal with the issue that the processing of a frame takes too long and thus playback of the processed video becomes slow. Thanks in advance.

Laniakea gravatar imageLaniakea ( 2017-02-08 09:24:56 -0600 )edit

Question Tools

Stats

Asked: 2013-11-16 23:04:40 -0600

Seen: 837 times

Last updated: Nov 19 '13