Ask Your Question
0

VideoWriter unhandled exception

asked 2016-11-10 22:53:05 -0600

Vizier87 gravatar image

Hi guys,

I was recording my video output combining 6 videos into one and used the DIVX encoding using VideoWriter.

The video recorded fine until it always returned "unhandled exception" at some point. The output video can be played only up to the point it crashed.

Can someone point out what's might be the reason?

int main(int, char**) {

(Contains deleted code lines due to file location for anonymity)

if (!capture1.isOpened() && !capture2.isOpened() && !capture3.isOpened() && !capture4.isOpened() && !capture5.isOpened() && !capture6.isOpened())  // check if we succeeded
    return -1;


struct timeb start, end;
int diff;
int i = 0;
long buffer = 0;


VideoWriter video("Path", CV_FOURCC('D', 'I', 'V', 'X'), 5, Size(1058, 710), true);
bool State = false;

namedWindow("Video", 1);
for (;;)
{

    ftime(&start);
    Mat frame1, frame2, frame3, frame4, frame5, frame6;

    capture1 >> frame1; // get a new frame from camera
    capture2 >> frame2;
    capture3 >> frame3;
    capture4 >> frame4;
    capture5 >> frame5;
    capture6 >> frame6;

    int width = frame1.size().width * 0.5;
    int height = frame1.size().height * 0.7;

    resize(frame1, frame1, Size(height, width), 0, 0, INTER_CUBIC);
    resize(frame2, frame2, Size(height, width), 0, 0, INTER_CUBIC);
    resize(frame3, frame3, Size(height, width), 0, 0, INTER_CUBIC);
    resize(frame4, frame4, Size(height, width), 0, 0, INTER_CUBIC);
    resize(frame5, frame5, Size(height, width), 0, 0, INTER_CUBIC);
    resize(frame6, frame6, Size(height, width), 0, 0, INTER_CUBIC);

    Mat dst_img(cv::Size(frame1.size().width * 3 + 50, frame1.size().height * 2 + 70), frame1.type(), cv::Scalar(0, 0, 0));



    frame1.copyTo(dst_img(Rect(10, 10, frame1.cols, frame1.rows)));
    frame2.copyTo(dst_img(Rect(frame2.size().width + 20, 10, frame2.cols, frame2.rows)));
    frame3.copyTo(dst_img(Rect(30 + frame2.size().width * 2, 10, frame3.cols, frame3.rows)));
    frame4.copyTo(dst_img(Rect(10, frame1.size().height + 20, frame4.cols, frame4.rows)));
    frame5.copyTo(dst_img(Rect(frame2.size().width + 20, frame1.size().height + 20, frame4.cols, frame4.rows)));
    frame6.copyTo(dst_img(Rect(30 + frame2.size().width * 2, frame1.size().height + 20, frame4.cols, frame4.rows)));

    ////////////////////////////////////////////////////////////////////////////////////
    // Calculating Frequency Output Region /////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////
    std::stringstream FreqDisp;
    ftime(&end);
    diff = (int)(1000.0 * (end.time - start.time) + (end.millitm - start.millitm));
    buffer = diff + buffer;


    if (10 + buffer * 0.01 > 20000){
        return 0;
    }

    FreqDisp << std::setprecision(1) << std::to_string(10 + buffer * 0.01);


    putText(dst_img, FreqDisp.str() + " MHz", Point(20, dst_img.size().height - 20), FONT_HERSHEY_PLAIN, 1, Scalar::all(255), 2, 8);
    putText(dst_img, "Control", Point(dst_img.size().width - 100, dst_img.size().height - 20), FONT_HERSHEY_PLAIN, 1, Scalar::all(255), 2, 8);

    imshow("Video", dst_img);
    video.write(dst_img);

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

return 0;

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-11-10 23:05:01 -0600

berak gravatar image

VideoCapture will return an empty, invalid frame, if it reached the end of your movie. you will have to check for this:

capture1 >> frame1;
if (frame1.empty()) {
    // what now ?
}
// repeat for all 6 captures
edit flag offensive delete link more

Comments

Thanks berak. Still gettting the same error. I wonder if it is due to memory? The output video file is only about 70 MB though..

Here's a screenshot of the error: https://s15.postimg.org/gkwc40w3v/Unt...

Anything else I might have missed?

Vizier87 gravatar imageVizier87 ( 2016-11-11 01:01:52 -0600 )edit

can you check your size calculations again ? (honestly, i'm too lazy for that now)

berak gravatar imageberak ( 2016-11-11 01:23:12 -0600 )edit

You mean the output window size? Yes, I checked by using

putText(dst_img, std::to_string(Point(dst_img.size().width)) + " " + std::to_string(dst_img.size().height)), FONT_HERSHEY_PLAIN, 1, Scalar::all(255), 2, 8);

And then put the values into the VideoWriter variables. I hope that's what you meant?

Vizier87 gravatar imageVizier87 ( 2016-11-11 01:30:18 -0600 )edit

no, more like that: frame1.size().width * 3 + 50, frame1.size().height * 2 + 70 and check, if the region you copy to fits inside

(i guess, you're going over the borders somewhere, but your maths there is far too ugly, to check ;) )

berak gravatar imageberak ( 2016-11-11 01:43:14 -0600 )edit

Well the output video is definitely working fine, as for the recording as well, and I can even play the video afterwards but only up to the point when the program crashed though.

I understand if there's an "overflow" there'll be that error you're thinking of, right? But in my case the output was as attached: https://s11.postimg.org/c8de7sstv/Unt...

Anyway if I understood you wrong, please correct me.

Thanks!

Vizier87 gravatar imageVizier87 ( 2016-11-11 01:58:09 -0600 )edit

if you're writing to invalid memory, your program is UB, and errors will (if at all) show up later, in some seemingly unrelated part.

berak gravatar imageberak ( 2016-11-11 02:05:25 -0600 )edit

Oh dayum!! You're a wizard man. I left the program running without the Videowriter and there it pops up! What should I do? Resize the window to a bigger one or make it static sized without depending on frame1.size().width etc?

Vizier87 gravatar imageVizier87 ( 2016-11-11 02:08:39 -0600 )edit

no, reconsider you maths there, simply. you want to tile an image into 2 rows and 3 cols, so just do that.

determine your desired output size (say, 640x480), then do the resp. divisions.

all those *0.7 , +20-cat's tail terms are murky.

berak gravatar imageberak ( 2016-11-11 02:21:38 -0600 )edit

Ok new development, I've set the output size as you suggested, still same error but I noticed in the console when it occured: https://s16.postimg.org/67cpsstv9/Unt...

I've read about it in another forum: http://stackoverflow.com/questions/21... , and I suspect the reason was as provided on the lack of image in some frames.. I'm pretty sure it may occur in a few instances throughout some of the videos.

Can I just make the code skip that particular error like VB.net's On Error Resume Next? Sorry I'm still polishing my C++.

Thanks berak.

Vizier87 gravatar imageVizier87 ( 2016-11-14 03:12:36 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-11-10 22:53:05 -0600

Seen: 849 times

Last updated: Nov 10 '16