1. Environment and Description
Here I am using widnows 10 64位
, Visual Studio
usingVisual Studio Community 2022 (社区版)
For
Android
development engineers, why useVisual Studio
it?
Because it is convenient toVisual Studio
develop and debug in . After development and debugging, it can be transplanted to .OpenCV
Android
2. Download OpenCV version
The official address is here: official download address
. However, the official download address may be slower. You can download the resources I uploaded: OpenCV-4.8.0 package download
Here we download widnows
the version, which corresponds toopencv-4.8.0-windows.exe
2.1 Install OpenCV
Double-click opencv-4.8.0-windows.exe
to install it in the specified directory, such asD:\Developer\opencv4.8.0
The installation
opencv-4.8.0-windows.exe
is actually to unzip it into a directory
3. Download Visual Studio
Just download the Visual Studio Community 2022 community edition here, free of charge, download address: Visual Studio Download
3.1 Configure the required environment
Visual Studio
The environment required for installation and configuration is required, as 工具 > 获取工具和功能
can be seen in
We need to check Use C++的桌面开发
and使用C++的Linux开发
Tick and Runtime 单个组件中 SDK 、库和框架
underWindows 10 SDK
Windows 通用 C
编译器、生成工具和运行时
Tick under用于 Windows 的 C++ CMake 工具
4 Create a new CMake project
After configuring the environment, we open Visual Studio
and create a new CMake
project
Here we name the project asOpenCVTest
Click Create, select the startup item, OpenCVTest.exe
and then click Run.
You can see such a command line window, which means the operation is successful.
5. Verify OpenCV configuration
5.1 Copy opencv_world480d.dll
Will D:\Developer\opencv4.8.0\opencv\build\x64\vc16\bin\opencv_world480d.dll
be copied to E:\WorkSpace\Demo\OpenCV\OpenCVTest\out\build\x64-debug\OpenCVTest
the directory, that is, in OpenCVTest.exe
the same directory as
opencv_world480d.dll : Debug versionopencv_world480.dll
: Release version
5.2 Configure CMakeList.txt
In the project (and cpp
at the same level) CMakeLists.txt
, add the directory of the OpenCV header file and replace it \
with /
, the specific code is as follows
# CMakeList.txt: OpenCVTest 的 CMake 项目,在此处包括源代码并定义项目特定的逻辑。
# 指定要引用的dll的头文件所在路径
include_directories("D:/Developer/opencv4.8.0/opencv/build/include")
# 指定该dll的lib所在路径
link_directories("D:/Developer/opencv4.8.0/opencv/build/x64/vc16/lib")
# 将源代码添加到此项目的可执行文件。
add_executable (OpenCVTest "OpenCVTest.cpp" "OpenCVTest.h")
# 指定链接库的名字,即dll
# 需要将opencv_world480d.dll复制到out\build\x64-Debug\OpenCVTest目录下,即和OpenCVTest.exe同一目录下
target_link_libraries(OpenCVTest opencv_world480d.lib)
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET OpenCVTest PROPERTY CXX_STANDARD 20)
endif()
5.3 Write cpp code
Copy a picture to D
the root directory of the disk, name it and write the following code ImageTest.jpg
in it OpenCVTest.cpp
to display the picture in the form of the original picture and the gray and white picture.
#include "OpenCVTest.h"
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main()
{
cout << "Hello CMake." << endl;
Mat img = imread("D:/ImageTest.jpg");
imshow("img", img); //显示原图
cvtColor(img, img, COLOR_BGR2GRAY);
imshow("img2", img); //显示灰色图片
waitKey();
return 0;
}
5.4 Operation effect
Click to run, the display effect is as follows, the two pop-up boxes show the original image and gray-white image respectively.
At this point, the configuration Visual Studio
in the project OpenCV
is successful.