vscode中导入#include “opencv2/opencv.hpp“

时间:2024-03-26 08:17:44

鼠标放到上面 点击快速修复

1.img.cpp

// 图片的读取和显示
// 导入opencv头文件
#include "opencv2/opencv.hpp"
#include <iostream>

int main(int argc, char** argv)
{
    // 读取图片,mat是matrix的缩写,是一个矩阵,类似与numpy ndarray
    cv::Mat image = cv::imread("./media/cat.jpg");
    // 判断是否读取成功
    if (image.empty())
    {
        std::cout << "无法读取图片 " << std::endl;
        return 1;
    }
    // 打印图片高度和宽度
    std::cout << "图片高度: " << image.rows << " 宽度: " << image.cols << std::endl;

    // 打印图片data
    // 以Numpy的方式打印
    // std::cout << "图片data: " << cv::format(image, cv::Formatter::FMT_NUMPY) << std::endl;
    // 以python list的方式打印
    // std::cout << "图片data: " << cv::format(image, cv::Formatter::FMT_PYTHON) << std::endl;

    // 创建一个灰度图
    cv::Mat gray;
    // 转换为灰度图
    cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);
    // 保存
    cv::imwrite("./gray.jpg", gray);

    // // 显示
    // cv::imshow("原图", image);
    // cv::imshow("灰度图", gray);
    // // 等待按键
    // cv::waitKey(0);

}

2.创建cmake-CMakeLists.txt

# 最低版本要求
cmake_minimum_required(VERSION 3.10)

# 项目信息
project(opencv_demo)

# 使用find_package命令查找OpenCV库
find_package(OpenCV REQUIRED)
find_package(gflags REQUIRED)

if (OpenCV_FOUND)
    message(STATUS "OpenCV library status:")
    message(STATUS "    version: ${OpenCV_VERSION}")
    message(STATUS "    libraries: ${OpenCV_LIBS}")
    message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
else()
    message(FATAL_ERROR "Could not find OpenCV library")
endif()

# 添加头文件
include_directories(${OpenCV_INCLUDE_DIRS} ${gflags_INCLUDE_DIRS})
# 链接库
link_libraries(${OpenCV_LIBS} ${gflags_LIBRARIES})

# 添加可执行文件
add_executable(demo_1.img src/1.img.cpp)
add_executable(demo_2.video src/2.video.cpp)
add_executable(demo_3.camera src/3.camera.cpp)

终端--运行生成任务--g++----task.json

{
	"version": "2.0.0",
	"tasks": [
		// 1. cmake 配置
		{
			"label": "cmake 配置",
			"type": "shell",
			"command": "cmake -B build -S . -DCMAKE_BUILD_TYPE=Debug",
			"problemMatcher": [],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"options": {
				"cwd": "${workspaceFolder}"
			}
		},
		// 2. cmake 构建
		{
			"label": "cmake 构建",
			"type": "shell",
			"command": "cmake --build build",
			"problemMatcher": [],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"options": {
				"cwd": "${workspaceFolder}"
			},
			"dependsOn": [
				"cmake 配置"
			]
		},
		// 3. 删除build 目录
		{
			"label": "删除build 目录",
			"type": "shell",
			"command": "rm -rf build",
			"problemMatcher": [],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"options": {
				"cwd": "${workspaceFolder}"
			}
		},
		// 4. 运行可执行文件
		{
			"label": "运行可执行文件",
			"type": "shell",
			"command": "./build/demo_${fileBasenameNoExtension}",
			"problemMatcher": [],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"options": {
				"cwd": "${workspaceFolder}"
			},
			"dependsOn": [
				"cmake 构建"
			]
		}

	]
}

launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "C++ CMake Debug",
            "program": "${workspaceFolder}/build/demo_${fileBasenameNoExtension}",
            "args": [],
            "cwd": "${workspaceFolder}",
            "preLaunchTask": "cmake 构建",
        }
    ]
}