Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

videowriter: integer division by zero error

I am using the sample code video-write.cpp(included below for convenience) to learn how to use videos with OpenCV.


#include <iostream> // for standard I/O
#include <string>   // for strings

#include <opencv2 core="" core.hpp="">        // Basic OpenCV structures (cv::Mat)
#include <opencv2 highgui="" highgui.hpp="">  // Video write

using namespace std;
using namespace cv;

static void help()
{
    cout
        << "------------------------------------------------------------------------------" << endl
        << "This program shows how to write video files."                                   << endl
        << "You can extract the R or G or B color channel of the input video."              << endl
        << "Usage:"                                                                         << endl
        << "./video-write inputvideoName [ R | G | B] [Y | N]"                              << endl
        << "------------------------------------------------------------------------------" << endl
        << endl;
}

int main(int argc, char *argv[])
{
    help();

    if (argc != 4)
    {
        cout << "Not enough parameters" << endl;
        return -1;
    }

    const string source      = argv[1];           // the source file name
    const bool askOutputType = argv[3][0] =='Y';  // If false it will use the inputs codec type

    VideoCapture inputVideo(source);              // Open input
    if (!inputVideo.isOpened())
    {
        cout  << "Could not open the input video: " << source << endl;
        return -1;
    }

    string::size_type pAt = source.find_last_of('.');                  // Find extension point
    const string NAME = source.substr(0, pAt) + argv[2][0] + ".avi";   // Form the new name with container
    int ex = static_cast<int>(inputVideo.get(CV_CAP_PROP_FOURCC));     // Get Codec Type- Int form

    // Transform from int to char via Bitwise operators
    char EXT[] = {(char)(ex & 0XFF) , (char)((ex & 0XFF00) >> 8),(char)((ex & 0XFF0000) >> 16),(char)((ex & 0XFF000000) >> 24), 0};

    Size S = Size((int) inputVideo.get(CV_CAP_PROP_FRAME_WIDTH),    // Acquire input size
                  (int) inputVideo.get(CV_CAP_PROP_FRAME_HEIGHT));

    VideoWriter outputVideo;                                        // Open the output
    if (askOutputType)
        outputVideo.open(NAME, ex=-1, inputVideo.get(CV_CAP_PROP_FPS), S, true);
    else
        outputVideo.open(NAME, ex, inputVideo.get(CV_CAP_PROP_FPS), S, true);

    if (!outputVideo.isOpened())
    {
        cout  << "Could not open the output video for write: " << source << endl;
        return -1;
    }

    cout << "Input frame resolution: Width=" << S.width << "  Height=" << S.height
         << " of nr#: " << inputVideo.get(CV_CAP_PROP_FRAME_COUNT) << endl;
    cout << "Input codec type: " << EXT << endl;

    int channel = 2; // Select the channel to save
    switch(argv[2][0])
    {
    case 'R' : channel = 2; break;
    case 'G' : channel = 1; break;
    case 'B' : channel = 0; break;
    }
    Mat src, res;
    vector<mat> spl;

    for(;;) //Show the image captured in the window and repeat
    {
        inputVideo >> src;              // read
        if (src.empty()) break;         // check if at end

        split(src, spl);                // process - extract only the correct channel
        for (int i =0; i < 3; ++i)
            if (i != channel)
                spl[i] = Mat::zeros(S, spl[0].type());
       merge(spl, res);

       //outputVideo.write(res); //save or
       outputVideo << res;
    }

    cout << "Finished writing" << endl;
    return 0;
}

However, once the program starts to write to the output file, 3 lines from the bottom of the code, I get the error below. I get the same error using either output method, and if I try to just put the src straight back to the output(outputVideo << src).

I found another sample program(http://opencv.willowgarage.com/wiki/documentation/cpp/highgui/VideoWriter) that opens the default camera(my built-in webcam), it is able to output just fine. This code is below.

    
    #include "opencv\cv.h"
#include "opencv\highgui.h"

using namespace cv;

int main(int, char**)
{
    VideoCapture capture(1); // open the default camera

    if( !capture.isOpened() )  {
        printf("Camera failed to open!\n");
        return -1;
    }

    Mat frame;
    capture >> frame; // get first frame for size

    // record video
    VideoWriter record("RobotVideo2.avi", CV_FOURCC('D','I','V','X'), 30, frame.size(), true);

    if( !record.isOpened() ) {
        printf("VideoWriter failed to open!\n");
        return -1;
    }

    namedWindow("video",1);

    for(;;)
    {
        // get a new frame from camera
        capture >> frame; 

        // show frame on screen
        imshow("video", frame); 

        // add frame to recorded video
        record << frame; 

        if(waitKey(30) >= 0) break;
    }

    // the camera will be deinitialized automatically in VideoCapture destructor
    // the recorded video will be closed automatically in the VideoWriter destructor
    return 0;
}

Is there something I am missing when setting up the VideoWriter? I am using the Megamind.avi video sample included with OpenCV. Thank you for any help!