Cmake compilation and installation of OpenCV under Windows

Time:2021-10-10

Cmake compilation and installation of OpenCV under Windows

This is a novice oriented opencv on windows,   Helloword’s tutorial   Here we use vs2019 to compile OpenCV,   And run a HelloWorld to show how to compile and run with cmake
 
0   preparation
Required software: Visual Studio 2019   Community Edition,   CMake,   Add vs2019 msbuild.exe under the system variable   The path of the
 
1. Download:

stay   https://opencv.org/releases/   Download source file

 

2. Compilation:
Open cmake GUI as shown in the figure: select the unzipped source code in browser source and the directory where the build results are stored in build
Then check the content to build, and finally click generate   Here we choose   Visual   Studio 16 2019,   set up   Platform is   x64

 

 

During the generation process, you may encounter download errors caused by network problems (you need to solve the network problems yourself), link library errors, etc. (you can consider removing unnecessary functions)
For example, here we choose OpenCV_ world,   And removed support for Python 3

 

 

three   compile
After generation,   The contents under the build folder should be as follows:
We open opencv.sln with vs,   Select all_ Compile after build
If successful, the following display will appear:

At this time, there should be a generated install folder under the build folder (without selecting install for another compilation). The contents are as follows  
If you can see this, our opencv will be compiled

 

 

Finally, you need to add the bin path of OpenCV to the path of the system environment variable:   D:\Developments\opencv_ build\install\x64\vc16\bin
 
four   Compile and run HelloWorld
We also use cmake to compile and run HelloWorld
 
demo.cpp
 1 #include "opencv2/opencv.hpp"
 2 #include "opencv2/highgui/highgui.hpp"
 3 
 4 using namespace cv;
 5 
 6 int main(int argc, char** argv) {
 7     //create a gui window:
 8     namedWindow("Output",1);
 9     //initialize a 120X350 matrix of black pixels:
10     Mat output = Mat::zeros( 120, 350, CV_8UC3 );
11     //write text on the matrix:
12     cv::putText(output, "Hello World", cv::Point(10,output.rows-18),
13         cv::FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(200,200,250), 1, 8);
14     //display the image:
15     imshow("Output", output);
16     //wait for the user to press any key:
17     waitKey(0);
18     return 0;
19 
20 }
The following is a basic OpenCV. Hello   World level cmake file   Set first   OpenCV_ DIR   To the install folder,   Cmake find_ package   The location of header files and libraries will be found automatically   After compiling the program,   Link required   opencv_ The world library
 
CMakeLists.txt
1 project("DEMO")
 2 
 3 cmake_minimum_required(VERSION 3.21)
 4 
 5 set (opencv_dir "D: / developments / opencv_build / install") # opencv installation location
 6 find_ Package (openCV required) # allows cmake to automatically find opencv header files and libraries
 7 
 8 include_ Directories (${opencv_include_dirs}) # header file to include
 9 add_executable(demo demo.cpp)
10 target_ link_ Libraries (demo "D: / developments / opencv_build / install / x64 / vc16 / lib / opencv_world453d. Lib") # link opencv Library

 

 
After creating a new build folder,   cmake ..
again   MSBuild.exe   DEMO.sln   Compile
last,   The generated file will be displayed in the   Debug   lower