How to: manipulate webcam streams with OpenCV and OpenGL

Okay – third and final part… I saw a video, I wanted to recreate the effect they used, and now I have, on a live, real-time-processed webcam stream using C++, OpenCV and OpenGL. Yay! =D

Full source code after the jump =D

I pulled a lot of junk out of the mainline and put it into functions to try to clear things up, but it’s still a little messy… Oh well – at least it works =D

13 thoughts on “How to: manipulate webcam streams with OpenCV and OpenGL”

    1. You need to modify the code to point to wherever your copy of cv.h resides. cv.h will come with OpenCV.

      Also, don’t forget to link the project against the following libraries:
      – libopencv_core
      – libopencv_highgui
      – libGLEW
      – libGL
      – libglfw
      – libGLU

    1. 1.) Get a copy of GLEW for your platform,
      2.) Link in the glew library for your platform into your project (this will vary by platform and IDE/Compiler, in Linux you’d use or libGLEW.a)
      3.) Make sure that your project’s “include” search path points to the directory that contains the GL directory that contains glew.h. Or modify the line to #include “glew.h”;, and make sure your project’s “include” search path points at the exact directory that contains glew.h. Or move glew.h into a folder which is already in your project’s “include” search path!
      4.) Compile =D

  1. Hi ,
    I get a webcam image, i can see it in two of the windows. But in the last one there where the matrix is i get only a blackscreen.

    1. OpenGL black screen problems are the bane of graphics programming – you’ll need to get something, anything displayed and then work up from that.

      Try adding the following function and then calling it at multiple points in your code so you can track down if OpenGL is throwing any errors, (i.e. checkGLError(“Just before doing this!”); or checkGLError(“Just after doing that!”);)

      1. just at the beginning in main after
        string windowTitle = “Low Rez Stream Circles |\tFPS: –“;
        initGL(windowWidth, windowHeight, windowTitle);
        the error “Invalid enum.” was thrown

        1. Okay, so it’s failing in initGL… add a bunch of checkGLError(“1”) etcs through the initGL until you find where it’s going wrong.

          I just built OpenCV 2.3.1 to check it out for myself and IplImage is replace by cv::Mat, so the project segfaults on me. As I’ve got a research poster to do before Monday I’m not going to have time to fix it before then.

          If you want to keep going at it feel free, but I’ll come back and do a modern API re-write next week when I have the time.


  2. Hello

    Very interesting stuff. Thank you for your sharing

    I have tried the code, the compilation and linking are OK

    but it fails during InitGL() function, specifically the “glfwOpenWindow” failed!!

    I can not figure out why

    Thank you in advance for your help !


      1. Okay, I’ve rebuilt the project using OpenCV 3.2.1a and got it working again. The problem I was having was that I’d built OpenCV but without the Video 4 Linux drivers to use the webcam, so cvCaptureCAM would return NULL and segfault instead of grabbing a frame from the webcam.

        The dead giveaway for this when you’re building OpenCV is if you see something like this during the cmake configuration (look at the V4L/V4L2 line):

        The way to fix this is to install libv4l-dev which when you run cmake on OpenCV to configure it thereafter will show:

        With this done and OpenCV rebuilt capturing images from the webcam will work – which is a good start.

        I’ve modified the project to include “opencv.hpp” instead of the separate “cv.h”, “highgui.h” etc files (which are the OpenCV1 way of doing things) – and with that done I get this:

        Low-Rez-Stream Update

        I’ve also put together the updated Code::Blocks project in an archive here: – be aware that my opencv libraries (“” and “”) are being pointed at in /usr/local/lib – if your libs are in a different location then you’ll need to change the paths (in C::B you can do this from the menu with Project | Build Options | Linker Settings).

        Hope this helps you out – if it’s still not working (because on second look at your comment [wang] – it looks more like a glfw issue than an opencv one) – then you’re just going to have to try to build up to getting an OpenGL window open using GLFW – take a look at the docs and examples at gpwiki or smth..

        Tuxy – maybe trying from my C::B project will help fix your issue, too.

        Cheers guys – hope you get it all working.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.