Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I correctly link OpenCV libraries to Eclipse on Windows?

For the last couple of hours I have been trying to get a basic OpenCV program to work in my Eclipse Mars IDE. The program consists of the following main.cpp:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
  if( argc != 2)
  {
   cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
   return -1;
  } 

  Mat image;
  image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

  if(! image.data )                              // Check for invalid input
  {
    cout <<  "Could not open or find the image" << std::endl ;
    return -1;
  }

  namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
  imshow( "Display window", image );                   // Show our image inside it.

  waitKey(0);                                          // Wait for a keystroke in the window
  return 0;
}

What I have done up till now:

  1. Installed MinGW (in E:\NVPACK\MinGW) and added its bin file path to my Environmental Variables.
  2. Installed OpenCV 2.4.12 at E:\opencv. Inside the installed folder are the "build" and "sources" folder.

  3. Made a new Eclipse C++ project with the MinGW GCC Toolchain.

  4. Inside the Tool Settings (Project Properties --> C/C++ Build --> Settings) I included the OpenCV library in the GCC C++ Compiler as "E:\opencv\build\include"

  5. Inside the Tool Settings I added in the MinGW C++ Linker all the libraries (i.e. opencv_core2412, opencv_highgui2412, opencv_imgproc2412) and set the Library search path to "E:\opencv\build\x86\vc12\lib"

  6. Set my Binary parser to PE Windows Parser.

Building the project all the OpenCV functions get undefined references errors, such as:

undefined reference to `cv::imread(std::string const&, int)'    main.cpp    line 17 C/C++ Problem
undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'   main.cpp    line 26 C/C++ Problem

Weird enough, declaring Mat image does not give an error and holding my mouse over it correctly displays the OpenCV's Mat documentation.

Googling the problem I found that for some reason I might need to use CMake to build my own library files (the ones used in step 5). So after installing CMake gui, I used E:\opencv\sources as the source directory and made a new directory E:\opencv\MinGW as my build directory. I used "Eclipse CDT4 -MinGW Makefiles" generator to Configure the files. After pressing Configure, I will receive the following error for my g++ compiler (a similar one for my gcc compiler):

CMake Error at CMakeLists.txt:71 (project):
The CMAKE_CXX_COMPILER:

E:/NVPACK/MinGW/bin/mingw32-g++

is not a full path to an existing compiler tool.

Tell CMake where to find the compiler by setting either the environment
variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
to the compiler, or to the compiler name if it is in the PATH.

The compilers are however identified: "The CXX compiler identification is GNU 4.8.1" and "The C compiler identification is GNU 4.8.1"

Any help on how to correctly set up the OpenCV 2.4.12 libraries in Eclipse Mars on Windows 7 would be very much appreciated!

like image 642
JimmyJumbo Avatar asked May 15 '16 12:05

JimmyJumbo


2 Answers

Well, I hope this is detailed enough ! First of all, a big cup of coffee will help !

Let's get started :

Requirements :

  • Download OpenCV source from github repo (extract it to a folder we will name opencv_src_folder)
  • Download and install MinGW (also add /bin folder to system path)
  • Download and install CMake

Steps :

  • Open CMake-gui

    • set src folder as opencv_src_folder
    • set build folder as opencv_src_folder/Mingw_build/
    • Hit configure and choose Eclipse CDT4 - MinGW MakeFile then next.
    • if configure successful , hit generate.
  • Open a command line (Windows + R)

    • $ cd opencv_src_folder/Mingw_build/
    • $ mingw32-make : this will make openCV using MinGW, so wait until it's finished !
    • now, let's install it $ mingw32-make install this will install cmake in opencv_src_folder/Mingw_build/install/
  • Add opencv_src_folder/Mingw_build/install/bin/ to your system path & restart session.

  • Eclipse Configuration :

    • Go to the Eclipse CDT IDE, create a C++ program using the sample OpenCV code.
    • Go to Project > Properties > C/C++ Build > Settings > GCC C++ Compiler > Includes, and add the source OpenCV folder "opencv_src_folder/Mingw_build/install/include/"
    • Go to Project > Properties > C/C++ Build > Settings > MinGW C++ Linker > Libraries, and add to the Libraries (-l) separately : (Don't forget to change the 245 to whichever number they use for the lib files) opencv_calib3d245 opencv_contrib245 opencv_core245 opencv_features2d245 opencv_flann245 opencv_gpu245 opencv_highgui245 opencv_imgproc245 opencv_legacy245 opencv_ml245 opencv_nonfree245 opencv_objdetect245 opencv_photo245 opencv_stitching245 opencv_video245 opencv_videostab245

    • Add the built OpenCV library folder, opencv_src_folder/Mingw_build/install/lib to Library search path (-L).

Testing :

you can use the following code as a test :

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
  if( argc != 2)
  {
   cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
   return -1;
  }

  Mat image;
  image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

  if(! image.data )                              // Check for invalid input
  {
    cout <<  "Could not open or find the image" << std::endl ;
    return -1;
  }

  namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
  imshow( "Display window", image );                   // Show our image inside it.

  waitKey(0);                                          // Wait for a keystroke in the window
  return 0;
}

And last build your project !

Hope that helps !

like image 178
Vtik Avatar answered Nov 01 '22 17:11

Vtik


The above posts are really good and got me close but not all the way through. The following are some additional steps that I took that finally worked for me.

Tips for building and installing OpenCV

NOTE: I built this in Windows 10 (Using Parallel's desktop on Mac but I don't think that should matter)

  1. First, I used version 3.13.1 of Cmake and download the latest openCV repo (The above link takes you to an older version).

  2. Make sure that every time you are building using Cmake-gui, you either clear the cache, and delete everything from that folder or make a new directory and build in this new directory (I personally did the second one).

  3. Next, I downloaded the MinGW installer from here. But when installing, instead used the following configurations

MinGW Configuration

After a lot of google searching and reading through github issues, I found out that someone was only able to build and install using this configuration of MinGW and so was I. Also, when you do install this version of MinGW and add this to path make sure that you delete any other version that is being currently used from the system path and delete the folder as well. When you run Cmake-GUI, just when you hit configure, it should show you that it built with this new MinGW libraries if you did everything correctly.

  1. Next, add to system path as usual and restart Command Prompt.

  2. After configuring in Cmake-GUI the first time, also do the following:

    • Search for CMAKE_BUILD_TYPE and in its Value Column add 'Release' (without the quotes)

    • Search for WITH_MSMF and uncheck it.

    • Search for ENABLED_PRECOMPILED_HEADERS and uncheck it.

Tips for Eclipse Configuration

Everything for eclipse worked for me as mentioned in the above post, except that I had to explicitly add the

--std=c++11 flag under Project > Properties > C/C++ Build > Settings > GCC C++ Compiler > Miscellaneous > Other Flags

Before adding this, every program with openCV library was giving me 1000s of errors.

like image 30
Shank Avatar answered Nov 01 '22 17:11

Shank