Installation Steps
I could not find anything about installing OpenCV without using the Visual Studio compiler, so I decided to share the way I did it using MinGW gcc compiler.
Create a directory at
C:\
, e.g.,C:\opencv
(this is where theopencv-[version_number]
andopencv_contrib-[version_number]
source files will be extracted).Go to opencv github and download from the master branch the latest tag. (It will be downloaded as
opencv-[version_number]
)
Go to opencv contrib and download the same master and tag version as in the previous step
Extract the two source files at the directory created in step 1 (
C:\opencv
in my case) and create an empty folder named build**.** This is how it should look so farDownload the MinGW→ gcc compiler by following step 3 to 6 of the following link: Get Started with C++ and Mingw-w64 in Visual Studio Code. If you would like to use visual studio code as your editor, then also follow step 1 and 2 from that same link.
Download the cmake binary distribution for windows at **Download | CMake
That’s all we need! It is time to generate those build files, compile it, and execute it.
IMPORTANT: Make sure in your "edit environmental variables” that only `C:\msys64\mingw64\bin` is set in path **and not** `C:\msys64\usr\bin` (as in image from step 10). There are several make commands and that other path contains the ‘msys make’ and you dont want to combine make commands during compilation( we only need the ‘mingw make’).
To generate those build files open Cmake GUI and set your source and build directories and then click `Configure` with Mingw. This will scan the source code for a `CmakeList.txt` and set the parameters to later create a MakeFile (next step) inside the build folder. This MakeFile file contains all the instructions for compilation.Before we click generate. This is the part where we can set everything: CUDA, Cudnn, math libraries, GPU, etc. We are only setting opencv contrib but if you want to set GPU configurartion the info is specified below. Now search for “extra” and set the path to the contrib modules like in image. Finally click
Configure
to update and thenGenerate
.
GPU CONFIG
Search and set the following:
WITH_CUDA = ON
OPENCV_DNN_CUDA=ON
CUDA_STUBS=ON
ENABLE_FAST_MATH=1
BUILD_opencv_dnn =ON
BUILD_opencv_world =ON
Click Configure to update/unlock new config parameters (Then continue)
CUDA_FAST_MATH=ON
CUDA_ARCH_BIN=7.5 # Check yours according to your GPU Here!!!!
CMAKE_BUILD_TYPE=RELEASE
CMAKE_CONFIGURATION_TYPES=RELEASE
Click Configure to update/unlock new config parameters (Then continue)
BUILD_EXAMPLES = OFF # OPTIONAL
INSTALL_PYTHON_EXAMPLES=OFF # OPTIONAL
INSTALL_C_EXAMPLES=OFF # OPTIONAL
WITH_CUBLAS=1
WITH_CUDNN = ON
OPENCV_ENABLE_NONFREE=ON
WITH_TBB=ON # For Thread blocking
CMAKE_INSTALL_PREFIX=C:\opencv\build\install
All configs can be found here
Time to compile and run! Open a terminal,
cd
inside your build file where now is full of files and most importantly lies your MakeFile.> RULE of THUMB: Always match cmake makefile generation with make command. > cmake mingw makefiles → mingw32-make > cmake msys makefile → msys make > cmake visual studio makefiles → vc make
At this directory level type in your terminal
wingw32-make
and enjoy! (It will take a while: maybe hours). Finally run the executable.Final step is to set the opencv environmental variables so that you can later use it in your code. It should like this:
OpenCV Test Example! Read an Image
Create a directory say opencvtest
and under your directory place the following:
img.png
main.cpp
CMakeLists.txt
main.cpp
NOTE: Use double backslash & single forward for path.
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
Mat image;
image = imread("C:/path_to_image/img.png");
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
CMakeLists.txt
NOTE: Use as project name same as that of directory you created. In my case opencvtest
cmake_minimum_required(VERSION 3.0.0)
project(opencvtest VERSION 0.1.0)
include(CTest)
enable_testing()
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(opencvtest main.cpp)
target_link_libraries( opencvtest ${OpenCV_LIBS} )
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
Compile & Run
NOTE: opencvtest is this project’s main directory
cd opencvtest
mkdir build
cd build
cmake -G "MinGW Makefiles" .. #Create makefiles using MinGW with instructions at ..
mingw32-make # create executable
opencvtest.exe # RUN
REFERENCES