OpenCV下载
https://opencv.org/releases/
无脑下载,选则一个存放的目录
将bin路径`C:\OpenCV\build\x64\vc16\bin`添加到系统的Path中
CMakeLists 示例
cmake_minimum_required(VERSION 3.5)
project(TestOpenCVProject)
# 查找OpenCV库
set(OpenCV_DIR "C:/OpenCV/build")
find_package(OpenCV REQUIRED)
# 包含OpenCV头文件目录
include_directories(${OpenCV_INCLUDE_DIRS})
# 指定源文件创建可执行文件
add_executable(main main.cpp)
# 链接OpenCV库
target_link_libraries(main ${OpenCV_LIBS})
`main.cpp`示例
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
int main() {
// Path to the image file
// std::string imagePath = "path/to/your/image.jpg";
std::string imagePath = "../test.png";
// Load the image
cv::Mat image = cv::imread(imagePath, cv::IMREAD_COLOR);
// Check if the image was loaded successfully
if(image.empty()) {
std::cerr << "Could not open or find the image" << std::endl;
return -1;
}
// Create a window for display
cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE);
// Show our image inside it
cv::imshow("Display window", image);
// Wait for a keystroke in the window
cv::waitKey(0);
return 0;
}