Configuring Code::Blocks to use OpenCV in Linux Environments

A quick guide to setting up and installing OpenCV for using in the Code::Blocks integrated development environment in Linux. The version of Linux I am currently using is Ubuntu 14.04. At the time of writing the version of OpenCV for Linux used is 2.4.9. (I had originally tried version 2.4.10 but had problems compiling it with the version of gcc I had (4.8.2), so I reverted to 2.4.9 instead.)

Step 1: Download and extract OpenCV for Linux

Versions of OpenCV can be downloaded from here:

http://opencv.org/downloads.html

Save it to the location of your choice. Open a command prompt, navigate to the download location and unzip:

opencv_codeblocks1

Step 2: Create an OpenCV build directory

Navigate to the OpenCV directory, and use

mkdir

to create the build directory:

opencv_codeblocks2

Step 3: Use CMake to build OpenCV

Navigate to the ‘build’ directory created and run the following CMake command:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

opencv_codeblocks3

And then run

make

command, which can take a while to complete:

opencv_codeblocks9

Followed by the

sudo make install

command:

opencv_codeblocks10

Step 4: Create a Code::Blocks Project

Open Code::Blocks and select File > New > Project. Select Empty Project:

opencv_codeblocks4

Give the project a name:

opencv_codeblocks5

And finish with the default settings:

opencv_codeblocks6

Once the project is created, select File > New > Empty File, to add the main.cpp file to the project. Copy and paste the following code example to main.cpp:

// DetectBlobs.cpp : Defines the entry point for the console application.
//
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

int main()
{
    // Initialise
    //std::string filepath = "spots.bmp";
    std::string filepath = "spots2.jpg";
    int num_blobs = 0;

    // Load grayscale version of coloured input image
    IplImage* original  = cvLoadImage( filepath.c_str() );
    IplImage* grayscale = cvLoadImage( filepath.c_str(),
                                       CV_LOAD_IMAGE_GRAYSCALE );

    // Check bitmap image exists
    assert( grayscale );

    // Create IplImage struct for a black and
    // white (binary) image
    IplImage* img_bw = cvCreateImage( cvGetSize( grayscale ),
                                      IPL_DEPTH_8U,
                                      1 );

    // Use thresholding to convert grayscale image
    // into binary
    cvThreshold( grayscale,             // source image
                 img_bw,                // destination image
                 40,                    // threhold val.
                 255,                   // max. val
                 CV_THRESH_BINARY );    // binary

    // Create IplImage struct for inverted black
    // and white image
    IplImage* img_bw_inv = cvCloneImage( img_bw );
    IplImage* img_bw_cpy = cvCloneImage( img_bw );

    // Find connected components using OpenCV
    CvSeq* seq;
    CvMemStorage* storage = cvCreateMemStorage( 0 );
    cvClearMemStorage( storage );

    // cvFindContours the 12 + 1 extra object for
    // white backgrounds and black spots, hence
    // subtract 1
    num_blobs = cvFindContours( img_bw,
                                storage,
                                &seq,
                                sizeof( CvContour ),
                                CV_RETR_LIST,
                                CV_CHAIN_APPROX_NONE,
                                cvPoint( 0, 0 ) ) - 1;

    // Display the input / output windows and images
    cvNamedWindow( "original" );
    cvShowImage( "original", original );

    cvNamedWindow( "grayscale" );
    cvShowImage( "grayscale", grayscale );

    cvNamedWindow( "black_and_white" );
    cvShowImage( "black_and_white",
                  img_bw_cpy );

    // Wait for user key press and then tidy up
    cvWaitKey(0);

    cvReleaseImage( &original );
    cvReleaseImage( &grayscale );
    cvReleaseImage( &img_bw );
    cvReleaseImage( &img_bw_inv );
    cvReleaseImage( &img_bw_cpy );

    cvDestroyWindow( "greyscale" );
    cvDestroyWindow( "black_and_white" );
    cvDestroyWindow( "inverted" );

    return 0;
}

Step 5: Configure project settings

Right click the project folder and select Build Options…

Select the Search Directories tab and then select the Compiler tab. Select the Add button in order to set the location of the include files:

opencv_codeblocks7

Then select the Linker tab and then Add button in order to set the location of the OpenCV libraries, in the build/lib folder that was created using CMake earlier:

opencv_codeblocks8

Select the Linker Settings tab and set the libraries that you will need to include, in this example these are core, highgui and imgproc:

opencv_codeblocks11

That should be sufficient to enable the project to build correctly.

Step 6: Try it!

This is the “spots2.jpg’ image used in this test program (obviously, save the file in a place where your Code::Blocks project can find it):

spots2

And this screenshot shows the results of running the program, giving us the original colour image, the gray-scale equivalent image and the thresholded black and white image (and hence detect the number of spots in the jpg image):

opencv_codeblocks12

Latest Comments

  1. Adarsh 13 November 2015
  2. Vishal 26 February 2016
  3. Akarsh Seggemu 13 September 2016
  4. danie1 22 September 2016
  5. Baltazar_le 3 November 2016
  6. Himj 4 October 2017

Leave a Reply