Thursday, February 4, 2010

Setting up OpenCV in Eclipse on Windows

IF YOU ARE INSTALLING OPENCV VERSION 2.2 OR LATER PLEASE GO HERE INSTEAD: OpenCV 2.2 GUIDE.

Recently OpenCV 2.0 was officially released. For those looking to do an upgrade to 2.0 (OR LATER) or installing OpenCV for the first time, there are some discrepancies with how older sites say you should setup your environment. I did some file digging and this is what I found works:

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.0 (OR download a more recent version)
  • 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.0\include\opencv\". Of course, change C:\OpenCV2.0 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):
  • cv200
  • cvaux200
  • cxcore200
  • cxts200
  • highgui200
  • ml200
In most cases you will only need cv200 and highgui200 to get started

ALSO NOTE: Versions 2.0 and later postfix the libraries names with a three digit number that corresponds to the version of OpenCV that you are linking to. Example: OpenCV2.1's highgui library must be linked using the name highgui210.

FINALLY, under the "Library search path (-L)" section add:
  • "C:\OpenCV2.0\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 snippet of code I made to test if your video camera and environment are working properly. I would have posted the code here, but blogger is being very difficult with the c++ syntax (i.e. it is trying to read a lot of things as html tags).

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!

31 comments:

  1. Thanks very much for this post, you have saved my time. Do you know if lcvcam is depricated in 2.0 version?

    ReplyDelete
  2. Worked brilliantly, thanks a million.

    Wasted hours trying to get it going with Visual Studio.

    ReplyDelete
  3. Thanks for the kind words everyone. As for lcvcam, I have not used it much in OpenCV1.0 and I haven't touched it at all in 2.0 so I can't be of much help. For certain cameras that don't work with cvCreateCameraCapture I created a hacky method that downloads a snapshot from the video stream, decompresses, sends it to stdout, and string streams it to stdin.

    ReplyDelete
  4. You should be the first result on Google!

    ReplyDelete
  5. Thanks a lot!
    After hours of trying to make OpenCV to work, just 5 minutes with your guide, and it works!

    Well done!
    You certainly should be the first result on Google.

    ReplyDelete
  6. Hi Jonathan,

    Excellent writeup! I do have a quick question regarding the change from cv -> cv200. When I update highgui and cv to highgui200 and cv200, I no longer get the cannot find -lcv error, but now get all these undefined references:

    src\main.o: In function `main':
    C:/EclipseWorkspace/Newtest/Debug/../src/main.cpp:32: undefined reference to `cvSplit'
    C:/EclipseWorkspace/Newtest/Debug/../src/main.cpp:33: undefined reference to `cvRandArr'
    C:/EclipseWorkspace/Newtest/Debug/../src/main.cpp:36: undefined reference to `cvConvertScale'
    C:/EclipseWorkspace/Newtest/Debug/../src/main.cpp:37: undefined reference to `cvMerge'
    C:/EclipseWorkspace/Newtest/Debug/../src/main.cpp:41: undefined reference to `CvImage::show(char const*)'
    src\main.o: In function `ZN7CvImageC1EPKcS1_i':
    C:/OpenCV2.0/include/opencv/cxcore.h:(.text$_ZN7CvImage6createE6CvSizeii[CvImage::create(CvSize, int, int)]+0x69): undefined reference to `cvCreateImage'
    C:/OpenCV2.0/include/opencv/cxcore.h:(.text$_ZN7CvImage6attachEP9_IplImageb[CvImage::attach(_IplImage*, bool)]+0x29): undefined reference to `cvReleaseImage'
    C:/OpenCV2.0/include/opencv/cxcore.h:(.text$_ZN7CvImageaSERKS_[CvImage::operator=(CvImage const&)]+0x34): undefined reference to `cvReleaseImage'
    C:/OpenCV2.0/include/opencv/cxcore.h:(.text$_ZN7CvImage5cloneEv[CvImage::clone()]+0x17): undefined reference to `cvCloneImage'
    C:/OpenCV2.0/include/opencv/cxcore.h:(.text$_ZN7CvImageD1Ev[CvImage::~CvImage()]+0x23): undefined reference to `cvReleaseImage'
    C:/OpenCV2.0/include/opencv/cxcore.h:(.text$_ZN7CvImageC1EPKcS1_i[CvImage::CvImage(char const*, char const*, int)]+0x35): undefined reference to `CvImage::load(char const*, char const*, int)'
    collect2: ld returned 1 exit status

    Any thoughts?

    ReplyDelete
  7. Hi Jim,
    I am by no means an expert in coding but a couple suggestions that may help.
    1. Did you try compiling a very basic program such as the one I linked to in the post? Making sure the very basics (cv.h and highgui.h) work is important (make sure if you use my above code to include cv.h and highgui.h for some reason I left that out)
    2. Make sure all the needed libraries are included in "main". These errors all seem to point to cxcore.h so maybe add "cxcore200" to the linker in STEP 9 of the guide and also include it in main if it is not already. I know cv.h will include cxcore if it hasn't already been defined but it should probably be done in main just to be sure.
    3. Maybe as a safety precaution you can try to "clean" the project. In eclipse this is under "Project-->Clean...". This has helped me in the past were the compiler wasn't rebuilding a part of my project. Cleaning the project will just remove prebuilt files from the project, so everything has to be made again. I think this will only make a difference if you use MAKEFILES for building your project.
    4. Posting atleast the lines that these errors refer to in "main" might be helpful for me to see what's going on, or if you feel comfortable, and none of the above help, you can email me your code, so I can see if I have the same problem when compiling or if it's something with the syntax or such. my email is jjcarrier at gmail dot com. But for future questions please keep posting here since this might be helpful to others as well.

    Hope this helps

    ReplyDelete
  8. Hi Jim,

    I followed the instructions as suggested and then copied your snippet into my main as follows:

    #include
    using namespace std;

    int main(void)
    {
    char key;
    IplImage* image;
    cvNamedWindow( "Example", CV_WINDOW_AUTOSIZE );
    CvCapture* capture;
    capture = cvCreateCameraCapture ( 0 );
    while(1){
    image = cvQueryFrame( capture );
    cvShowImage( "Example", image );
    key=cvWaitKey(10);
    if( key == 27 ) {
    cout << "Exiting on KEY: " << key << endl;
    break;
    }

    }
    cvReleaseCapture(&capture);
    cvDestroyWindow( "Example" );
    return 0;
    }


    but when I built it i get the following errors:

    Description Resource Path Location Type
    `cvCreateCameraCapture' was not declared in this scope test.cpp /test/src line 18 C/C++ Problem
    `cvDestroyWindow' was not declared in this scope test.cpp /test/src line 30 C/C++ Problem
    `cvNamedWindow' was not declared in this scope test.cpp /test/src line 16 C/C++ Problem
    `cvQueryFrame' was not declared in this scope test.cpp /test/src line 20 C/C++ Problem
    `cvReleaseCapture' was not declared in this scope test.cpp /test/src line 29 C/C++ Problem
    `cvShowImage' was not declared in this scope test.cpp /test/src line 21 C/C++ Problem
    `cvWaitKey' was not declared in this scope test.cpp /test/src line 22 C/C++ Problem
    `image' was not declared in this scope test.cpp /test/src line 15 C/C++ Problem
    `IplImage' was not declared in this scope test.cpp /test/src line 15 C/C++ Problem


    MY apology for the for the indentations.
    I have two questions:

    1) when we install OPENCV2.0 and use cmake gui to create the the project for Eclipse, Don't we need to build it first? (if so how to do that)

    2) The errors I am getting when i followed steps 1 to 9. Is it due to the reason outlined in my first question

    ReplyDelete
  9. Irf,
    1. I am not familiar with cmake, so I can't help you much here. But to build a project in Eclipse, you can either hit the button in the gui that looks like a hammer (which will just build the project) or you can hit the green "play" button which will build and run the project. Additionally you can hit "ctrl + b" which will also build the project.
    2. The problem you are seeing is due to missing #include statements. The code you posted only shows ONE "#include" (I cannot see which one you included because of the html but I suspect you included iostream). In order to run this code you need to include the following:

    #include "iostream" <--replace "" with brackets
    #include "cv.h"
    #include "highgui.h"

    I will update that file so that wont be a problem for others anymore.

    Best of luck

    ReplyDelete
  10. I followed the steps exactly as described and ran the sample code. But nothing shows up on the screen.
    I am expecting a window to pop up and display what my camera is seeing. Am i right?
    There are no compiler errors.
    am i missing something?
    please help!

    ReplyDelete
  11. Manaswi,
    Yes, you should expect to see a window popup up and the frames from the camera being displayed as a video sequence. This most likely means the program could not locate your camera via the statement:

    capture = cvCreateCameraCapture ( 0 );

    If this is the case, try setting the integer to "-1" instead of "0". OpenCV will then try to decide what camera interface to use. If this doesn't fix anything then take a close look at the supported video cameras page and make sure your camera is supported. I would give a link, but opencv's site seems to be down for me. A simple google search for "opencv supported cameras" will give you the needed info.

    I updated the code to be a little more informative if there is a problem with the camera capture device or querying frames. Re-download the code and try that if the above doesn't fix anything.

    ReplyDelete
  12. Thanks a lot Jon. You are GOD!!!

    I also had to copy the .dll files from OpenCV2.0\bin to C:\windows\system32

    I guess I did't do this while installing OpenCV

    And I used
    capture = cvCreateCameraCapture ( 0 )

    It was working brilliant

    Thanks again. Else the whole of my summer would have been a waste.

    ReplyDelete
  13. Jon,
    I followed your instructions for the set up but I am attempting to use it for my own code using OpenCV. When I attempt to build my code, I am prompted with almost 200 errors all of the type "undefined reference to". In most cases the undefined references are to simple things such as printf, sscanf, etc. I am probably forgetting to link a certain library or something. Any ideas?

    Thanks in advance,

    JT

    ReplyDelete
  14. Hi John,
    Yes, your assessment seems sane to me. I would say that you are forgetting to include stdio.h

    You shouldn't have to link to it in the linker settings, but it does need to be included in your c/c++ code with the usual #include statement.

    ReplyDelete
  15. Good post. Just made the shift from CodeBlocks to CDT.

    ReplyDelete
  16. Hie ,
    Installed MinGw
    I jus followed the steps said by john (advance setting in eclipse IDE) .But I am unable to find GCC Compiler branch.Instead I am having GCC C compiler and GCC c++ compiler braches in Tool settings section and also I am unable to find ''directories'' folder in any of these two branches.plzzz help me out.
    Im using Eclipse Helios C/C++ IDE

    ReplyDelete
  17. and also I am getting error error, during compile time,related to:
    __exchange_and_add.I think the problem mi8 be due to incorrect advance setting.plz guide me

    ReplyDelete
  18. Hi Santhosh,
    This post was in reference to Eclipse Galileo so there are apparently changes made between Galileo and Helios. It appears you will want to go to C/C++ Build --> Settings --> Tool Settings --> Includes. In here, you what to specify the path that I mention in Step #8. I will update this post when I get a chance later on today so that it matches Helios. Thank you for pointing this out!

    As for the __exchange_and_add, I mentioned this problem in my post; however, it appears the solution that I linked to has been moved. I updated the link in my post to point to the correct place. In summary you just need to modify the cxoperations header file.

    ReplyDelete
  19. Awesome guide, however people should keep in mind that with the newer CV all the DLLs that need to be linked @ MinGW Linker

    cv210
    cvaux210
    cxcore210
    cxts210
    highgui210
    ml210

    I literally followed the 2.0 guide for the 2.1 version. To make the guide universal you could have everybody find the DLLs in the regular explorer in the lib folder for the correct names.

    But still a very big thanks as this saved me quite some time.

    ReplyDelete
  20. Weszjuh, thank you for pointing this out. I figured that was going to be the naming scheme they would adopt. I will update this guide to include this, to help make it more universal/generic.
    Thanks again,
    -Jon

    ReplyDelete
  21. Thank you very much, you saved me a few hours at least :)

    ReplyDelete
  22. And what about 2.2.0 ? I didn't seea tutorial on it like this

    ReplyDelete
  23. Hi Jon,
    Thanks for the step by step write up for configure Eclipse w/OpenCV. I am pretty new to OpenCV so this is a God Sent. I followed your instructions making the needed adjustments based on the version of OpenCV. However, I get the following:
    C:\OpenCV2.2\include\opencv/cv.h:63:33: fatal error: opencv2/core/core_c.h: No such file or directory compilation terminated. Build error occurred, build is stopped
    These is what I am working with:

    Version: Helios Service Release 2
    Build id: 20110218-0911
    OpenCV 2.2

    Thanks

    ReplyDelete
  24. Hi keszeg robi and Kraxs,
    Thanks for your responses. Sorry I have not been as active as I have in the past on updating this guide. I posted a new guide which you can access in this post (I added a link at the top of the guide). OpenCV version 2.2 changed A LOT of things about the file structure and made this guide very much out-of-date. So I decided to make a new post for versions 2.2 and up and leave this guide intact for legacy purposes.
    Thanks,
    Jon

    ReplyDelete
  25. **** Build of configuration Debug for project OpenCV ****

    **** Internal Builder is used for build ****
    g++ -LC:\OpenCV2.1\lib -oOpenCV.exe src\main.o -lcv210.lib -lcvaux210.lib -lcxcore210.lib -lcxts210.lib -lhighgui210.lib -lml210.lib
    c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe: cannot find -lcv210.lib
    c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe: cannot find -lcvaux210.lib
    c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe: cannot find -lcxcore210.lib
    c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe: cannot find -lcxts210.lib
    c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe: cannot find -lhighgui210.lib
    c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe: cannot find -lml210.lib
    collect2: ld returned 1 exit status
    Build error occurred, build is stopped
    Time consumed: 812 ms.


    I Got the Above Build Error,, Please Help me..

    ReplyDelete
    Replies
    1. Hi Vasu,
      Make sure you are not including the .lib extension in the linker settings, for example you want highgui210 not highgui210.lib.
      If this isn't the issue let me know.

      Delete
  26. Hi Jon,
    I have installed Eclipse europa on Windows 64bit platform. I followed all you instructions to run openCV code, but I'm getting 'launch failed no binaries', btw I'm using OpenCV2.2 , and I've used opencv_core220 opencv_highgui220 so on, as the library files itself were named like that. Is there any changes to be made during the setup? please help me out!

    ReplyDelete
  27. I am using OpenCV 2.4.4 and it doesn't have a "lib" directory

    **** Internal Builder is used for build ****
    g++ -LC:\OpenCV\lib -LC:\OpenCV\build\x86\vc11\lib -LC:\OpenCV\build\x86\mingw\lib -oDisplayImage.exe src\DisplayImage.o -lcv -lopencv_core244 -lopencv_highgui244 -lopencv_ml244 -lopencv_video244 -lopencv_objdetect244 -lopencv_imgproc244
    src\DisplayImage.o: In function `main':
    H:\sw\test\workplace\DisplayImage\Debug/../src/DisplayImage.cpp:23: undefined reference to `cv::imread(std::string const&, int)'
    H:\sw\test\workplace\DisplayImage\Debug/../src/DisplayImage.cpp:31: undefined reference to `cv::namedWindow(std::string const&, int)'

    ReplyDelete