Ask Your Question

writing MJPG file by VideoWriter in Visual Studio x64/Release platform

asked 2014-04-07 06:19:11 -0500

swk gravatar image

I am trying the following code in Windows 7 pro; Visual Studio 2012 Express with x64/Release configuration using the pre-built OpenCV 2.4.8 and 2.4.5 (x64/vc11). In my environment, EXPECT_EQ fails.

#include "opencv2/opencv.hpp"
void EXPECT_EQ(double a, double b) { if (a != b) { std::cout << "failed" << std::endl; } } 
int main()
    double current_time = 0.0;
    cv::Mat blank = cv::Mat::zeros(480, 640, CV_8UC3);
    cv::VideoWriter writer("output.avi", CV_FOURCC('M','J','P','G'),
                           50.0, blank.size(), true);
    current_time += 20.0;
    writer << blank;
    EXPECT_EQ(20.0, current_time);
    return 0;

This happens only when I write MJPG format files as far as I tried. The problem does not reproduce if I choose Win32 (x86) or Debug configurations.

Any help or comments are appreciated. Thank you very much in advance.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-04-19 19:49:11 -0500

crackwitz gravatar image

updated 2014-04-19 19:50:43 -0500

it's probably a floating point comparison issue. you do not compare floats for equality. you compute the absolute or relative error and check if it's below some acceptable threshold "epsilon".

alternatively: something happens to EXPECT_EQ(). you'll need to investigate, what this function (macro?) does in a release build, or an x64 build.

edit flag offensive delete link more


Thank you for your suggestion. In this particular case, floating point comparison or the macro EXPECT_EQ (defined just above the main function) do not play crucial roles, because replacing the "EXPECT_EQ(20.0, current_time);" line with something like "if (fabs(20.0 - current_time) > 0.1) { std::cout << "failed" << std::endl; }" reproduces the problem.

This behaviour is fragile: By replacing the above line with "std::cout << current_time << std::endl;" the program puts out 20 correctly; while by inserting this after the above if statement it puts a very small number close to zero. So I am afraid "writer << blank;" destroys the stack frame.

swk gravatar imageswk ( 2014-04-21 01:10:37 -0500 )edit

Question Tools


Asked: 2014-04-07 06:19:11 -0500

Seen: 1,558 times

Last updated: Apr 19 '14