Visual Studio 2022 cmake configures opencv development environment

1. Environment and Description

Here I am using widnows 10 64位, Visual StudiousingVisual Studio Community 2022 (社区版)

For Androiddevelopment engineers, why use Visual Studioit?
Because it is convenient to Visual Studiodevelop and debug in . After development and debugging, it can be transplanted to .OpenCVAndroid

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

insert image description here

Here we download widnowsthe version, which corresponds toopencv-4.8.0-windows.exe

2.1 Install OpenCV

Double-click opencv-4.8.0-windows.exeto install it in the specified directory, such asD:\Developer\opencv4.8.0

The installation opencv-4.8.0-windows.exeis 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

insert image description here

3.1 Configure the required environment

Visual StudioThe environment required for installation and configuration is required, as 工具 > 获取工具和功能can be seen in

insert image description here

We need to check Use C++的桌面开发and使用C++的Linux开发

insert image description here
insert image description here
Tick ​​and Runtime 单个组件中 SDK 、库和框架underWindows 10 SDKWindows 通用 C

insert image description here
编译器、生成工具和运行时Tick ​​under用于 Windows 的 C++ CMake 工具

insert image description here

4 Create a new CMake project

After configuring the environment, we open Visual Studioand create a new CMakeproject

insert image description here
Here we name the project asOpenCVTest

insert image description here
Click Create, select the startup item, OpenCVTest.exeand then click Run.
insert image description here
You can see such a command line window, which means the operation is successful.

insert image description here

5. Verify OpenCV configuration

5.1 Copy opencv_world480d.dll

Will D:\Developer\opencv4.8.0\opencv\build\x64\vc16\bin\opencv_world480d.dllbe copied to E:\WorkSpace\Demo\OpenCV\OpenCVTest\out\build\x64-debug\OpenCVTestthe directory, that is, in OpenCVTest.exethe same directory as

opencv_world480d.dll : Debug versionopencv_world480.dll
: Release version

insert image description here

5.2 Configure CMakeList.txt

In the project (and cppat 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 Dthe root directory of the disk, name it and write the following code ImageTest.jpg
in it OpenCVTest.cppto 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.
insert image description here
At this point, the configuration Visual Studioin the project OpenCVis successful.

Guess you like

Origin blog.csdn.net/EthanCo/article/details/131911635