OpenCV 2.1 and 2.3 with Visual Studio 2010 Quick Start

I am using OpenCV 2.1/2.3 with the newer C++ style OpenCV interface. There are a few tricky parts and changes that aren't mentioned in the cheatsheet. Hopefully the OpenCV documentation will continue to improve as it grows.  Anyway, here is a quick start guide that might help beginners out.

Camera Capture in OpenCV 2.x

#include "opencv\cv.h"
#include "opencv\highgui.h"
using namespace cv; ...
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
  return -1;
for(;;) {
  Mat frame;
  cap >> frame; // get a new frame from camera
  imshow("Camera Preview", frame);
  if(waitKey(30) >= 0) break;
}

OpenCV 2.1 Visual Studio Project Starter

  • C/C++ > Include Directories:
    • X:\OpenCV2.1\include
  • Linker > Additional Library Directories:
    • X:\OpenCV2.1\lib
  • Linker > Input > Additional Dependencies: (this will vary from one project to the next, but you probably want the first three)
    • cv210.lib
    • cxcore210.lib
    • highgui210.lib
    • cvaux210.lib
    • cxts210.lib
    • ml210.lib
    • opencv_ffmpeg210.lib
  • Add to PATH environment variable (or copy DLLs into output directory):
    • X:\OpenCV2.1\bin

OpenCV 2.3.1 Visual Studio Project Starter

OpenCV 2.3.1 directory structure is different from other versions. I downloaded from OpenCV-2.3.1-win-superpack.exe. This assumes x86, 32-bit architecture, so change accordingly if you are on 64-bit Windows. Note this also includes libs for static linking (instead of requiring DLLs) in OpenCV2.3\build\[architecture]\[compiler]\staticlib. Project settings (this is kind of a deluxe set that was necessary to build the opencv_stitching example):

  • C/C++ > Include Directories:
    • X:\OpenCV2.3\build\include
    • X:\OpenCV2.3\modules\imgproc\src
  • Linker > Additional Library Directories:
    • X:\OpenCV2.3\build\x86\vc10\lib
  • Linker > Input > Additional Dependencies: (this will vary from one project to the next, but you probably want the first three)
    • opencv_core231.lib
    • opencv_highgui231.lib
    • opencv_imgproc231.lib
    • opencv_features2d231.lib
    • opencv_flann231.lib
    • opencv_gpu231.lib
    • opencv_haartraining_engine.lib
    • opencv_legacy231.lib
    • opencv_ml231.lib
    • opencv_objdetect231.lib
    • opencv_ts231.lib
    • opencv_video231.lib
    • opencv_calib3d231.lib
    • opencv_contrib231.lib
  • Add to PATH environment variable (or copy DLLs into output directory):
    • X:\OpenCV2.3\build\x86\vc10\bin
    • X:\OpenCV2.3\build\common\tbb\ia32\vc10 (optional, for Intel Threading Building Blocks parallel processing support)

And you'll need to include the right headers at the top of your source. You have the option of including the entire OpenCV 1 and OpenCV 2 C++-style interface, or just the C++-style interface. This gives you everything, since cv.h and highgui.h include the C++ interface:

#include "opencv/cv.h"
#include "opencv/highgui.h"
...
using namespace cv;

If you are feeling dangerous, you can use the C++ interface exclusively with .hpp:

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
...
using namespace cv;

If everything compiles and then you get runtime errors at the first instance of an OpenCV function call, something like this:

First-chance exception at 0x7c90e4ff in OpenCVHello.exe: 0xC0000008: An invalid handle was specified.
This might be a bug in the OpenCV build, I don't know. You can disable it by going to Debug > Exceptions, unfold Win32 exceptions, and uncheck 0xC0000008.

Integrating OpenCV in larger programs

OpenCV highgui is great for experimenting.  But eventually, you might want to develop a useful stand-alone program.  So for this, cv::imshow and cvWaitKey are not going to cut it. See discussion at StackOverflow, Integrating OpenCV with larger programs.

In most cases, there is no magic to handing off image data to other libraries. The standard container is an unsigned char array. If your image is 320x240 RGB, then your array size is 3 x 320 x 240 x sizeof(unsigned char) bytes. Since this is an unlabeled container, the consumer of this data will also need to be supplied with the image height, width, and color mode. You can initialize a cv::Mat with raw data by supplying a pointer (this assumes RGB color mode): Mat img(rawDataWidth, rawDataHeight, CV_8UC3, rawData); and later access the raw data with img.data. The only snags you are likely to come across are in dealing with row order and RGB vs. BGR format. OpenCV provides cv::flip() and cv::cvtColor() to deal with this.

I have had success using videoInput library to access DirectShow devices. The library enumerates and lists friendly names of connected devices, and generally gives you more information when something goes wrong. It's much better than guessing capture device IDs, and essential if you are going to provide some camera selection to the user.

Qt + OpenCV

Key points:

  • Qt is a good option because Qt is great and has a big community.
  • Open a new thread to run cv::VideoCapture in a loop and emit signal after frame capture. Use Qt's msleep mechanism, not OpenCV. So, we are still using OpenCV highgui for capture.
  • Convert cv::Mat to QtImage:
    QImage qtFrame(cvFrame.data, cvFrame.size().width, cvFrame.size().height, cvFrame.step, QImage::Format_RGB888);
    qtFrame = qtFrame.rgbSwapped();
  • Optional: Render with GLWidget. Convert QtImage to GLFormat with Qt built-in method:
    m_GLFrame = QGLWidget::convertToGLFormat(frame);
    this->updateGL();

Juce + OpenCV

I have also had success integrating OpenCV with Juce/OpenGL, rendering a capture frame on rotating surface in OpenGL. Converting a cv::Mat to an OpenGL texture goes like this:

declarations:

cv::Mat frame; // object to receive a captured frame.
cv::VideoCapture cap;  // video capture object
CvMat *_img;
CvMat *arrMat, *cvimage, stub;

in camera capture loop (usually a separate thread):

cap >> frame; 
/* following will convert cv::Mat to CvMat data format, which can be
	used as a texture in OpenGL */
if (_img == 0)
	_img = new CvMat(frame);
CvArr *arr = _img;
arrMat = cvGetMat(arr, &stub);
cvimage = cvCreateMat(arrMat->rows, arrMat->cols, CV_8UC3);
cvConvertImage(arrMat, cvimage, 0);
//sleep function here:
waitKey(33); //OpenCV
//Sleep(33);   //windows.h
//msleep(33);  //Qt
//wait(33);    //Juce

in the OpenGL render function:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, cvimage->cols, cvimage->rows, 0, 
	GL_BGR_EXT /* GL_BGR */, GL_UNSIGNED_BYTE, cvimage->data.ptr);

I have a feeling this sample code, which I borrowed from this OpenGL+OpenCV tutorial, could be even simpler; I also don't like that it's using OpenCV 1.0 style structures. More on Juce/OpenGL later.

8 ResponsesLeave a Reply

  1. i've written a guide for opencv and netbeans, for those who feel uncomfortable with visual studio. hope u check it out.

    http://www.thirumal.in/2012/01/using-opencv-231-with-netbeans-7-ide.html

  2. chaitanya

     /  March 29, 2012 Quote

    Hey.
    Nice guide.....
    Have you tried for IplImage to/from juceImage.
    If you have such code please post.

    THanks ...

  3. You made my day, thanks!

  4. Hi thanks for posting this article!
    It helped me to be able to capture video from a webcamera which didn't work for me otherwise.

  5. Bayhaki

     /  January 17, 2013 Quote

    Nice guide ^^

    I have problem to put the capture of camera in MFC (picture box).
    could you help me, please?

    Thanks

  6. boushila

     /  February 19, 2013 Quote

    quand j'exécute le code de code source détection yeux bouche nez il mm'affiche toujours ce messages: 1>c:\users\poste\documents\visual studio 2010\projects\vendredi\vendredi.cpp(3): fatal error C1010: fin de fichier inattendue lors de la recherche d'un en-tête précompilé. N'auriez-vous pas oublié d'ajouter '#include "StdAfx.h"' à votre source ?
    mon environnement de travail: Windows 7 , Visual studio 2010 , opencv 2.3.1
    mon email :ibtissem.boushila@gmail.com

  7. vinothkumar

     /  November 19, 2014 Quote

    Hello friend,
    Nice guide. I've a question "how to convert video into frames " and "How to select object from video using mouse??".If you have a code for this problem could you post or send mail to vinoth91.aec@gmail.com. Thanks!..

  8. boushila

     /  June 10, 2015 Quote

    boushila: quand j'exécute le code de code source détection yeux bouche nez il mm'affiche toujours ce messages: 1>c:\users\poste\documents\visual studio 2010\projects\vendredi\vendredi.cpp(3): fatal error C1010: fin de fichier inattendue lors de la recherche d'un en-tête précompilé. N'auriez-vous pas oublié d'ajouter '#include "StdAfx.h"' à votre source ?
    mon environnement de travail: Windows 7 , Visual studio 2010 , opencv 2.3.1

Leave a Reply to ibuff