visual-studio-2010 - 如何编写视频?

标签 visual-studio-2010 opencv

我在带有32位OS的Windows 7中将Visual Studio 2010与opencv一起使用。...运行人员检测示例程序时,它显示在窗口中播放的输出视频...但是我无法打开输出视频,存储在特定位置...请帮助我...谢谢...

#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;
} 

最佳答案

您应该在无限循环之前初始化视频编写器,并在没有更多可抓取的帧时释放视频编写器(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。我相信以'cv'开头的每个函数都是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;
} 

关于visual-studio-2010 - 如何编写视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28496459/

相关文章:

c++ - 如何在 XML 文本(标签内)中搜索换行符?

c++ - 您是否推荐在构建时为 C/C++ 启用代码分析?

visual-studio-2010 - 什么是相当于 eclipse alt + arrow Left 的 Visual Studio

java - java中的TCP网络摄像头流

opencv - RANSAC相机校准实现

ios - 使用 OpenCV 在卡片边缘绘制轮廓

visual-studio-2010 - 使用 vs 2010 在负载测试中测试迭代设置

visual-studio-2010 - 在 Visual Studio 2010 中查看 TFS 变更集时,如何更改打开选定文件的默认程序(记事本)?

python - 在 OpenCV-Python 中绘制直方图

python - PyOpenCV 是否支持 GPU?