怎么写视频?

时间:2015-02-13 09:36:12

标签: visual-studio-2010 opencv

我在Windows 7中使用带有32位操作系统的visual studio 2010的opencv ....在运行People检测的示例程序时,它显示在窗口中播放的输出视频...但我无法打开输出视频,存储在特定位置......请帮助我...谢谢...

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/core/core.hpp>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
using namespace cv; 
using namespace std; 
int main(int argc, char** argv) 
{ 
    Mat img; char _filename[1024];
    HOGDescriptor hog; 
    hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
    namedWindow("people detector", 1); 
    CvCapture *cap=cvCaptureFromFile("E:/Phase_I_output/2.walk.avi");
    img=cvQueryFrame(cap);
    for(;;)
    { 
    img=cvQueryFrame(cap); 
    if(img.empty())
    break;
    fflush(stdout); 
    vector<Rect> found, found_filtered; 
    double t = (double)getTickCount(); 
    int can = img.channels(); 
    hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2); 
    t = (double)getTickCount() - t; 
    printf("tdetection time = %gms\n", t*1000./cv::getTickFrequency()); 
    size_t i, j; 
    for( i = 0; i < found.size(); i++ )
    { 
        Rect r = found[i]; 
        for( j = 0; j < found.size(); j++ )
            if( j != i && (r & found[j]) == r)
                break; 
        if( j == found.size() ) found_filtered.push_back(r); 
    } 
    for( i = 0; i < found_filtered.size(); i++ )
    {
        Rect r = found_filtered[i]; 
        r.x += cvRound(r.width*0.1);
        r.width = cvRound(r.width*0.8); 
        r.y += cvRound(r.height*0.07);
        r.height = cvRound(r.height*0.8); 
        rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3); 
    } 
                Size size2 = Size(640,480);
                int codec = CV_FOURCC('M', 'J', 'P', 'G');
                VideoWriter writer2("E:/Phase_I_output/video_.avi",codec,50.0,size2,true);
                writer2.open("E:/Phase_I_output/video_.avi",codec,15.0,size2,true);
                writer2.write(img);
                imshow("people detector", img);
                if(waitKey(1) == 27)            
                break;
    } 
    std::cout <<  "Completed" << std::endl ;
    waitKey();
    return 0;
} 

1 个答案:

答案 0 :(得分:0)

你应该在无限循环之前初始化videowriter,并且在没有更多的帧可以抓取之后释放视频标记(不需要使用C ++ API)和视频捕获:

Size size2 = Size(640,480);
int codec = CV_FOURCC('M', 'J', 'P', 'G');
VideoWriter writer2("E:/Phase_I_output/video_.avi",codec,50.0,size2,true);
writer2.open("E:/Phase_I_output/video_.avi",codec,50.0,size2,true);

for(;;){
//do your stuff
//write the current frame
writer2.write(img);
}
cvReleaseVideoWriter( writer2 );
cvReleaseCapture( &cap );

您还应该使用openCV的C ++ API。我相信每一个以&#39; cv&#39;是C API的一部分(不再受支持)。检查openCV documentation以查找相应的C ++函数。

例如:

img=cvQueryFrame(cap);

将成为:

cap >> img;

修改

我更正了你的代码以使用openCV的C ++ API,并且它的工作正常(人们检测似乎给出了误报)。这是代码:

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp" 
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/core/core.hpp>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
using namespace cv; 
using namespace std;

int main(int argc, char** argv) 
{ 
Mat img;
string _filename;
_filename = "path/to/video.avi";
HOGDescriptor hog; 
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
namedWindow("people detector", WND_PROP_AUTOSIZE);
VideoCapture cap = VideoCapture(_filename);
if(!cap.isOpened()){
    cout<<"error opening : "<<_filename<<endl;
    return 1;
}
Size size2 = Size(640,480);
int codec = static_cast<int>(cap.get(CV_CAP_PROP_FOURCC));
double fps = cap.get(CV_CAP_PROP_FPS);
VideoWriter writer2("../outputVideo_.avi",codec,fps,size2,true);


for(;;)
{ 
    cap >> img;
    if(img.empty()){
        cout<<"frame n° "<<cap.get(CV_CAP_PROP_FRAME_COUNT)<<endl;
        break;
    }
    fflush(stdout); 
    vector<Rect> found, found_filtered; 
    double t = (double)getTickCount(); 
    int can = img.channels(); 
    hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2); 
    t = (double)getTickCount() - t; 
    printf("tdetection time = %gms\n", t*1000./cv::getTickFrequency()); 
    size_t i, j; 
    for( i = 0; i < found.size(); i++ )
    { 
        Rect r = found[i]; 
        for( j = 0; j < found.size(); j++ )
            if( j != i && (r & found[j]) == r)
                break; 
        if( j == found.size() ) found_filtered.push_back(r); 
    } 
    for( i = 0; i < found_filtered.size(); i++ )
    {
        Rect r = found_filtered[i]; 
        r.x += cvRound(r.width*0.1);
        r.width = cvRound(r.width*0.8); 
        r.y += cvRound(r.height*0.07);
        r.height = cvRound(r.height*0.8); 
        rectangle(img, r.tl(), r.br(), Scalar(0,255,0), 3); 
    } 

    //writer2.write(img);
    writer2 << img;
    imshow("people detector", img);
    if(waitKey(1) == 27)
    {
        break;
    }
}
writer2.release();
cap.release();
cout <<  "Completed" << endl ;
waitKey();
destroyAllWindows();
return 0;
} 

screen shot