Linux/Ubuntu下使用VS Code配置C/C++项目环境调用OpenCV
在Ubuntu下配置VS Code以调用OpenCV的步骤如下:
- 安装VS Code:
sudo apt update
sudo apt install code
- 安装OpenCV:
sudo apt update
sudo apt install libopencv-dev
安装C/C++扩展和CMake工具:
在VS Code中,打开扩展市场并安装以下扩展:
- C/C++
- CMake Tools
配置CMakeLists.txt:
在项目根目录中创建一个CMakeLists.txt文件,并添加以下内容:
cmake_minimum_required(VERSION 3.0.0)
project(my_opencv_project)
set(CMAKE_CXX_STANDARD 11)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(my_opencv_project main.cpp)
target_link_libraries(my_opencv_project ${OpenCV_LIBS})
- 创建源代码文件main.cpp并编写代码调用OpenCV:
#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
cv::Mat image = cv::imread("path_to_image");
if (image.empty()) {
std::cout << "Could not open or find the image" << std::endl;
return -1;
}
cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE);
cv::imshow("Display window", image);
cv::waitKey(0);
return 0;
}
- 在VS Code中打开项目根目录,CMake Tools将自动检测CMakeLists.txt并配置项目。
编译并运行:
在VS Code的终端中,使用以下命令编译项目:
cd <project_directory>
mkdir build
cd build
cmake ..
make
运行生成的可执行文件:
./my_opencv_project
以上步骤配置了一个简单的VS Code环境,用于在Ubuntu上使用OpenCV和C/C++。
评论已关闭