Monday, May 2, 2011

Setting up OpenCV in Eclipse on Windows (OpenCV2.2)

The latest release of OpenCV (version 2.2) has undergone dramatic changes to the library. Because of these changes, my older guide is now outdated. For OpenCV version 2.2, please use this guide.

First download and setup the basics:
  • Get MinGW
  • Download Eclipse C/C++ IDE
Note: I have a tutorial on these two steps already so just head over HERE for more info.

  • Next download/install OpenCV 2.2
  • Now launch Eclipse and start a new project by going to:
  1. File->New->C++ Project (or File->New->C Project)
  2. Give your project a name in the "Project name" box
  3. Select the "Hello World" option under the "Project Type" section under the"Executable" folder. I recommend this over the "Empty Project" as it creates the c/c++ file for you instead of having to do it manually (it also creates a "src" folder and a"Debug" folder which helps keep things a little more organized)
  4. Make sure the "MinGW" Toolchain is selected in the "Toolchains" section
  5. Hit NEXT
  6. Fill in your Author and other file information, then hit NEXT
  7. In the next window select "Advanced settings...". This will bring you to the "Project Settings" which can always be accessed later by going to Project->Properties
  8. Under the "C/C++ Build" Section go to the "Settings" and select the "Tool Settings"Tab. Then select the "Includes" folder (on older versions of eclipse it is the "Directories" folder) in the GCC Compiler branch and add the opencv include directory to Include paths (-I): "C:\OpenCV2.2\include\". Of course, change C:\OpenCV2.2 to match the installed path that you used.
  9. Now, under the MinGW Linker select the "Libraries" folder and add the following to the Libraries (-l) section (note not all of these are necessarily needed for your project but these are all the libraries available in opencv version 2.2):
  • opencv_calib3d220
  • opencv_contrib220
  • opencv_core220
  • opencv_features2d220
  • opencv_ffmpeg220
  • opencv_flann220
  • opencv_gpu220
  • opencv_highgui220
  • opencv_imgproc220
  • opencv_legacy220
  • opencv_ml220
  • opencv_objdetect220
  • opencv_ts220
  • opencv_video220
In most cases you will only need opencv_core220 and opencv_highgui220 to get started

NOTE: Versions 2.2 and later postfix the libraries names with a three digit number that corresponds to the version of OpenCV that you are linking to.

FINALLY, under the "Library search path (-L)" section add:
  • "C:\OpenCV2.2\lib"
  1. Hit OK when done
  2. Hit Finish to create and start the Project
Write up a simple program to test if opencv works properly. You can use this EXAMPLE CODE I made to test if your video camera and environment are working properly.

NOTE: The way this guide was written will require you to use a relative path to specify the header files you wish to include. OpenCV breaks up the library into two sections, "legacy libraries" and "new libraries".

If you wish to specify one of the new opencv libraries use the following format:

#include "opencv2\[subfolder]\[library.hpp]"

ADDITIONALLY, if you wish to include all of the primary opencv2 header files in a project you can use:

#include "opencv2\opencv.hpp"

Note: opencv.hpp does not include every library in OpenCV2.2. It may be helpful for you to view the "opencv.hpp" library (located by default in: C:\OpenCV2.2\include\opencv2\opencv.hpp) to see what is included and how you can reference the new libraries in your code. To see the complete list of libraries available, go to "C:\OpenCV2.2\include\" and browse around the directory.

Legacy libraries can be defined by: #include "opencv\[library.hpp]"


If you get an error, during compile time, pertaining to:
__exchange_and_add
Then visit this page and follow the instructions under:
Building on Windows using MinGW 3.4.5

Hope this helps!

23 comments:

  1. Thanks a lot.. It is the MOST helpful article in OpenCV.
    Finally I got red of the missing cv.h problem.
    But I got this error in a sample file I'm testing:

    error: 'CvImage' was not declared in this scope
    error: expected ';' before 'img'
    error: 'img' was not declared in this scope
    error: 'img_yuv' was not declared in this scope
    ...
    ...
    I'm new to OpenCV, can u help in this?

    ReplyDelete
  2. Also, the example code u provided compiles without errors.

    ReplyDelete
  3. Hi doaa,
    Thanks for the feedback. It's tough to debug a problem without seeing code, but the second error suggests that you have a syntax error. As you said in your second post, the code I provided complied without error; so as far as the environment is concerned..everything is in working order.

    The other errors seem to suggest that it might have to do with your "includes". You might want to double check that the libraries that you are using contain the CvImage data type. I am a little rusty with opencv, but I cannot recall a CvImage data type, are you certain you didn't mean IplImage?

    -Jon

    ReplyDelete
  4. Actually the codes using IplImage works, but CvImages don't.. I think the latter are older codes, maybe changed in ver. 2,2 !!

    Thanks for ur help.

    btw, this article should be the top on google search for openCV installation.. how can I promote it! :)

    ReplyDelete
  5. you forgot one letter

    "opencv_improc220" is really "opencv_imgproc220"

    ReplyDelete
  6. Jacques,
    Thanks for pointing that out. I have updated the post to reflect that.
    Thanks again,
    Jon

    ReplyDelete
  7. First, thanks for the tutorial.

    I have followed the tutorial but always getting the error: Type 'IplImage' could not be resolved for IplImage* img = 0;

    Why is that?

    Thanks.

    ReplyDelete
  8. Hi Abder-Rahman,
    IplImage* img = 0; is a valid statement so I am not quite sure what the issue might be. Are you using opencv 2.2 or 2.3? I haven't tried 2.3 using this guide so there is a chance that they changed the layout again with opencv. Have you double checked your includes and linker settings? I tried to replicate a similar error to the one you mentioned and I couldn't produce that type of error. Have you tried compiling my code that I provided above or using one of the samples in opencv samples\c\ folder?

    ReplyDelete
  9. Hello!
    I m using opencv2.2 with eclipse c/c++.
    I tried the following program:

    #include "C:\OpenCV2.2\include\opencv2\opencv.hpp"
    #include "C:\OpenCV2.2\include\opencv2\highgui\highgui.hpp"


    int main( int arc, char ** argv)
    {
    int W = 640;
    int H = 480;


    IplImage *image = cvCreateImage(cvSize(W, H), IPL_DEPTH_8U, 1);


    cvNamedWindow("My First OpenCV Program", 1);
    cvShowImage("My First OpenCV Program", image);


    cvWaitKey(0);
    cvReleaseImage(&image);
    return 0;
    }

    I have followed various tutorials for installing opencv with eclipse but getting the error:



    **** Build of configuration Debug for project OpenCV ****

    **** Internal Builder is used for build ****
    gcc -IC:\OpenCV2.2\include -O0 -g3 -Wall -c -fmessage-length=0 -osrc\main.o ..\src\main.c
    In file included from C:\OpenCV2.2\include\opencv2\opencv.hpp:56:0,
    from ..\src\main.c:2:
    C:\OpenCV2.2\include/opencv2/ml/ml.hpp:2075:15: fatal error: map: No such file or directory
    compilation terminated.
    Build error occurred, build is stopped
    Time consumed: 2531 ms.

    plx tell me how to resolve it. I shall be very thankful...

    ReplyDelete
  10. Hi Rimi,
    I tried to compile and run your program and I saw no issues, the program ran without error and produced expected results.

    Apparently your project can't find the ml.hpp file. Make sure that ml.hpp exists in the path:
    "C:\OpenCV2.2\include\opencv2\ml\ml.hpp"

    If it doesn't exist...well that would be your problem and you probably would want to reinstall opencv; however if it does exist, make sure your C/C++ Build Settings are correct. As a last resort you can copy and paste the ml.hpp file that resides in that path to your project's folder and then comment out the #include in opencv.hpp that declares ml.hpp and then manually declare ml.hpp in your main cpp.

    As a side note, it is not needed to include:
    #include "C:\OpenCV2.2\include\opencv2\highgui\highgui.hpp"
    since opencv.hpp declares this library for you. But this shouldn't cause any problems if you choose to leave it as is.

    Hope this helps,
    Jon

    ReplyDelete
  11. I agree with the commenters above, this is the best help with openCV that I've found. I downloaded your code, but when I open it in my test project that I created according to the instructions above, there are several errors.

    These are the errors I get. Any help would be great

    Description Resource Path Location Type
    Function 'cvCreateCameraCapture' could not be resolved opencv_test.cpp /OpenCVDemo/src line 10 Semantic Error
    Function 'cvDestroyWindow' could not be resolved opencv_test.cpp /OpenCVDemo/src line 27 Semantic Error
    Function 'cvNamedWindow' could not be resolved opencv_test.cpp /OpenCVDemo/src line 8 Semantic Error
    Function 'cvQueryFrame' could not be resolved opencv_test.cpp /OpenCVDemo/src line 15 Semantic Error
    Function 'cvReleaseCapture' could not be resolved opencv_test.cpp /OpenCVDemo/src line 26 Semantic Error
    Function 'cvShowImage' could not be resolved opencv_test.cpp /OpenCVDemo/src line 19 Semantic Error
    Function 'cvWaitKey' could not be resolved opencv_test.cpp /OpenCVDemo/src line 20 Semantic Error
    Symbol 'CV_WINDOW_AUTOSIZE' could not be resolved opencv_test.cpp /OpenCVDemo/src line 8 Semantic Error
    Type 'CvCapture' could not be resolved opencv_test.cpp /OpenCVDemo/src line 9 Semantic Error
    Type 'IplImage' could not be resolved opencv_test.cpp /OpenCVDemo/src line 7 Semantic Error

    ReplyDelete
  12. Scratch that. I got it. I was telling it to look for the library files in the lib directory, but it needed to be looking in the bin directory.

    Note: I am using openCV 2.3

    ReplyDelete
  13. Okay I thought it worked, but now there are no errors being displayed. In the console window it says the project isn't be built. This is the output:




    **** Rebuild of configuration Release for project OpenCVDemo ****

    **** Internal Builder is used for build ****
    g++ -IC:\OpenCV2.3\build\include -O3 -Wall -c -fmessage-length=0 -o src\opencv_test.o ..\src\opencv_test.cpp
    g++ -LC:\OpenCV2.3\build\x64\mingw\bin -o OpenCVDemo.exe src\opencv_test.o -llibopencv_calib3d230 -llibopencv_contrib230 -llibopencv_core230 -llibopencv_features2d230 -llibopencv_flann230 -llibopencv_gpu230 -llibopencv_highgui230 -llibopencv_imgproc230 -llibopencv_legacy230 -llibopencv_ml230 -llibopencv_objdetect230 -llibopencv_video230
    C:\OpenCV2.3\build\x64\mingw\bin/libopencv_calib3d230.dll: file not recognized: File format not recognized
    collect2: ld returned 1 exit status
    Build error occurred, build is stopped
    Time consumed: 2044 ms.

    ReplyDelete
  14. Please accept my appoligies if I ask beginner ?s trying to teach myself C++ for hobby robotics and found your page. Looks to me like a pretty good place to start and I thank you for posting this tutorial.

    So far, I followed all the steps outlined, installed Eclipse Indigo (C++), CV2.2 and MiniGW successfully.

    I think my problem is one or both of two issues relating to the file paths.

    on startup with Eclipse a window prompts for workspace location and has an autofill in the form "C:\Users\My Name\workspace" as default, which for not having a better idea I choose.

    I read above that when adding opencv_calib3d220, etc. to Libraries (-l) under MinGW that "relative paths" must be used.

    I wasn't sure exactly what to add to make this right, adding some more coffee and trying to read some more.

    any help would be appriciated, thank you :)

    ReplyDelete
  15. Ok, looking at the file location I think I'm on the right track, so far I have

    C:\OpenCV2.2\lib\opencv_calib3d220

    to add to Libraries (-l). Guessing I need to add .lib or .hpp to get the relative path correct?

    also, I noticed when right clicking properties on the library file that my Altium Designer demo has associated all the CV2 Lib files as an Altium file, so I changed that to Eclipse Indigo.

    Still tinkering, any thoughts?

    Thanks,
    Robert

    ReplyDelete
  16. I don't know, trying this over and over...

    I keep getting an error message: "Launch failed, binary not found"

    ReplyDelete
  17. "Hello World" and the below following code appear but when the run button is pressed I still get the "Application Launcher" window and the message "Launch failed. Binary not found". I didn't continue as far as adding more code from an example application,
    (should I?) thinking the basic "hello world" should "run" before spending more time? again, appoligies, I understand the principles but I am new to Eclipse and coding in general. Thanks, Robert.

    #include
    using namespace std;

    int main() {
    cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
    return 0;
    }

    ReplyDelete
  18. AHHHHH!!!! finally, it works!

    overthinking I guess had entered the complete file path in librarys, discovering this when I tried "building" the app. So editing the files just as they are above with no file path specified on each individual .lib file resulted in build and run with no errors!

    Perseverence!

    Thanks,
    Robert

    ReplyDelete
  19. Great to hear to got it figured out, don't hesitate to ask for more help. Be aware though, if you choose to use opencv2.3 I think this guide is no longer valid. I haven't bothered to explore 2.3 yet, but the file structure of the install is definitely different to opencv2.2

    ReplyDelete
  20. Hello, I have a problem. You say:
    FINALLY, under the "Library search path (-L)" section add:

    "C:\OpenCV2.2\lib"

    Well, I have no such folder! I've downloaded opencv 2.3.1, OpenCV-2.3.1-win-superpack.exe to be exact, I've run it, than it created the opencv folder, but there are only these:

    folders:

    3rdparty
    android
    build
    data
    doc
    include
    modules

    files:

    CMakeLists.txt
    cmake_uninstall.cmake.in
    cvconfig.h.cmake
    index.rst
    opencv-XXX.pc.cmake.in
    OpenCV.mk.in
    opencv.pc.cmake.in
    OpenCVAndroidProject.cmake
    OpenCVConfig-version.cmake.in
    OpenCVConfig.cmake.in
    OpenCVFindIPP.cmake
    OpenCVFindLATEX.cmake
    OpenCVFindOpenEXR.cmake
    OpenCVFindOpenNI.cmake
    OpenCVFindPkgConfig.cmake
    OpenCVFindXimea.cmake
    OpenCVModule.cmake
    OpenCVPCHSupport.cmake
    README
    samples

    ReplyDelete
  21. Hi Szilvia, unfortunately this guide is outdated. I haven't bothered to look into updating my install of opencv, but the files you listed in your comment suggest you need to run cmake to create the library files.

    ReplyDelete
  22. Hi Jon,


    First of all, thanks for the excellent blog post.

    I am trying to run your sample code on Eclipse. I have installed OpenCV 2.3.1. (System: 64 bit Windows 7)

    I get this in my console:

    ****************************

    **** Build of configuration Debug for project OpenCV_testing ****

    **** Internal Builder is used for build ****
    g++ -LC:\OpenCV2.3.1Package\opencv\build\bin -o OpenCV_testing.exe src\opencv_test.o -lopencv_core231 -lopencv_highgui231
    c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe: cannot find -lopencv_core231
    c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe: cannot find -lopencv_highgui231

    collect2: ld returned 1 exit status

    Build error occurred, build is stopped

    Time consumed: 218 ms.
    ****************************

    Since the method you have written is for OpenCV 2.2, the folder names are all different. I also have no way of checking what files my 'Includes' folder and 'Libraries' folder, that I link in the Project Properties, must have. So I really do not understand what I am missing here.


    Thank you so much!

    ReplyDelete
  23. Hello

    I want to use openCv to eclipse and for this I have followed the below link:

    http://docs.opencv.org/doc/tutorials/introduction/linux_eclipse/linux_eclipse.html

    these are my includes
    -------------------------------------------------------------------------------
    #include"stdio.h"
    #include"string.h"
    #include "C:/opencv/build/include/opencv2/imgproc/imgproc.hpp"
    #include "C:/opencv/build/include/opencv2/highgui/highgui.hpp"
    #include "C:/opencv/build/include/opencv2/imgproc/imgproc_c.h"
    #include "C:/opencv/build/include/opencv2/legacy/legacy.hpp"
    #include "C:/opencv/build/include/opencv/cv.h"
    #include "C:/opencv/build/include/opencv/highgui.h"
    and I want to be able to do this:

    IplImage* imgInfo = cvLoadImage(file_name,CV_LOAD_IMAGE_GRAYSCALE);
    ---------------------------------------------------------------------------------
    but I get the below error:

    Multiple markers at this line
    - Symbol 'CV_LOAD_IMAGE_GRAYSCALE' could not be
    resolved
    - Type 'IplImage' could not be resolved

    I wanted to know what should I do?

    Thanks

    ReplyDelete