OpenCV安装:最基础的openCV程序运行示例

·  阅读 36
OpenCV安装:最基础的openCV程序运行示例

openCV 下载安装


基础环境

  • 操作系统:Ubuntu 18.04.5
  • 编程工具:bash shell
  • VScode
  • 基础的 gcc、G++ 安装

下载一个喜欢的版本即可

咕泡AI-感谢查阅

极简【无脑】安装

# 解压到服务器目录
unzip opencv-4.5.3

cd opencv-4.5.3/

# 开始安装之旅

mkdir build
cd build/

# root 用户直接运行,会默认编译安装到/usr/local 目录下
cmake ..
make -j16
make install

# 普通用户,没有 /usr/local 写入权限,需要 指定安装路径

make install DESTDIR=/home/Moli/usr/local 
复制代码

openCV 读取保存图像【极简示例】


程序只包含 | example.cpp 和 CMakeLists.txt 即可

example.cpp

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    cout << "Built with OpenCV " << CV_VERSION << endl;
    Mat src = cv::imread("../conda.png");
    // cv::imshow("src", src);
    // waitKey(0);
    // 图像缩放
    int width{256}, height{256};
    cv::resize(src, src, cv::Size(width, height));

    cv::imwrite("256src.png", src);
    return 0;
}
复制代码

CMakeLists.txt

配置 OpenCV_DIR 路径【openCV build 目录即可】

# cmake needs this line
cmake_minimum_required(VERSION 3.1)

# Define project name
project(first_墨理_project)

# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI

set(OpenCV_DIR /home/墨理/project/project21Next/vscodeFirst/opencv-4.5.3/build)
find_package(OpenCV REQUIRED)

# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    config: ${OpenCV_DIR}")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

# Declare the executable target built from your sources
add_executable(first example.cpp)

# Link your application with OpenCV libraries
target_link_libraries(first PRIVATE ${OpenCV_LIBS})
复制代码

Linux 下编译运行

命令如下

mkdir build

cd  build/

cmake ..
make

# 生成得到可执行文件 first | 运行即可 

./first 
复制代码

查看生成的缩放图片【没看错,是在下】

点赞+关注

  • 计算机视觉:超分重建、图像修复、目标检测、风格迁移 等领域 稍有所学
  • AI 工程化:Ncnn、MNN、TensorRT 正在 学习
  • C++、Python、Java 略懂一二

喜欢请关注 咕泡AI回复:168

取经路上,让咕泡AI 陪你畅享更多有趣AI

分类:
人工智能
标签:
收藏成功!
已添加到「」, 点击更改