Compiling OpenCV with MinGW for Qt in Windows

This is a simple update of a previews article which I had written some years ago for compiling OpenCV in Ubuntu linux. This time I am going to be compiling OpenCV under MinGW 5.3.0 for Windows so that I can use OpenCV in Qt with the MinGW compiler. I used similar steps to compile the OpenCV for my portable IDE tutorial.
Once again before continuing I will like to thank the people at OpenCV for all the work they have put into it and if you have not visited their webpage I recommend you do.
http://opencv.org/
I will also like to thank the people at Qt for their brilliant library and to allow users to use it for open source purposes.
https://www.qt.io/
So to get started we need to download and install Qt with mingw compiler this is straight forward so I am not going to go into that.

Then download the latest source from github of OpenCV, this can be found here:
https://github.com/opencv/opencv

Also downlaod and install CMake this can be find here: CMAKE

Once the Qt has been installed make sure to have the location of the bin directory for MinGW on the PATH (PATH can be found on environmental variables in system properties. Make sure to logout and in after updating PATH), while installing Qt this may be done for you.
To check this got to system:

Then go to system properties:

Ant there to environment variables:

There you should add the path to the bin directory of MinGW to the above highlighted Path. In my case the path is “C:\Qt\Tools\mingw530_32\bin”. Make sure to log out and in to make sure the PATH is reloaded.

Once this has been done open CMake, and place the location of the OpenCV source on the source edit and place the directory of where you want to locate the build directory in my case this looks as follow:

Then click on configure and the section marked in red as above should show up if there are no errors.
Make sure to disable the use of pre-compile headers to be used as mingw will not like this and generate again. After configuring again you can generate.

By default OpenCV will be installed on a folder named install inside the same build directory.

Now that we have generated the build we can make and install the OpenCV library. To do this we will have to open a command line and go to the build directory and make the project with mingw32-make.exe and then mingw32-make.exe install.
To be able to make you will also have to add #define NO_DSHOW_STRSAFE on the top of the file.

After mingw32-make install then tall the compiled files should be installed under the install folder inside the build directory. Now we can use the compile library with Qt creator compiling with mingw.

Hope this helps anyone that need this.

Cheers,

Guivi