It was tremendously long procedure with setup of the environment but eventually it came up 🙂 I have been trying this for last 4 days.
Let me produce my steps which ultimately led to this.
I started with this tutorial. But, its way too old to follow. Its basically written for usage with Visual Studio. The process it asks to follow is like:
- Download opencv source from sourceforge. They offer it in some executable but believe me its not something to get happy about – its just an extractor and everything else has to configured manually.
- Next, they want us to download and install CMake.
- Now use CMake to build opencv source. This step should ideally create a visual studio solution project file which has to opened and compiled from within Visual Studio.
I had following problem with visual studio:
- With visual studio 2010, CMake was not able to build the project – it said it couldn’t get the associated compiler working.
- Also, opencv 3.1 don’t come with script for VS 10 so there was also not any way to bypass building process and use the library directly. So, in nutshell it failed terribly with visual studio 2010.
- Next, I downloaded VS 2015, thinking that it might work. VS 2015 was a 4 GB setup and to install it was such a chore for my CPU. It took about 1.5 hrs to install only. My laptop has serious problem of overheating and this exertion caused shutdown several times. Finally, I got VS 2015 installed on my PC.
- I directly jumped to CMake and yes it did build the project without error. This time it didn’t said anything about compiler error. I thought it was fine. But when I tried creating c++ project in VS 2015 it simply didn’t allowed me. The template window kept on popping without creating any project. Same was the scene with python – it gave some object reference error.
- Besides this, VS was getting too heavy on my system so at last I uninstalled it.
Now, I began thinking about eclipse as it is very light weight and there is a CDT package specifically for c++/c development. I found this tutorial very useful. This tutorial is written for Netbeans but one can easily get it to work with Eclipse.
I had to download Mingw in order to proceed. One can also use Cygwin but its easy with Mingw so I continued with Mingw. I found this tutorial useful for installing Mingw. If you want to install Cygwin – use this. You also need to mention “C:\MinGW\bin” path in User path variable” inside environment variable.
After Mingw is installed open CMake and build the opencv source as was shown here. Cautiously select “MinGW Makefiles” and build the source in some directory. Building with CMake will create libraries (.dlls) which will be used in an opencv project.
After building you will need to make the project (as given in step 5, mentioned in above tutorial). Note that MinGW shell is located inside (Install directory)C:\MinGW\msys\1.0\msys.bat
Type “mingw32-make"
inside the shell after going into the build directory. This will start compilation process with MinGW and will take a while, after which we are ready to use opencv.
I used this test code:
#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
using namespace cv;
int main(int argc, char** argv) {
   //create a gui window:
   namedWindow("Output",1);
   //initialize a 120X350 matrix of black pixels:
   Mat output = Mat::zeros( 120, 350, CV_8UC3 );
   //write text on the matrix:
   putText(output,
           "Hello World :)",
           cvPoint(15,70),
           FONT_HERSHEY_PLAIN,
           3,
           cvScalar(0,255,0),
           4);
   //display the image:
   imshow("Output", output);
   //wait for the user to press any key:
   waitKey(0);
   return 0;
}
But, compiling this on eclipse now, will create reference error as we haven’t yet configured eclipse to point to opencv library. Open Eclipse > create new c++ project > select MinGW tool chain in right panel and create the project.
Now open Project > Properties > GCC C++ compiler > Includes and give here the path to include files (header files). This has to be path to the build file include which we downloaded from Git (and not the one we build). Example:
Given is the file structure for opencv:
+opencv
> build (came with opencv source for windows)
> source (came with opencv source for windows)
>> MyBuild (we build this using CMake)
In this step we need to give the include path to: C:\opencv\build\include
This folder will have all the headers defined in it. We also need to point to the libraries that we will be using. This post shows what is the difference between library and headers. In short headers are interfaces and libraries are implementation.
Now we need to point to libraries. Go to Project > Properties > MinGW C++ Linker > Libraries > “Library search path”. In “Library search path” type in the location for compiled source. In our case it is: C:\opencv\MyCV\lib
Also add the libraries that you want to use in the project. Basically, for each header that you include in the project there is also a library entity that has to be included (Remember the interface – implementation relation of header and library).
Everything is set but it will not run the program even now saying – “terminated”. To correct this you need to copy “libstdc++-6.dll” from “C:\MinGW\bin” to the folder where the executable is being created.
It should work now. Got this output: