OpenCV: Brox Optical Flow Sample, possible fix

I had some fun getting OpenCV with CUDA support and the demo to work that required OpenGL that for some yet unknown reason would not connect in properly. So I thought I would share my experience.

First of compile OpenCV with CUDA



You can also enable OpenGL, but this didn’t work for me yet it did enable the #define HAVE_OPENGL compile it up and copy out the brox example and make the following corrections, aka #defeine out the OPENGL window parts.

1 2 cout << "OpenCV / NVIDIA Computer Vision" << endl; 3 cout << "Optical Flow Demo: Frame Interpolation" << endl; 4 cout << "=========================================" << endl; 5 6 namedWindow("Forward flow"); 7 namedWindow("Backward flow"); 8 9 #ifdef HAVE_OPENGL 10 11 namedWindow("Needle Map", WINDOW_OPENGL); 12 setGlDevice(); 13 14 #endif 15 16 namedWindow("Interpolated frame"); 17 18

To note you may need to #undef HAVE_OPENGL. For a full easy run source you can get from here


Reference: Body of code taken from OpenCV Sample




Left – Forward Optical flow, Right – Backward Optical flow


Some interesting performance info for computation of Brox Optical Flow it took 0.01s, but the Image Copy to GPU took 6.485s [e.g. GpuMat d_frame0(frame0Gray) ]. This was on a laptop spec of i7 (8 thread), 10gb ram, GeForce GT540M 1GB. I will try on another machine sometime see if can get a faster performance specifically in image copy.



  • There may be a better way to overcome the OpenGL requirement this was just a quick work around
  • NVIDIA CUDA compiler does not currently support Visual Studio 2012 therefore for CUDA you need to use 2010.
Saturday, September 8, 2012 9:19:00 PM Categories: API C++ Computer Vision Programming Software Visual Studio
Stuart James