OpenCV C++ installation on Windows with MinGW

Rajat Kanti Bhattacharjee
csmadeeasy
Published in
5 min readMar 3, 2020

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

  1. Windows (10/8/7) system
  2. Mingw-w64 , you can download from here
  3. OpenCv 3+ , official github repo
  4. Cmake , download from here
  5. 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.

  1. Install MinGW-GW64 with the following setting
Fig: Posix thread must be chosen. MUST BE.

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”

Fig: The path is based on your installation folder so do accordingly.

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

  1. Choose a source code path and a binaries path. Make sure they are separate.
Fig: Make sure this is how it is

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.

Fig: Make sure this is how it is.

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”

Fig: look ate the output

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.

Fig: During build process

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.

Fig: One eternity later. your build is done.

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

Fig: C++ code

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.

Fig:

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.

Fig: Phew !!! That worked.

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.

--

--

Rajat Kanti Bhattacharjee
csmadeeasy

Your everyday programmer. Talking about everyday engineering. Love natural science, physics buff and definitely not good at no-scope 360.