Skip to content

Instantly share code, notes, and snippets.

@talayhan
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save talayhan/f20f73d20b4485b29c12 to your computer and use it in GitHub Desktop.

Select an option

Save talayhan/f20f73d20b4485b29c12 to your computer and use it in GitHub Desktop.
OpenCV sample code for testing CodeBlocks

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.

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.

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**

Now you can create a new C++ console project in CodeBlocks. With the new project, right click on the project in the left bar and choose "Build Options..".


Click on the "Search Directories" tab, then Compiler tab, and add these locations:

   /usr/local/include/opencv
   /usr/local/include/opencv2

Still on the "Search Directories" tab, then click on the the Linker tab and add these locations:

   /usr/local/lib

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

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

====================================================

using namespace cv;

int main(int argc, char *argv[])
{
    Mat img = imread("lena.jpg", CV_LOAD_IMAGE_COLOR);
    if(img.empty())
       return -1;
    namedWindow( "lena", CV_WINDOW_AUTOSIZE );
    imshow("lena", img);
    waitKey(0);
    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment