|Submitted by jestin on Sun, 01/11/2009 - 19:55.|
OpenCV is an open source "computer vision" library, which means that it can be used to extract information from an image, or a series of images (like a video). A computer vision library has many uses, but my favorite is in the field of robotics.
The Cowtown Computer Congress has started an OpenCV Study Group twice a month, and I would like a common development platform for everyone in the group to use. This has led me to consider the Eclipse IDE. Eclipse was originally developed for the Java Programming Language, but using the Eclipse CDT it can be used for C++ development as well. Eclipse is cross platform, and has a huge following in both the open source and the corporate communities (for example, Google uses Eclipse for quite a bit). Since we don't want to dictate what the OpenCV Study Group participants use in terms of an operating system, I think Eclipse would a good choice, just to keep everyone on the same page.
I am not going to go into details about installing OpenCV, installing Eclipse, nor about installing the CDT which we will be using. There are other and better tutorials for doing that. I will cover how to set up a simple OpenCV project in Eclipse, so that studiers can get writing code as soon as possible.
To install OpenCV, check these links:
To install Eclipse, check these links:
*NOTE: If you are using an operating system with software repositories that include Eclipse, use the repository to install, as it will be much cleaner and easier.
To install the Eclipse CDT, check this link:
At this point, I will assume that you have OpenCV, Eclipse, and the Eclipse CDT already installed. Now it is time to create our project. Start up Eclipse. You should see something like this:
Now go to File->New->Project..., and you will get this dialog:
Select Managed Make C++ Project under the C++ tree item, and click Next. You will now be prompted to enter the name of the project:
Enter the name of the project, and click next.
Make sure you have "Executable" selected as your project type. Click Next again to see the Additional Project Settings page, and then click Finish.
You will be asked if you want to open the C/C++ perspective. Click Yes.
You should now be back at the main Eclipse window. Make sure that your new project is selected in the C/C++ Projects tab, and the go to Project->Properties.
Select C/C++ Build from the menu at the far left, and then go to the Tool Settings tab, and select Directories under GCC C++ Compiler. On the right, add a new Include Path at /usr/include/opencv (or wherever you have the opencv include files installed).
Now select Libraries under GCC C++ Linker. Add 'cv' and 'highgui' to the libraries, and add a library path to /usr/local/lib (or wherever your OpenCV library files are installed).
Do add these paths and libraries for both the Debug and Release builds of your project. Click OK to continue.
Now right click on your project listed on the left, and select New->Source Folder. Enter 'src' as the folder name, and click Finish. Now right click on the newly created folder and select New->Source File. Name the new file 'main.cpp' and click Finish.
You can now enter code into your main.cpp file. For a quick test, just enter the code of the image.cpp file in the OpenCV samples folder. Here is the code:
// The short example shows how to use new-style image classes declared in cxcore.hpp.
// There is also a very similar matrix class (CvMatrix) - a wrapper for CvMat
int main( int argc, char** argv )
// load image in constructor: the image can be loaded either from bitmap (see cvLoadImage),
// or from XML/YAML (see cvLoad)
CvImage img(argc > 1 ? argv : "lena.jpg", 0, CV_LOAD_IMAGE_COLOR),
img_yuv, y, noise;
CvRNG rng = cvRNG(-1);
if( !img.data() ) // check if the image has been loaded properly
img_yuv = img.clone(); // clone the image
// (although, later the content will be replaced with cvCvtColor,
// clone() is used for simplicity and for the illustration)
cvCvtColor( img, img_yuv, CV_BGR2YCrCb ); // simply call OpenCV functions and pass the class instances there
y.create( img.size(), IPL_DEPTH_8U, 1 ); // another method to create an image - from scratch
noise.create( img.size(), IPL_DEPTH_32F, 1 );
cvSplit( img_yuv, y, 0, 0, 0 );
cvRandArr( &rng, noise, CV_RAND_NORMAL, cvScalarAll(0), cvScalarAll(20) );
cvSmooth( noise, noise, CV_GAUSSIAN, 5, 5, 1, 1 );
cvAcc( y, noise );
cvConvert( noise, y );
cvMerge( y, 0, 0, 0, img_yuv );
cvCvtColor( img_yuv, img, CV_YCrCb2BGR );
cvNamedWindow( "image with grain", CV_WINDOW_AUTOSIZE );
img.show( "image with grain" ); // .show method is the conveninient form of cvShowImage
// all the images will be released automatically
To compile, simply save the project. If all went okay, you should have no errors. If you have errors, check to make sure that your include and library paths are correct.
To run the program, you will have to set up a run configuration. Don't worry, this is easy. Just go to Run->Run.. and you will get the Run dialog. Double click C/C++ Local Application item on the left, and you should see your project appear under it. Select this project, and click the Browse button next to the C/C++ Application field. Select your project's executable.
For the Image sample program to run, you will need an image file called 'lena.jpg' located in your project's root directory. You can copy this file from the OpenCV sample folder, or create your own. If you want to get fancy, you can re-edit your run configuration to use a file you pass in from the command line. Click Run to see your program execute.
I hope that helps everyone, and I can't wait to see you at CCCKC's OpenCV Study Group!