Getting started with OpenCV 2.4 and MinGW on Windows 7 - c++

How do I install OpenCV 2.4 and compile my code with MinGW?

1. Installing OpenCV 2.4.3
First, get OpenCV 2.4.3 from sourceforge.net. Its a self-file-extracting so just double click the file to start installation. Install it in a directory, say C:\.
Wait until all files get extracted. It will create a new
directory C:\opencv which contains OpenCV header files, libraries, code samples, etc.
Now you need to add C:\opencv\build\x86\mingw\bin directory to your system PATH. This directory contains OpenCV DLLs which is required for running your code.
Open Control Panel → System → Advanced system settings → Advanced Tab → Environment variables...
You will see a window like shown below:
On the System Variables section,
select Path (1), click Edit... (2), add C:\opencv\build\x86\mingw\bin (3) then click Ok.
This will completes the OpenCV 2.4.3 installation on your computer.
2. Installing MinGW compiler suite
I highly recommend you to use gcc (GNU Compiler Collection) for compiling your code. gcc is the compiler suite widely available in Linux systems and MinGW is the native port for Windows.
Download the MinGW installer from Sourceforge.net and double click to start installation. Just follow the wizard and select the directory to be installed, say C:\MinGW.
Select "C Compiler" and "C++ Compiler" to be installed.
The installer will download some packages from the internet so you have to wait for a while. After the installation finished, add C:\MinGW\bin to your system path using the steps described before.
To test if your MinGW installation is success, open a command-line box and type: gcc. If everything is ok, it will display this message:
gcc: fatal error: no input files
compilation terminated
This completes the MinGW installation, now is the time to write your "Hello, World!" program.
3. Write a sample code
Open your text editor and type the code below and save the file to loadimg.cpp.
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat im = imread(argc == 2 ? argv[1] : "lena.jpg", 1);
if (im.empty())
{
cout << "Cannot open image!" << endl;
return -1;
}
imshow("image", im);
waitKey(0);
return 0;
}
Put lena.jpg or any image you like in the same directory with the file above. Open a command-line box and compile the code above by typing:
g++ -I"C:\opencv\build\include" -L"C:\opencv\build\x86\mingw\lib" loadimg.cpp -lopencv_core243 -lopencv_highgui243 -o loadimg
If it compiles successfully, it will create an executable named loadimg.exe.
Type:
loadimg
To execute the program. Result:
4. Where to go from here?
Now that your OpenCV environment is ready, what's next?
Go to the samples dir → C:\opencv\samples\cpp.
Read and compile some code.
Write your own code.

The instructions in #bsdnoobz answer are indeed helpful, but didn't get OpenCV to work on my system.
Apparently I needed to compile the library myself in order to get it to work, and not count on the pre-built binaries (which caused my programs to crash, probably due to incompatibility with my system).
I did get it to work, and wrote a comprehensive guide for compiling and installing OpenCV, and configuring Netbeans to work with it.
For completeness, it is also provided below.
When I first started using OpenCV, I encountered two major difficulties:
Getting my programs NOT to crash immediately.
Making Netbeans play nice, and especially getting timehe debugger to work.
I read many tutorials and "how-to" articles, but none was really comprehensive
and thorough. Eventually I succeeded in setting up the environment; and after a
while of using this (great) library, I decided to write this small tutorial,
which will hopefully help others.
The are three parts to this tutorial:
Compiling and installing OpenCV.
Configuring Netbeans.
An example program.
The environment I use is:
Windows 7, OpenCV 2.4.0, Netbeans 7 and MinGW 3.20 (with compiler gcc 4.6.2).
Assumptions:
You already have MinGW and Netbeans installed on your system.
Compiling and installing OpenCV
When downloading OpenCV, the archive actually already contains pre-built
binaries (compiled libraries and DLL's) in the 'build' folder. At first, I
tried using those binaries, assuming somebody had already done the job of
compiling for me. That didn't work.
Eventually I figured I have to compile the entire library on my own system in
order for it to work properly.
Luckily, the compilation process is rather easy, thanks to CMake. CMake
(stands for Cross-platform Make) is a tool which generates makefiles specific
to your compiler and platform. We will use CMake in order to configure our
building and compilation settings, generate a 'makefile', and then compile the
library.
The steps are:
Download CMake and install it (in the installation wizard choose to add
CMake to the system PATH).
Download the 'release' version of OpenCV.
Extract the archive to a directory of your choice. I will be using
c:/opencv/.
Launch CMake GUI.
Browse for the source directory c:/opencv/.
Choose where to build the binaries. I chose c:/opencv/release.
Click 'Configure'. In the screen that opens choose the generator
according to your compiler. In our case it's 'MinGW Makefiles'.
Wait for everything to load, afterwards you will see this screen:
Change the settings if you want, or leave the defaults. When you're
done, press 'Configure' again. You should see 'Configuration done' at
the log window, and the red background should disappear from all the
cells.
At this point CMake is ready to generate the makefile with which we will
compile OpenCV with our compiler. Click 'Generate' and wait for the
makefile to be generated. When the process is finished you should see
'Generating done'. From this point we will no longer need CMake.
Open MinGW shell (The following steps can also be done from Windows' command
prompt).
Enter the directory c:/opencv/release/.
Type mingw32-make and press enter. This should start the compilation
process.
When the compilation is done OpenCV's binaries are ready to be used.
For convenience, we should add the directory C:/opencv/release/bin
to the system PATH. This will make sure our programs can find the
needed DLL's to run.
Configuring Netbeans
Netbeans should be told where to find the header files and the compiled
libraries (which were created in the previous section).
The header files are needed for two reasons: for compilation and for code
completion. The compiled libraries are needed for the linking stage.
Note: In order for debugging to work, the OpenCV DLL's should be available,
which is why we added the directory which contains them to the system PATH
(previous section, step 5.4).
First, you should verify that Netbeans is configured correctly to work with
MinGW. Please see the screenshot below and verify your settings are correct
(considering paths changes according to your own installation). Also note
that the make command should be from msys and not from Cygwin.
Next, for each new project you create in Netbeans, you should define the
include path (the directory which contains the header files), the libraries
path and the specific libraries you intend to use. Right-click the project
name in the 'projects' pane, and choose 'properties'. Add the include path
(modify the path according to your own installation):
Add the libraries path:
Add the specific libraries you intend to use. These libraries will be
dynamically linked to your program in the linking stage. Usually you will need
the core library plus any other libraries according to the specific needs of
your program.
That's it, you are now ready to use OpenCV!
Summary
Here are the general steps you need to complete in order to install OpenCV and
use it with Netbeans:
Compile OpenCV with your compiler.
Add the directory which contains the DLL's to your system PATH (in our case:
c:/opencv/release/bin).
Add the directory which contains the header files to your project's include
path (in our case: c:/opencv/build/include).
Add the directory which contains the compiled libraries to you project's
libraries path (in our case: c:/opencv/release/lib).
Add the specific libraries you need to be linked with your project (for
example: libopencv_core240.dll.a).
Example - "Hello World" with OpenCV
Here is a small example program which draws the text "Hello World : )" on a GUI
window. You can use it to check that your installation works correctly. After
compiling and running the program, you should see the following window:
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.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;
}

This isn't working for me. I spent few days following every single tutorial I found on net and finally i compiled my own binaries. Everyting is described here: OpenVC 2.4.5, eclipse CDT Juno, MinGW error 0xc0000005
After many trials and errors I decided to follow this tutorial and to compile my own binaries as it seems that too many people are complaining that precompiled binaries are NOT working for them. Eclipse CDT Juno was already installed.
My procedure was as follows:
Download and install MinGW and add to the system PATH with
c:/mingw/bin
Download cmake from http://www.cmake.org and install it
Download OpenCV2.4.5 Windows version
Install/unzip Opencv to C:\OpenCV245PC\ (README,index.rst and CMakeLists.txt are there with all subfolders)
Run CMake GUI tool, then
Choose C:\OpenCV245PC\ as source
Choose the destination, C:\OpenCV245MinGW\x86 where to build the binaries
Press Configure button, choose MinGW Makefiles as the generator. There are some red highlights in the window, choose options as you need.
Press the Configure button again. Configuring is now done.
Press the Generate button.
Exit the program when the generating is done.
Exit the Cmake program.
Run the command line mode (cmd.exe) and go to the destination
directory C:\OpenCV245MinGW\x86
Type "mingw32-make". You will see a progress of building
binaries. If the command is not found, you must make sure that the
system PATH is added with c:/mingw/bin. The build continues
according the chosen options to a completion.
In Windows system PATH (My Computer > Right button click >
Properties > Advanced > Environment Variables > Path) add the
destination's bin directory, C:\OpenCV245MinGW\x86\bin
RESTART COMPUTER
Go to the Eclipse CDT IDE, create a C++ program using the sample OpenCV code (You can use code from top of this topic).
Go to Project > Properties > C/C++ Build > Settings > GCC C++ Compiler > Includes, and add
the source OpenCV folder "C:\OpenCV245PC\build\include"
Go to Project > Properties > C/C++ Build > Settings > MinGW C++ Linker > Libraries, and add to the Libraries (-l) ONE BY ONE (this could vary from project to project, you can add all of them if you like or some of them just the ones that you need for your project): 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, "C:\OpenCV245MinGW\x86\lib" to Library search path (-L).
You can use this code to test your setup:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("c:/lenna.png", CV_LOAD_IMAGE_COLOR);
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE);
imshow("MyWindow", img);
waitKey(0);
return 0;
}
Don't forget to put image to the C:/ (or wherever you might find suitable, just be sure that eclipse have read acess.

If you installed opencv 2.4.2 then you need to change the -lopencv_core240 to -lopencv_core242
I made the same mistake.

On Windows 64bits it´s works:
Download opencv-3.0 (beta), MinGW (command line tool);
Add above respective bin folder to PATH var;
Create an folder "release" (could be any name) into ;
Into created folder, open prompt terminal and exec the below commands;
Copy and Past this command
cmake -G "MinGW Makefiles" -D CMAKE_CXX_COMPILER=mingw32-g++.exe -D WITH_IPP=OFF MAKE_MAKE_PROGRAM=mingw32-make.exe ..\
Execute this command mingw32-make
Execute this command mingw32-make install
DONE

I used the instructions in this step-by-step and it worked.
http://nenadbulatovic.blogspot.co.il/2013/07/configuring-opencv-245-eclipse-cdt-juno.html

As pointed out by #Nenad Bulatovic one has to be careful while adding libraries(19th step). one should not add any trailing spaces while adding each library line by line. otherwise mingw goes haywire.

Related

Where is the lib folder (or its replacement) in the current OpenCV?

I'm following a book written for the older version of OpenCV (OpenCV 2 Computer Vision, by PACT) and it tells me to include the lib folder in my Visual Studio 2013 Property Manager when creating a new property sheet.
I don't see a directory called lib in the current GitHub version (opencv-master, which is OpenCV 3.1.x). Has this folder been replaced by something else?
I built OpenCV with cmake. I found a 4 year old unanswered question wherein someone was also looking for this folder. They tried building the library from another directory that no longer exists, but that didn't work for them anyway...
Another OpenCV user just told me that GitHub doesn't include the libraries, so you have to cmake them locally. I'm still not clear on where / how I can cmake them.
I realize the pre-built binaries have this, but I'm avoiding them because I need the SURF functions in opencv_contrib, so I needed to build it from source.
The pre-built binaries will have a library folder in the corresponding path
Local System Path(Opencv Folder)-> build->x64/x86->vc10/vc11/vc12->lib.
As you mentioned that you don't wish to use it then the only option left for you is to build it locally which is a much better option if you plan to use Opencv libraries for varied functions and projects as it resolves many build errors that you might face later.
I used the Cmake Graphical user interface to build opencv, following are the steps I followed to successfully build the libraries on my system .
So, you would need to create a new folder that will contain all
the Makefiles generated.
Please refer to this image for clearer understanding:
In GUI you define source directory path where OpenCVConfig.cmake is present, according to your image it's the
current folder in your image opencv-master.
Similarly, define the path to the new directory you created where all the build files would be stored.
Make sure to uncheck Build_Examples to avoid configuration errors.
Then click Configure at the bottom when configuring is done .(you may need to configure it twice)
After click the tab adjacent to configure, Generate to create the solution file.It will ask you for the compiler name
select the
compiler installed on your system from the list of choices. After
generation is done.
Go to this path Build_New_Directory(the directory you created) you will find OpenCV.sln build this project, it will take around 10-15 minutes depending on your processor, wait patiently .If you get a build error at this point don't invest your time in debugging on Visual Studio go back to Cmake GUi and configure it again and this time give the path to dependent libraries on your system that it could not find .Repeat the process it should be successfully build now.
After it is successfully build you can now locate the path of all opencv libraries build on your system as follows.
Build_New_Directory(the directory you created)->
install->x64->vc10(compiler I used)->lib.
As an update to Nikita's awnser:
There is a cmake build bug where the x64 folder will not be created if OpenCV_RUNTIME is not set. This will happen if you build an old OpenCV (e.g. <= 3.2.0) with a newer Visual Studio Version than was available at that time (e.g. Visual Studio 2017)
To fix this, add the correct MSVC_VERSION elseif-cases in both ./cmake/OpenCVDetectCXXCompiler.cmake and the ./cmake/templates/OpenCVConfig.root-WIN32.cmake.in (or ./cmake/OpenCVConfig.cmake in < v3.2.0) files:
[...]
elseif(MSVC_VERSION EQUAL 1900)
set(OpenCV_RUNTIME vc14)
# old version ends here with endif()
elseif(MSVC_VERSION GREATER 1909 AND MSVC_VERSION LESS 1920)
set(OpenCV_RUNTIME vc15)
elseif(MSVC_VERSION GREATER 1919 AND MSVC_VERSION LESS 1930)
set(OpenCV_RUNTIME vc16)
endif()
[...]

OpenCV - how to prioritize opencv libraries compiled in /home over those from /usr/lib/ using Eclipse C++ IDE

I want to setup an OpenCV project in Linux using Eclipse IDE in C++.
First, I have compiled my own version of OpenCV using the instructions mentioned on the openCV website in my /home/ directory. After this, I wanted to setup a basic show image project using Eclipse IDE following their instructions.
However, I am using OpenSUSE 12.2 and I have an older, preinstalled version of OpenCV in /usr/ and when I build the project and check the linked libraries of OpenCV with the ldd command applied to the generated binary, it points out to the library files from /usr/lib64/.
In order to point out to the compiled library files from my home library I had to modify the LD_LIBRARY_PATH environment variable from .bashrc file:
export LD_LIBRARY_PATH=/home/user/OpenCV/release/lib/:$LD_LIBRARY_PATH
Following this, I started the Eclipse IDE from a terminal with the reinitialized .bashrc file.
After this step it worked. However, is there a way to prioritize the /home/ path over the /usr/ path from Eclipse IDE instead of doing the ./bashrc trick?
Try run your program after setting LD_PRELOAD environmental variable, pointing to your libraries that you want to be loaded first.
You can prepare a script to lauch your program:
export LD_PRELOAD=./your_library.so
./your_program

Trouble building the Open Asset Import Library (Assimp)

I have just downloaded the Open Asset Import Library (Assimp) which is an API used to import 3D file formats such as .3DS and .Obj into source code. Doing so allows for much easier rendering of meshes using openGL.
The problem is, I can't get the Library to build. It comes with very vague and cryptic directions on how to build it and connect to a compiler such as XCode (which I am using). It says I should use a program called CMake to build the library. I have downloaded CMake and tried to use it but it has not yet worked for me.
Has anyone here successfully built and installed Assimp using CMake or some other tool?
I totally agree that the documentation for Mac is a bit lacking, but I have managed (after several attempts) to build and install the library.
A few things before starting: make sure you have installed XCode Command Line Tools (that installs the GNU compiler).
These are the steps I followed:
Download the latest release of Assimp and extract it (I got the file
from here, the one with source only:
http://sourceforge.net/projects/assimp/files/assimp-3.0/)
If you don't have it, install CMake (http://www.cmake.org/cmake/resources/software.html; I'm currently using an older version, but the latest one should work fine as well)
Create a build directory (should be outside the source directory)
Open CMake and point it to the folder you created at step 1 (where it says "Where is the source code"); the other folder ("Where to build the binaries") should point to the folder created at step 3
Click "Configure" on the bottom; it will ask you which environment you'd like to use. I picked "Eclipse CDT 4 - Unix Makefiles"
You should get a list of options; the two I selected were "BUILD_STATIC_LIB" and "ENABLE_BOOST_WORKAROUND"
Click "Generate"
Now you should move to the terminal and go to the folder created at step 3
Type "make" and launch it; you should see the build progressing without issues
When the build is finished, type "sudo make install"; it will ask for your password and install the library
After the last step you should be able to include the library in your code:
#include <assimp/cimport.h>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
Make sure you also include the library for linking; when linking you should add -lassimp to the list
Let me know if you have any issues with the steps above!
I had a slightly different approach that worked for me building assimp on Mac. Here are the steps that I took if it helps anybody.
Step 1 - Install Libraries with Homebrew
brew install glm glew assimp
Step 2 - Update my CMake with the Following
FIND_PACKAGE(ASSIMP REQUIRED)
LINK_DIRECTORIES(/usr/local/lib)
add_executable(
myExecutable
...
)
TARGET_LINK_LIBRARIES( myExecutable PRIVATE ${ASSIMP_LIBRARIES} )

NetBeans with C++ and OpenGL / Freeglut under Windows 7

I recently tried to figure out how to use freeglut with NetBeans 7.
I Google'd a lot and I didn't find a suitable tutorial on how to bind the stuff I need into it.
Currently I have more problem then this:
First one is:
- freeglut 2.8 => in order to compile it I need MinGW and msys.
I have both installed but I can't "configure" or "make all" and "make install" freeglut.
I didn't find an easy-to-read how-to for that task.
Next one:
- once built, where to put what files into a specific folder?
Last one:
- Once put the files into corresponding folder, how to use finally freeglut under NetBeans 7 on a C++ project?
I feel like a lack of information on that.
All I found are always answers like:
download, build, install and done. But I'm already stuck on the "install" and "build".
The solution:
Close NetBeans.
Install and download MinGW from mingw.org
After installing, add the Path to your SystemEnvironemt (example: C:\MinGW).
go to Start->Computer, right mousebutton, select properties
go to advanced system setting, in the following dialog press "Environment Variables".
in the tab "system variables", find the entry Path (or PATH)
add: C:\MinGW;
get freeglut pre-compiled package:
(http://www.transmissionzero.co.uk/computing/using-glut-with-mingw/)
there is a good tutorial how to set up.
Now have fun (just #include " and you're done.
have fun.
Note #1:
if you have problems finding make, the folder is:
C:\MinGW\msys\1.0\bin\make.exe
(when installed MinGW on C:\MinGW).
"mingw-get install msys" in command line should get msys...
last but not least dont forget to add msys\bin to path, too. :)
here's a full tutorial, but in german language.
http://techchan.blog.de/2012/03/18/einrichten-opengl-netbeans-7-1-1-teil-0-start-13209096/
If you need it in english, let me know if you could translate it ;) otherwise i'll think about to translate it myself.
I had a difficult time getting glut working in Netbeans on Windows 7 until I found freeglut, I attributed this to the fact that glut is just very out of date and so are the tutorials I found.
Maybe this will help others looking for instructions on using freeglut in Netbeans. These instructions are like the ones in the link already provided, but help explain how to use it in Netbeans rather than from the command line. This tutorial expects that you've already gotten Netbeans and your compiler working together. IF you have not done so, I found the guide here very helpful: http://netbeans.org/community/releases/68/cpp-setup-instructions.html#mingw
download and extract freeglut from http://www.transmissionzero.co.uk/computing/using-glut-with-mingw/ into an easy to reach folder. (I recommend C:/FrGlut)
either add the bin folder (C:\FrGlut\bin) to your Windows PATH variable, or copy the freeglut.dll from your bin folder into C:\Windows\System32
Open Netbeans and open or create your project
go to: file->project properties
Select your compiler, go to include directories, and add the path to your freeglut include folder (C:\FrGlut\include)
under linker, select add libary, and navigate to the file C:\FrGlut\lib\libfreeglut.a
select options and add the following individually:
-dynamic -lfreeglut -lopengl32 -LC:\FrGlut\lib\libfreeglut.a
Netbeans should now be ready to use with your freeglut library.
To use the glut libary add the following include to your source:
#include <GL/glut.h>
Remember that freeglut.dll will need to be shared along with your executable in order for the program to run, as most users will not already have it.

How to use OpenCV2.2 with Dev C++ 4.9.9.2?

I wish to use opencv on my project work. Therefore, I downladed OpenCV2.2 http://sourceforge.net/projects/opencvlibrary/ and installed it. Then I made following configuration in Dev C++ 4.9.9.2 since i am using Dev C++ compiler for my works;
OpenCV library is installed in the C:\ OpenCV2.2 directory
Open Dev C++ and Tools ->Compiler Options and then add a new compiler named OpenCV
Then, click Add these commands to the linker command line and include following linker: -lopencv_core220d -lopencv_highgui220d -lopencv_video220d -lopencv_ml220d -lopencv_legacy220d -lopencv_imgproc220d
Add C:\ OpenCV2.2\include\opencv and C:\ OpenCV2.2\include\opencv2 on to C includes and C++ Includes on directories.
Also add static libaries paths to C:\ OpenCV2.2\lib and bin directory path to the C:\ OpenCV2.2\bin
then, i tried to compilng C programs on the sample directory but I got lot of error messages
Then, I downloaded Cmake (Windows (Win32 Installer) cmake-2.8.4-win32-x86.exe- binary distributions) from “http://www.cmake.org/cmake/resources/software.html and instaled it. According to the OpenCV installation guide (http://opencv.willowgarage.com/wiki/InstallGuide), I tried to configure opencv using Cmake. I did this using Cmake GUI and also tried using command line however both attempts were not success anf got lot of error messages (ex.Error in configuration process , project files may be invalid).
Please give me correct steps to do this.