Foreword: Hey, I have not used VS to create a cmake project for a long time, and the configuration of opencv is rusty, so record it
premise:
First make sure you have downloaded opencv, my opencv version is 4.5.5, and unzip it to a folder you can remember, as shown in the figure
The directory I decompressed opencv is: E:\opencv, in the opencv folder of the E drive
first step:
After the prerequisites are ready, you need to configure the environment variables. My Computer -> Right-click -> Properties -> Advanced System Settings -> Environment Variables -> System Variables -> Path
Create a new one in the path, configure the environment of opencv (find the decompressed path):
E:\opencv\opencv\build\bin
E:\opencv\opencv\build\x64\vc15\bin
I am vs2022, so I choose vc15, different versions, choose different vc, see the table below:
VS version | VC version |
---|---|
VS2003 | VC7 |
VS2005 | VC8 |
VS2008 | VC9 |
VS2010 | VC10 |
VS2012 | VC11 |
VS2013 | VC12 |
VS2015 | VC14 |
VS2017 | VC15 |
After the environment variables are configured, the next thing to do is to create a cmake project with vs and modify the content in cmaklist.txt.
In cmakelist.txt, fill in the following content:
# CMakeList.txt: CMakeProject1 的 CMake 项目,在此处包括源代码并定义 # 项目特定的逻辑。 # cmake_minimum_required (VERSION 3.8) add_executable (CMakeProject1 "CMakeProject1.cpp" "CMakeProject1.h") # 将源代码添加到此项目的可执行文件。 set(OpenCV_DIR E:/opencv/opencv/build) find_package(OpenCV REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS}) link_directories(${OpenCV_LIBRARY_DIRS}) target_link_libraries(CMakeProject1 ${OpenCV_LIBS}) # TODO: 如有需要,请添加测试并安装目标。
Where CMakeProject1 is the name of the created project (default, I did not change the name)
At this point, you can call opencv in vs.
have a test:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
cv::Mat img = cv::imread("E:/cmake_project/CMakeProject1/CMakeProject1/imgs/1305031102.175304.png");
cv::namedWindow("test");
cv::imshow("test", img);
cv::waitKey(0);
}
Finish! ! !