OpenCV Beginners/Newbie Frequently Asked Questions

Contents

  1. General Questions
    1. How can I get acquainted with OpenCV fast?
    2. How do I send bug reports on the Intel® Ingegrated Performance Primitives
    3. How do I join the OpenCV forum?
    4. How do I modify the web group so that I don't receive tons of email's?
    5. Ok, I found the group completely useless for me. How can I unsubscribe?
    6. When using OpenCV and IPL simultaneously, I get compiler errors. How to resolve this problem?
    7. Does OpenCV work on other processors?
    8. Does OpenCV still work with the optimized Intel IPP libraries?
  2. Where do I submit Bug reports?
  3. Windows® OS related Qs:
    1. How can I get OpenCV2.2 to run in Python under Windows7?
    2. When I try to build one of the apps, I get an error, streams.h not found.
    3. After installing DirectX SDK I'm still getting linker error about undefined or redefined "TransInPlace" filter class constructors etc.
    4. When I use try to use cvcam, it just crashes
    5. How to register *.ax (DirectShow filter)?
    6. Filter couldn't be registered (regsvr32 reports an error)
    7. cxcore096d.dll or cxcored.lib seem to be missing
    8. When compiling HighGUI I get the error message "mil.h is not found"
    9. How can I debug DirectShow filter?
    10. I get errors about missing DLLs (i.e. cxcore110.dll) when I try to run the sample EXEs or import opencv in Python
    11. How can I create DeveloperStudio project to start playing with OpenCV
  4. Linux Related Qs:
    1. How can I install OpenCV on Linux?
    2. Windows has Visual Studio, which IDE should I use to program OpenCV on Linux?
  5. Technical Questions on Library use:
    1. How to access image pixels
    2. How to access matrix elements?
    3. How do I apply a normalized/modified histogram back to an image?
  6. How to process my data with OpenCV
    1. How to load and display image
    2. How to find and process contours
    3. How to use 2 cameras (multiple cameras) with cvCam library
  7. Getting started with Python
    1. Where can I get help?
    2. I'm used to Matlab, is there a way to use matlab syntax?
    3. How do I convert an OpenCV image to a numpy array?


General Questions

How can I get acquainted with OpenCV fast?

  1. Look at samples.
  2. Look at some web guides
  3. Using C/C++ interpreter Ch to run OpenCV
  4. Within Visual Studio you may load OpenCV workspace:
    • opencv.dsw for Microsoft Visual Studio 6.0
    • opencv.sln for Miscrosoft Visual Studio .NET 2003
    • cbuilderx/opencv.bpgr for Borland C++ BuilderX
    • select cvsample project, build it and run. Look into the code and modify it as you wish.
  5. Also, scan through reference manuals - they contain some example code as well.
  6. Search OpenCV archives at http://groups.yahoo.com/group/OpenCV for the topic you are interesting in.

  7. Create a new project from scratch or modify existing cvsample. There are application wizards for Microsoft Visual Studio that create OpenCV-aware projects; look for them at http://groups.yahoo.com/group/OpenCV (Files section - you have to be registered OpenCV@yahoogroups.com user)

How do I send bug reports on the Intel® Ingegrated Performance Primitives

Send email to developer_support@intel.com

How do I join the OpenCV forum?

Send email to OpenCV-subscribe@yahoogroups.com , after you are a member and select your logon, you can read the web group at http://groups.yahoo.com/group/OpenCV. This mailing list is mirrored at gmane: http://news.gmane.org/gmane.comp.lib.opencv so you can read it with a usenet client.

How do I modify the web group so that I don't receive tons of email's?

To get the messages real time, or once a day as a daily digest, you can go to http://groups.yahoo.com/mygroups and choose your setting from the pull down list to the right of OpenCV.

Ok, I found the group completely useless for me. How can I unsubscribe?

Mail to OpenCV-unsubscribe@yahoogroups.com with subject [OpenCV] and arbitrary message contents.

When using OpenCV and IPL simultaneously, I get compiler errors. How to resolve this problem?

To be completely independent from IPL, OpenCV duplicates declarations of IplImage and few other structures and constants if it is not told explicitly that IPL is present. Defining HAVE_IPL before including OpenCV headers or putting "#include <ipl.h>" before OpenCV headers resolves the conflict.

Does OpenCV work on other processors?

Yes, OpenCV itself is open source and written in quite portable C/C++, it runs on other processors already and should be fairly easy to port (for example, there are already some CUDA optimizations on NVidia. On the other hand, OpenCV can sometimes run much faster on Intel processors (and sometimes AMD) because it can take advantage of SSE optimizations. OpenCV can be compiled statically with IPP libraries from Intel also which can speed up some function.

Does OpenCV still work with the optimized Intel IPP libraries?

a) Is it possible to get OpenCV 2.0 to use the IPP libraries?

b) Is it desirable to get OpenCV 2.0 to use the IPP libraries?

c) For best performance should we be using IPP, and if not, then what is the recommended practice for best performance?

d) Is there a list, and if not can we get a list, of OpenCV functions for versions 1.0 and 2.0 that can use IPP?

e) Why was the support for IPP dropped? Does the road map for OpenCV intend to steer clear of IPP in the future?

Where do I submit Bug reports?

  1. Submit them to this link

    • On the bugs page, click on the Add new button to add a bug.

    • NOTE: Bugs can includes documentation errors, missing documentation, as well as errors you find in the code.

      • Documentation bugs should be labled "documentation bug" and describe it (and its fix if you know it).

How can I get OpenCV2.2 to run in Python under Windows7?

  1. Install Python 2.7
  2. Download and run "OpenCV-2.2.0-win32-vs2010.exe"
  3. Copy "cv.lib" and "cv.pyd" from C:\OpenCV2.2\Python2.7\Lib\site-packages to the corresponding Python directory typically something like "C:\Python27\Lib\site-packages")
  4. Add "C:\OpenCV2.2\bin" to your Windows PATH

That should be it!

When I try to build one of the apps, I get an error, streams.h not found.

You need DirectShow SDK that is a part of Microsoft Platform SDK (note: It is no longer found in the DirectX SDK).

  1. Download DirectX SDK from msdn.microsoft.com/directx/ (It's huge, but you can download it by parts). If it doesn't work for you, consider HighGUI that can capture video using VFW or MIL
  2. Install it TOGETHER WITH SAMPLES.
  3. Open <DirectXSDKInstallFolder>\samples\C++\DirectShow\BaseClasses\baseclasses.{dsw|sln}. If there is no such file, it is that you either didn't install samples or the path has changed, in the latter case search for streams.h and open a workspace file located in the same folder.

  4. Build the library in both Release in Debug configurations.
  5. Copy the built libraries (in DirectX 9.x they are called strmbase.lib and strmbasd.lib) to <DirectXSDKInstallFolder>\lib.

  6. In Developer Studio add the following paths:
    • <DirectXSDKInstallFolder>\include <DirectXSDKInstallFolder>\samples\C++\DirectShow\BaseClasses to the includes' search path (at Tools->Options->Directories->Include files in case of Developer Studio 6.0) Add <DirectXSDKInstallFolder>\lib to the libraries' search path (at Tools->Options->Directories->Library files in case of Developer Studio 6.0) NOTE: PUT THE ADDED LINES ON THE VERY TOP OF THE LISTS, OTHERWISE YOU WILL STILL GET COMPILER OR LINKER ERRORS. This is necessary, because Developer Studio 6.0 includes some older DirectX headers and libs that conflict with new DirectX SDK versions.

    • (This solution works prior to DirectX 9 SDK. Now BaseClasses is included in the Platform SDK and don't forget to add strmiids.lib to linker options, otherwise you will get some unresolved symbol errors)

  7. Enjoy!

After installing DirectX SDK I'm still getting linker error about undefined or redefined "TransInPlace" filter class constructors etc.

Read the instructions from the previous answer, especially about the order of search directories.

When I use try to use cvcam, it just crashes

Make sure, you registered ProxyTrans.ax and SyncFilter.ax

How to register *.ax (DirectShow filter)?

Open the file (within explorer) using regsvr32.exe (under Win2000 it is done by Open with->Choose Program...->Browse...->c:\windows\system32\regsvr32.exe (path may be different). You may remember association to save clicks later.

Filter couldn't be registered (regsvr32 reports an error)

The most probable reason is that the filter requires some DLLs that are not in the path. In case of OpenCV make sure <OpenInstallFolder>\bin is in the path

cxcore096d.dll or cxcored.lib seem to be missing

cxcore096d.dll means debug version of cxcore DLL and cxcored.lib is the import library for cxcore096d.dll. Open OpenCV workspace, select "cxcore" as active project and select "Win32 Debug" configuration. Build the library and you will get bin\cxcore096d.dll and lib\cxcored.lib files. The same should be repeat for all the other OpenCV components - name of binary, ending with d means Debug version that are not shipped.

When compiling HighGUI I get the error message "mil.h is not found"

mil.h is a part of Matrox Imaging Library (MIL) that is usually supplied with Matrox (or compatible) framegrabbers, such as Meteor, Meteor II etc.

How can I debug DirectShow filter?

I get errors about missing DLLs (i.e. cxcore110.dll) when I try to run the sample EXEs or import opencv in Python

Make sure "c:\Program Files\OpenCV\bin" (or the equivalent for your installation) is included in your PATH variable.

  1. From the "Control Panel", open "System".
  2. In the "Advanced" tab click the "Environment Variables" button.
  3. In the "System Variables" section select "PATH" and click the "Edit" button.
  4. Append your "c:\Program Files\OpenCV\bin" (or equivalent) path to the end with a semicolon (";") as a separator.

How can I create DeveloperStudio project to start playing with OpenCV

(note: this is a lengthy answer)

To create your own OpenCV-based project in Developer Studio from scratch do the following:

  1. Within Developer Studio create new application:
    1. select from menu "File"->"New..."->"Projects" tab. Choose "Win32 Application" or "Win32 console application" - the latter is the easier variant and the both sample projects have this type.

    2. type the project name and choose location
    3. you may create own workspace for the project ("Create new workspace") or include the new project into the currently loaded workspace ("Add to current workspace").
    4. click "next" button
    5. choose "An empty project", click "Finish", "OK". After the above steps done Developer Studio will create the project folder (by default it has the same name as the project), <project name>.dsp file and, optionally, <project name>.dsw,.ncb ... files if you create own workspace.

  2. Add a file to the project:
    1. select from menu "File"->"New..."->"Files" tab.

    2. choose "C++ Source File", type file name and press "OK"
    3. add OpenCV-related #include directives:
      • #include "cv.h" #inlcude "cvaux.h" // experimental stuff (if need) #include "highgui.h"
      • Or, you may copy some existing file (say, opencv\samples\c\morphology.c) to the project folder, open it and add to the project (right click in editor view -> "Insert File into Project" -> <your project name> ).

  3. Customize project settings:
    1. Activate project setting dialog by choosing menu item "Project"->"Settings...".

    2. Select your project in the right pane.
    3. Tune settings, common to both Release and Debug configurations:
      • Select "Settings For:"->"All Configurations"

      • Choose "C/C++" tab -> "Preprocessor" category -> "Additional Include Directories:". Add comma-separated relative (to the .dsp file) or absolute paths to opencv\cxcore\include, opencv\cv\include, opencv\otherlibs\highgui and, optionally, opencv\cvaux\include.

      • Choose "Link" tab -> "Input" category -> "Additional library path:". Add the paths to all neccessary import libraries (cxcore[d].lib cv[d].lib hihghui[d].lib cvaux[d].lib)

    4. Tune settings for "Debug" configuration
      • Select "Settings For:"->"Win32 Debug".

      • Choose "Link" tab -> "General" category -> "Object/library modules". Add space-separated cvd.lib, highguid.lib, cvauxd.lib (optionally)

      • You may also want to change location and name of output file. For example, if you want the output .exe file to be put into the project folder, rather than Debug/ subfolder, you may type ./<exe-name>d.exe in "Link" tab -> "General" category -> "Output file name:".

    5. Tune settings for "Release" configuration
      • Select "Settings For:"->"Win32 Release".

      • Choose "Link" tab -> "General" category -> "Object/library modules". Add space-separated cv.lib, highgui.lib, cvaux.lib (optionally)

      • Optionally, you may change name of the .exe file: type ./<exe-name>.exe in "Link" tab -> "General" category -> "Output file name:".

  4. Add dependency projects into workspace:
    1. Choose from menu: "Project" -> "Insert project into workspace".

    2. Select opencv\cv\make\cv.dsp.
    3. Do the same for opencv\cvaux\make\cvaux.dsp, opencv\otherlibs\highgui\highgui.dsp.
    4. Set dependencies:
      • Choose from menu: "Project" -> "Dependencies..."

      • For "cv" choose "cxcore",
      • For "cvaux" choose "cv", "cxcore",
      • for "highgui" choose "cxcore",
      • for your project choose all: "cxcore", "cv", "cvaux", "highgui". The dependencies customization allows to automatically build debug versions of opencv libraries and rebuild the binaries if the sources are changed somehow.
  5. That's it. Now compile and run everything.

TODO: add more questions

How can I install OpenCV on Linux?

Many distros already have packages for OpenCV, like Ubuntu, Gentoo, Sabayon, Debian, Fedora, RedHat, SUSE (and OpenSUSE). Installing it on Ubuntu:

sudo apt-get install libcv1 libcv-dev libcvaux1 libcvaux-dev libhighgui1 libhighgui-dev

Installing it on Gentoo:

emerge -av opencv

Installing it on Sabayon:

equo install opencv

TODO: add info on how to install it on other distros

Windows has Visual Studio, which IDE should I use to program OpenCV on Linux?

There are many Linux IDE's for C programming on Linux, most notably:

TODO: add other IDE's and describe each one

Technical Questions on Library use:

How to access image pixels

(The coordinates are 0-based and counted from image origin, either top-left (img->origin=IPL_ORIGIN_TL) or bottom-left (img->origin=IPL_ORIGIN_BL)

I(x,y) ~ ((uchar*)(img->imageData + img->widthStep*y))[x]

I(x,y)blue ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3]
I(x,y)green ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3+1]
I(x,y)red ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3+2]

e.g. increasing brightness of point (100,100) by 30 can be done this way:

CvPoint pt = {100,100};
((uchar*)(img->imageData + img->widthStep*pt.y))[pt.x*3] += 30;
((uchar*)(img->imageData + img->widthStep*pt.y))[pt.x*3+1] += 30;
((uchar*)(img->imageData + img->widthStep*pt.y))[pt.x*3+2] += 30;

or more efficiently

CvPoint pt = {100,100};
uchar* temp_ptr = &((uchar*)(img->imageData + img->widthStep*pt.y))[pt.x*3];
temp_ptr[0] += 30;
temp_ptr[1] += 30;
temp_ptr[2] += 30;

I(x,y) ~ ((float*)(img->imageData + img->widthStep*y))[x]

I(x,y)c ~ ((T*)(img->imageData + img->widthStep*y))[x*N + c] '' or you may use macro CV_IMAGE_ELEM( image_header, elemtype, y, x_Nc ) '' I(x,y)c ~ CV_IMAGE_ELEM( img, T, y, x*N + c )

There are functions that work with arbitrary (up to 4-channel) images and matrices (cvGet2D, cvSet2D), but they are pretty slow.

How to access matrix elements?

The technique is very similar. (In the samples below i - 0-based row index, j - 0-based column index)

M(i,j) = ((float*)(mat->data.ptr + mat->step*i))[j]

Re M(i,j) = ((double*)(mat->data.ptr + mat->step*i))[j*2]
Im M(i,j) = ((double*)(mat->data.ptr + mat->step*i))[j*2+1]

M(i,j) ~ CV_MAT_ELEM( mat, float, i, j )

e.g. here is initialization of 3x3 identity matrix:

CV_MAT_ELEM( mat, float, 0, 0 ) = 1.f;
CV_MAT_ELEM( mat, float, 0, 1 ) = 0.f;
CV_MAT_ELEM( mat, float, 0, 2 ) = 0.f;
CV_MAT_ELEM( mat, float, 1, 0 ) = 0.f;
CV_MAT_ELEM( mat, float, 1, 1 ) = 1.f;
CV_MAT_ELEM( mat, float, 1, 2 ) = 0.f;
CV_MAT_ELEM( mat, float, 2, 0 ) = 0.f;
CV_MAT_ELEM( mat, float, 2, 1 ) = 0.f;
CV_MAT_ELEM( mat, float, 2, 2 ) = 1.f;

How do I apply a normalized/modified histogram back to an image?

This is called "back project". See "CalcBackProject" in CvReference : cv_imgproc_histograms.

How to process my data with OpenCV

Suppose, you have 300x200 32-bit floating point image/array, that resides in 60000-element array.

int cols = 300, rows = 200;
float* myarr = new float[rows*cols];

// step 1) initializing CvMat header
CvMat mat = cvMat( rows, cols,
                   CV_32FC1, // 32-bit floating-point, single channel type
                   myarr // user data pointer (no data is copied)
                   );
// step 2) using cv functions, e.g. calculating l2 (Frobenius) norm
double norm = cvNorm( &mat, 0, CV_L2 );

...
delete myarr;

Other scenaria are described in the reference manual. See cvCreateMatHeader, cvInitMatHeader, cvCreateImageHeader, cvSetData etc.

How to load and display image

/* usage: prog <image_name> */
#include "cv.h"
#include "highgui.h"

int main( int argc, char** argv )
{
    IplImage* img;
    if( argc == 2 && (img = cvLoadImage( argv[1], 1)) != 0 )
    {
        cvNamedWindow( "Image view", 1 );
        cvShowImage( "Image view", img );
        cvWaitKey(0); // very important, contains event processing loop inside
        cvDestroyWindow( "Image view" );
        cvReleaseImage( &img );
        return 0;
    }
    return -1;
}

How to find and process contours

Look at squares demo : http://www710.univ-lyon1.fr/~bouakaz/OpenCV-0.9.5/samples/c/squares.c Note: You might recieve an error as "too many arguments to function ‘char* cvGetSeqElem(const CvSeq*, int)’" If so, just edit the file and on lines 120,121,122 and 133 you can find the function cvGetSeqElem;

How to use 2 cameras (multiple cameras) with cvCam library

int ncams = cvcamGetCamerasCount( );    //returns the number of available cameras in the system

int* out; int nselected = cvcamSelectCamera(&out);

int cam1 = out[0];
int cam2 = out[1];

cvcamSetProperty(cam1, CVCAM_PROP_ENABLE, CVCAMTRUE);
cvcamSetProperty(cam1, CVCAM_PROP_RENDER, CVCAMTRUE);  //We'll render stream from this source
cvNamedWindow("Cam1", 1);
cvcamWindow MyWin1 = (cvcamWindow)cvGetWindowHandle("Cam1");
cvcamSetProperty(cam1, CVCAM_PROP_WINDOW, &MyWin1);   // Selects a window for  video rendering

//Same code for camera 2
cvcamSetProperty(cam2, CVCAM_PROP_ENABLE, CVCAMTRUE);
cvcamSetProperty(cam2, CVCAM_PROP_RENDER, CVCAMTRUE);
cvNamedWindow("Cam2", 1);
cvcamWindow MyWin2 = (cvcamWindow)cvGetWindowHandle("Cam2");
cvcamSetProperty(cam2, CVCAM_PROP_WINDOW, &MyWin1);

//If you want to open the property dialog for setting the video format parameters, uncomment this line
//cvcamGetProperty(cam1, CVCAM_VIDEOFORMAT, NULL);
//cvcamGetProperty(cam2, CVCAM_VIDEOFORMAT, NULL);

cvcamSetProperty(cam1, CVCAM_STEREO_CALLBACK , stereocallback); //stereocallback is the function running to process every frames

cvcamInit();
cvcamStart();

//Your app is working
while (1)
{
     int key = cvWaitKey(5);
     if (key == 27) break;
}

cvcamStop( );
cvcamExit( );

void stereocallback(IplImage* image1, IplImage* image2) {

}

Getting started with Python

Where can I get help?

There are quite a few samples included in the opencv source tarball, you should read those. Also consult PythonInterface.

I'm used to Matlab, is there a way to use matlab syntax?

Yes, there is a file matlab_syntax.py that comes with the opencv python bindings. It contains some of the most used matlab functions.

How do I convert an OpenCV image to a numpy array?

There is a file adaptors.py that helps you convert PIL images and Numpy arrays to and from OpenCV images. You can find it in the source tarball.


OpenCVWiki: faq (last edited 2011-07-02 15:43:22 by DMahalko)