准备工作-安装OpenCV4.6.0
参考之前的链接:Ubuntu20.04 安装OpenCV4.6.0
准备工作2:安装VsCode
正式工作
-
新建一个文件夹,打开VsCode,点击
文件
,打开文件夹
。 -
点击
扩展
,搜索vc++
,安装扩展即可。 -
新建一个
cpp
文件,随便找张图片链接,写入下列代码。
#include <iostream>
#include <string>
#include <filesystem>
#include <opencv2/opencv.hpp>
int main() {
std::filesystem::path file_path(__FILE__);
std::filesystem::path dir_path(file_path.parent_path());
std::filesystem::path img_path(dir_path.append("lena.jpg"));
// 判断文件是否存在,不存在就退出
if (!std::filesystem::exists(img_path)) {
std::cout << img_path << "不存在,请检查" << std::endl;
}
cv::Mat img = cv::imread(img_path);
cv::namedWindow("lena", cv::WINDOW_NORMAL);
cv::displayOverlay("lena", "overlay", 5000); // 显示5秒?
cv::displayStatusBar("lena", "status bar", 5000); // 显示5秒
cv::saveWindowParameters("lena"); // 保留参数
cv::loadWindowParameters("lena");
cv::imshow("lena", img);
cv::waitKey(0);
cv::destroyAllWindows();
return 0;
}
- 然后应该会报错,选择生成调试文件,依次选择
gdb
,g++
,之后会自动生成task.json
,launch.json
文件,修改后如下所示。
- 其中task.json文件如下:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
&