Sunday, December 7, 2014

Image processing Practicals - Part 1 (Configure OpenCV 2.4.9 and C++ with Eclipse IDE using CMake and MinGW)

Today onwards I'm going to write a new post series about image processing practicals. Let's start from the basics.

First we have to select a proper tool/language to work with image processing. There are number of languages and tools available for image processing. Each one have unique advantages and disadvantages. Please refer to this for more details. I prefer using  OpenCV computer vision library  with C++.

If you are using linux you can follow this tutorial to configure OpenCV with Eclipse IDE.

If you are using windows, configuring Opencv might be a challenging task some times.So follow these steps.

1. Download OpenCV and extract it. In this tutorial I use OpenCV-2.4.9. (But this tutorial may valid for  other recent  versions of OpenCV as well.)(http://opencv.org/downloads.html)


2. Download and Install MinGW. You can use the installer provided by MinGW. Add MinGW bin folder to the system path. Ex: C:\MinGW\bin.
(Its always a good practice to restart the computer after editing the system path) .(http://sourceforge.net/projects/mingw/files/)

3. There are pre-build OpenCV binaries available in the pack that you have downloaded in step 1. But some times those are not going to work properly with MinGW. So we are going to create our own OpenCV binaries. For that you need to download and install CMake  and add CMake bin folder to the system path.

4. Open CMake GUI and set source path to opencv/sources folder.

5. Create a new folder named "myBuild" inside opencv folder and set build path to that folder.




6. Click on configure button and from the drop down menu select MinGW Makefiles and click finish.


7. At this time you can see the progress bar is moving. When it's completed you will see a check box area with a red background. Here you can select what to build. In my case I didn't change any of the check box values. Press configure button again.


8. Then above mentioned red background will be changed to white. Then click generate button.


9. Download Eclipse IDE  for C/C++ developers and install it.(http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/heliossr2)



10. Open Eclipse IDE and go create a new C/C++ project. In Toolchains section select MinGW GCC.


11.In order to verify that your MinGW environment works properly, Build hello world C++ code and make sure it builds properly.

12. Now we are going to build the contents in myBuild folder. Open a command line window and go to myBuild  folder. Type the command "mingw32-make" without quotation marks and press enter. This will take some time. After this process completion set the system path to myBuild/bin folder. So now you have generated your own OpenCV  binaries.

13. Now we are going to create a test project and make sure our binaries are working properly. In Eclipse IDE, Go to project --> properties --> C/C++ build --> settings.

14. In Tool Settings section under the GCC  C++ Compiler branch select the includes folder. Add the opencv include directory to Include paths (-I). Ex: "F:\OpenCV\build\include\"



15. Go to MinGW C++ Linker section and add myBuild\lib folder path to the Library search path (-L) section under Libraries folder. Ex: F:\opencv\myBuild\lib

16. Go to MinGW C++ Linker section and add followings  to the Libraries (-l) section under Libraries folder(One line at a time). Note: Each and every line is ending with a number which corresponds to the OpenCV version that your are using. So you may have to replace it corresponding to your OpenCV version. You can check it by going to the lib folder used in 15th step. In this tutorial we are using OpenCV 2.4.9. So the corresponding number is 249.

 opencv_calib3d249  
 opencv_contrib249  
 opencv_core249  
 opencv_features2d249  
 opencv_flann249  
 opencv_gpu249  
 opencv_highgui249  
 opencv_imgproc249  
 opencv_legacy249  
 opencv_ml249  
 opencv_objdetect249  
 opencv_ts249  
 opencv_video249  


Note: All these are not necessary to run a basic OpenCV program. In most cases you will only need opencv_core249 and opencv_highgui249.



17. Create a folder named src and create a C++ source file in it( In my case its opencv.cpp).

18. Add this code to the C++ file.

1:  #include <opencv2/core/core.hpp>  
2:  #include <opencv2/highgui/highgui.hpp>  
3:  #include <iostream>  
4:  using namespace std;  
5:  using namespace cv;  
6:  int main() {  
7:   Mat image;  
8:   image = imread("lena.jpg", CV_LOAD_IMAGE_COLOR);  
9:   if (!image.data) {  
10:   cout << "No image data \n";  
11:   return -1;  
12:   }  
13:   namedWindow("Display Image", CV_WINDOW_AUTOSIZE);  
14:   imshow("Display Image", image);  
15:   waitKey(0);  
16:   return 0;  
17:  }  



19. Download the test image(lena.jpg.) and place it in the project folder. (In the root of the project - See my project structure in 17th step).

20. Build and run the C++ code. If you get following output, congratulations!!!.




0 comments:

Post a Comment