1

I'm using opencv with visual studio 2010 in Windows 7 with 32 bit OS.... While running the sample program of People detection, it shows the output video playing in a window... But I'm unable to open the output video, stored in a particular location... Kindly help me... Thankyou...

#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 Answers1

0

You should initialize the videowriter before the infinite loop, and release the videowriter (not necessary with the C++ API) and the videocapture once there are no more frame to grab :

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

You should also use the C++ API of openCV. I believe every function beginning with 'cv' is part of the C API (and no longer supported). Check the openCV documentation to find the corresponding C++ function.

For example :

img=cvQueryFrame(cap);

Will become :

cap >> img;

Edit

I corrected your code to use the C++ API of openCV, and it's working fine (the people detection seems to give false positive though). Here is the code :

#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

Neimsz
  • 1,554
  • 18
  • 22
  • Thanks @Neimsz... but i stored the processed frame in the name'img'... then how to get that img inside the 'for' loop??? for(;;){ //do your stuff //write the current frame writer2.write(img); } and onemore thing, I'm using this output stored video as a input for another module.... will it be written in the same module within the same main function??? will it be possible...??? – sridevi vijayan Feb 16 '15 at 09:39
  • `Mat img` is declared in your main, so I don't see where your problem is : the `for` loop is nested in the main, so you can access the variable img from there. The last part of your question is ambiguous, could you post your code ? – Neimsz Feb 17 '15 at 07:39
  • Thanks... Yes... able to access 'img' inside the 'for' loop... but the problem is... not able to play the stored video... and leave the last part of my previous question.... – sridevi vijayan Feb 17 '15 at 07:50
  • `writer2.open("E:/Phase_I_output/video_.avi",codec, 50.0 ,size2,true);` the parameters have to be the same as in the previous declaration of the videowriter. – Neimsz Feb 18 '15 at 09:59
  • Thankyou @Neimsz... the code working good... It plays the resultant video well, but it is unable to open the stored video... – sridevi vijayan Feb 21 '15 at 06:20
  • This is the warning I got in the place of videowrite, while running this code... "Warning 1 warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data c:\users\administrator.ecilpc34\documents\visual studio 2010\projects\pedesdet\pedesdet\pedesdet.cpp" – sridevi vijayan Feb 23 '15 at 04:27
  • I got this run time notification too... "could not find encoder for codec id 28".. Please help me... Thank you... – sridevi vijayan Feb 23 '15 at 10:12
  • Try using another codec, maybe `CV_FOURCC('M', 'J', 'P', 'G');` (in my code i tried to write the video with the same codec as the input video, but have the same runtime warning as you have when using a .mp4). Or try with other video extension – Neimsz Feb 24 '15 at 11:28
  • Thankyou so much... whether any encoder should be installed to overcome this problem??? – sridevi vijayan Feb 25 '15 at 03:52
  • I assume you are on windows since you are using Visual Studio, you can try using `-1` instead of `CV_FOURCC('M', 'J', 'P', 'G')` like proposed in this [answer](http://stackoverflow.com/questions/4872383/how-to-write-a-video-file-with-opencv). – Neimsz Feb 25 '15 at 13:10
  • I edited my answer, `int codec = static_cast(cap.get(CV_CAP_PROP_FOURCC));`, seems to be recommended in openCV tutorial – Neimsz Feb 25 '15 at 13:20
  • Even it is not able to open the output video,wthether it is possible to continued with optical flow method... since in optical flow method, operation between consecutive frame is done.... then how to get the continous frame in different variable??? – sridevi vijayan Feb 27 '15 at 05:56
  • Thankyou so much @Neimsz... that I saved the image frames instead of the video... and this will be helpful for proceeding further.... – sridevi vijayan Mar 02 '15 at 09:27