Sunday, January 13, 2013

Configuring CodeBlocks 12.11 IDE for use with OpenCV 2.4.3

I have gone through so much pain trying to get CodeBlocks being able to properly use the OpenCV libraries on my system so I decided to write a little something about it. I have Ubuntu 12.04 LTS with CodeBlocks 12.11 (latest stable) and OpenCV 2.4.3 libraries. Follow these steps to setup CodeBlocks IDE to use OpenCV 2.4.3.
  1. Make sure that you have OpenCV installed on your system. There is an excellent guide to follow here that will walk you through the process. You can test your installation by following this guide here.
  2. You can find out where OpenCV and its libraries are installed on your system by using these commands in bash:
            pkg-config --cflags opencv
        pkg-config --libs opencv 
  3. Now you can create a new C++ console project in CodeBlocks.
  4. With the new project, right click on the project in the left bar and choose "Build Options..".
  5. Click on the "Search Directories" tab, then Compiler tab, and add these locations:
            /usr/local/include/opencv
        /usr/local/include/opencv2
  6. Still on the "Search Directories" tab, then click on the the Linker tab and add these locations:
            /usr/local/lib
  7. Then click on the "Linker Settings" tab and be sure to add all of the OpenCV libraries that where shown from step 2. This is a list of all the libraries that I had installed on my system:
            /usr/local/lib/libopencv_calib3d.so
        /usr/local/lib/libopencv_contrib.so
        /usr/local/lib/libopencv_core.so
        /usr/local/lib/libopencv_features2d.so
        /usr/local/lib/libopencv_flann.so  
        /usr/local/lib/libopencv_gpu.so
        /usr/local/lib/libopencv_highgui.so
        /usr/local/lib/libopencv_imgproc.so
        /usr/local/lib/libopencv_legacy.so  
        /usr/local/lib/libopencv_ml.so
        /usr/local/lib/libopencv_nonfree.so
        /usr/local/lib/libopencv_objdetect.so
        /usr/local/lib/libopencv_photo.so
        /usr/local/lib/libopencv_stitching.so
        /usr/local/lib/libopencv_ts.so
        /usr/local/lib/libopencv_video.so
        /usr/local/lib/libopencv_videostab.so
  8. Once you have all that taken care of you can now go ahead and edit the main.cpp source code file. Copy and paste this sample code to test out your setup:
    #include <stdlib.h>
    #include <cv.hpp>
    #include <cxcore.hpp>
    #include <highgui.h> 
    
    int main(int argc, char** argv)
    {
        cv::Mat frame;
        int c;
        CvCapture *capture = cvCaptureFromCAM(0);
        while(1)
        {
            frame = cvQueryFrame(capture);
            cv::imshow("OpenCV",frame);
    
            c = cv::waitKey(10);
            if(c==27)
                break;
        }
        return 0;
    }
    
    
    
    
  9. Done! You can now build and run the sample application to make sure it's working. You should get a new window displaying the image from your webcam. You should now be able to copy the project and use it as a template for future work.

5 comments:

  1. Thank you very much..
    This tutorial solved the configutaion problem. :)

    ReplyDelete
  2. There are 4-5 libraries (.so) files that are present in the /usr/local/lib directory but don't show up on doing pkg-config --libs opencv.
    What is the harm in adding these libraries to the "Linker Settings" tab?
    (I just want to understand why we're doing what we're doing)

    ReplyDelete