How to setup CodeBlocks for OpenCV programs



In this tutorial i will be using OpenCV v2.0 and Code Blocks v10.05 with GNU compiler on Windows 7 operating system. Using OpenCV v2.2? Then you can check this post "Setup OpenCV 2.2 in codeblocks"


To work on OpenCV with Code Blocks you just need to add some libraries. Following are some simple steps that I followed.

  1. First of all i assume you have downloaded the OpenCV package and installed in your system. ( Not exactly install, just extract ) If not you can Download it from here. 
  2. Make sure the environment variable PATH is set. This could be done during the installation procedure itself. It will prompt you with this question. If you missed that then you have to manually add it. 
  3. Considering you have the OpenCV installed in your C drive, I continue with the method.
  4. Open Code Blocks.
  5. Goto menu Settings > Compiler and Debugger > Search Directories
  6.  There goto Add and add the directory C:\OpenCV2.0\include\opencv


    7.    In the Linker tab add the directory C:\OpenCV2.0\lib
            

    8.  Next click on the Linker Settings. Add all the .lib files from C:\OpenCV2.0\lib\


   9.  That's it!! Now you are ready to run your first OpenCV program. I tested a sample program demhist.c 
         from C:\OpenCV2.0\samples\c and the output looks something like this.



   Sometimes there are chances that even after doing the above setup, codeblocks gives an error saying library is missing. In that case copy all the .dll files from C:\OpenCV2.0\bin folder to your project folder ( Where your program file is present ). That should do the job!


You can also check my post  "OpenCV Tutorial References" where i have listed out some important links which could help you in understanding OpenCV better.


Please leave a comment so that know if this post is helpful or not. Also mention if any changes is required. It could help others :)

7 comments:

Unknown said...

Hi im a Newbie at openCV
And used your guide to set openCV up on codeblocks and it worked so fare. Im wrighting the program from O´Reillys learning openCV, page 17. Just to see if it works, but I get this error when I run the program that it can´t find libcxcore200.dll file

“the program cant start because libcxcore200.dll Is missing on the computer. install it to solve the problem.”

But I have followed your guide and cant figure out were I fucked up. Can you please help me?

Adithya said...

Just add C:\OpenCV2.0\bin into your PATH environment variable

or

When you install OpenCV,

Choose the option, Add OpenCV to the system PATH for current user which is not default one

danel said...

thanks ...

Enric Boix said...

Yes!! I've been trying to do this for a week now, and it finally worked!!!!!!!! Thank you so much!

Sid said...

trying to run this program on codeblock v10.05 and opencv v2.2
and getting error as:
undefined reference to 'cv::namedwindow(std::string const&,int);
and so on.. please help me...


#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include

using namespace cv;
using namespace std;

int _brightness = 100;
int _contrast = 100;

Mat image;

/* brightness/contrast callback function */
void updateBrightnessContrast( int /*arg*/, void* )
{
int histSize = 64;
int brightness = _brightness - 100;
int contrast = _contrast - 100;

/*
* The algorithm is by Werner D. Streidt
* (http://visca.com/ffactory/archives/5-99/msg00021.html)
*/
double a, b;
if( contrast > 0 )
{
double delta = 127.*contrast/100;
a = 255./(255. - delta*2);
b = a*(brightness - delta);
}
else
{
double delta = -128.*contrast/100;
a = (256.-delta*2)/255.;
b = a*brightness + delta;
}

Mat dst, hist;
image.convertTo(dst, CV_8U, a, b);
imshow("image", dst);

calcHist(&dst, 1, 0, Mat(), hist, 1, &histSize, 0);
Mat histImage = Mat::ones(200, 320, CV_8U)*255;

normalize(hist, hist, 0, histImage.rows, CV_MINMAX, CV_32F);

histImage = Scalar::all(255);
int binW = cvRound((double)histImage.cols/histSize);

for( int i = 0; i < histSize; i++ )
rectangle( histImage, Point(i*binW, histImage.rows),
Point((i+1)*binW, histImage.rows - cvRound(hist.at(i))),
Scalar::all(0), -1, 8, 0 );
imshow("histogram", histImage);
}
void help()
{
std::cout << "\nThis program demonstrates the use of calcHist() -- histogram creation.\n"
<< "Usage: \n" << "demhist [image_name -- Defaults to baboon.jpg]" << std::endl;
}

const char* keys =
{
"{1| |baboon.jpg|input image file}"
};

int main( int argc, const char** argv )
{
help();

CommandLineParser parser(argc, argv, keys);
string inputImage = parser.get("1");

// Load the source image. HighGUI use.
image = imread( inputImage, 0 );
if(image.empty())
{
std::cerr << "Cannot read image file: " << inputImage << std::endl;
return -1;
}

namedWindow("image", 0);
namedWindow("histogram", 0);

createTrackbar("brightness", "image", &_brightness, 200, updateBrightnessContrast);
createTrackbar("contrast", "image", &_contrast, 200, updateBrightnessContrast);

updateBrightnessContrast(0, 0);
waitKey();

return 0;
}

Adithya said...

@Sid Seems like you have no linked the libraries properly.

Unknown said...

Thank You so much..

Post a Comment