Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Displaying video using vector<mat> not working proplerly

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();
}