使用矢量<mat>无法正常工作显示视频</mat>

时间:2013-11-17 05:09:10

标签: c++ opencv

我正在尝试在单独的函数中显示视频,我正在使用向量i push_back每个帧然后将此向量传递给函数但我的函数重复显示单个帧。我的代码如下。请告诉我我做错了什么。

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

    void displayframe(Mat frame)
    {
        imshow("video", frame);
        waitKey(20); // waits to display frame
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    video obj("video.avi");
    //obj.readvideo();
}

1 个答案:

答案 0 :(得分:1)

您需要将框架复制或克隆到另一个垫子,然后推送到您的矢量,更改您的代码,如

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

            Mat tmp=frame.clone();
            obj.hsv_histogram(tmp);
            videoframes.push_back(tmp);
           }

在你的代码中,你每次都将为帧分配的相同指针传递给你的向量,所以你将得到一个Mat数组,指向向量中的单个(相同)内存位置。要了解有关OpenCV Mat和内存分配的更多信息,请参阅documantation