OpenCV C++ installation on Windows with MinGW
These days getting OpenCV running inside python is quite easy given conda does handle the installing the binaries and bringing the opencv python bindings inside python path.
However getting the same to run in C++ , is a different story. C++ is all about bringing in the right interface (hpp/header files) and binaries (so/dll) in the context of the program being compiled by the “g++” compiler. I will show you how I was able to set my system up for the same. Most tutorials out there will consider using an IDE like Eclipse or Netbeans. For bringing in “g++” (GNU C++ compiler) , we will use MinGW64. Remember we must use the 64bit variant since Mingw32 does not supports posix threads properly in windows. The ming64 binaries are built with a posix implementation that gives native posix but when compiled on windows will be slower compared to something built on windows native API (win32)
Prerequisites
- Windows (10/8/7) system
- Mingw-w64 , you can download from here
- OpenCv 3+ , official github repo
- Cmake , download from here
- Editor and terminal, do not care what you use. You can use notepad 😥 if you wish.
Step 1
Download the OpenCV source from their official git repository. You can choose to download any one of them. In my case I went for this one
Step 2
Setup Mingw-w64. In case you had trouble finding the link. Look here . Go for the link that says Online Installer for MinGW-GW64. Direct link , may break in future.
- Install MinGW-GW64 with the following setting
Make sure posix is selected. Otherwise the OpenCV installation will fail with unable to find mutex error. Which is part of pthreads library and specs. Architecture can be x86(32/64 bit machin) or x86_64(64bit machine) based on your working system.
2. Click next configure path of installation and let the binaries install.
3. After installation make sure to include MinGW bin folder in your path. It should be inside “..basePath/x86_64–8.1.0-posix***/mingw64/bin”
After this just test the installation by opening a terminal and type command “g++ — version”. If it says command not found. It could not get the path ,path was not set right, restart terminal or the machine. It should work! Google for the same !
Step 3
Once done install Cmake. Open Cmake and do the following
- Choose a source code path and a binaries path. Make sure they are separate.
2. Once done click on configure.A dialogue box will appear select MinGW Makefiles. This will make sure that the OpenCV new makefiles are compatible to mingw.
3. Click Finish. Let it generate some stuff. At this stage you should have some list of options to tick on. I did not needed any special attention to any of those options. So go on and click on Generate. Match the output from below, it should say “Configuration done” and “Generating done”
Step 4
Remember we chose a binaries folder, keep that in mind. Now navigate to that folder and open a terminal. In my case the folder I will have to navigate is.
“ C:\Users\Asus\Desktop\opencv”. Open a terminal in this folder. I use git-bash and you can use too. hit command “mingw32-make install” and pray that it builds hopefully. Because if it does my tutorial was successful.
After it’s done it should look like this. Also C++ compilation process takes a lot of time, LOT OF TIME . There are conference discussion on the same !! So you understand how serious is this problem.
At this point a new folder install will be created in your targeted build folder. We are concerned with the contents of this folder. So let’s do one thing. Lets move our build folder to “C:” drive root path that contains the install folder also. Now we add the binary path. Refer from the above image. In the environment variable there is a second path that points at the bin folder add that to environment variable also. Path “ C:\opencv\install\x64\mingw\bin” must be included in environment variable. This was in our case. These paths are IMPORTANT . 😧 Make sure it is there. The bin folder consist of all the various compiled binaries that are required for our OpenCV C++ compiled application. So you can choose to ship your application but then you will require the dll inside the bin to be shipped with your application also. Also since they are dll you cannot statically link them. So “-static” does not works. At least I could not get it done.
Step 5
Write a program
Now once you have the code copied. It’s time to test the installation. Remember to change the image location to your target image. Make sure you do that otherwise empty Mat will be created.
Double check that the include ,lib and bin folder of the install folder are targeted by your g++ compilation command.
If your command worked with no error. If there are linker errors then would advice to check whether linked dll are existing in the folder included by the command.
So hopefully you got a similar out put of size of the image currently being used.
Hope this tutorial helps you start your journey into the rabbit hole of C++ and Computer Vision. For more such content to follow this blog and if you found this helpful maybe a few claps. 😄 ….
Hopefully in the next version we will try to support cuda for Opencv DNN module.
Getting started with OpenCV 2.4 and MinGW on Windows 7
How do I install OpenCV 2.4 and compile my code with MinGW?
7 Answers 7
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:
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 .
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:
If it compiles successfully, it will create an executable named loadimg.exe .
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.
- 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: