本文最后更新于:2024年6月25日 上午
本文记录 Ubuntu 下 opencv C++ 环境配置,以 4.7.0 为例。
基础流程
- 下载 opencv 源码
- 安装相关依赖
- 源码编译、安装
- 配置环境
具体配置
以 opencv 4.7.0 版本为例
基础工具
1 2 3
| sudo apt install cmake sudo apt install g++ sudo apt install make
|
资源下载
opencv 源码下载
opencv 拓展库 contrib 源码下载
解压两个源码包,将 contrib 放到 opencv 文件夹内
新建 build 文件夹
1 2 3
| unzip opencv_* mv opencv_contrib-4.7.0/ opencv-4.7.0/ cd opencv-4.7.0 && mkdir build && cd build
|
安装依赖库
可以根据需要选择安装
1 2 3 4 5 6 7 8
| sudo apt-get install build-essential sudo apt-get install python-numpy python3-numpy python-pandas python3-pandas sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff5-dev libdc1394-22-dev sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev sudo apt-get install libxvidcore-dev libx264-dev # 处理视频所需的包 sudo apt-get install libatlas-base-dev gfortran # 优化opencv功能 sudo apt-get install ffmpeg
|
编译安装
1 2 3 4 5
| cd build/ cmake -D CMAKE_INSTALL_PREFIX=/usr/local -D CMAKE_BUILD_TYPE=build -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.7.0/modules/ .. (缺资源就下载复制到.cache中) make -j8 sudo make install
|
OPENCV_EXTRA_MODULES_PATH
指 contrib 相对路径
环境配置
1 2 3
| sudo vim /etc/ld.so.conf 增加一行: usr/local/lib sudo ldconfig
|
1 2
| include /etc/ld.so.conf.d
|
在环境变量中添加:
1
| export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
|
对于 opencv 4
1
| sudo ln -s /usr/local/include/opencv4/opencv2/ /usr/local/include/
|
测试运行
源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include <opencv2/core.hpp> #include <opencv2/imgcodecs.hpp> #include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
int main() { std::string image_path = "/home/vvd/VVD/lab/cpp-opencv-test/test.png"; Mat img = imread(image_path, IMREAD_COLOR);
if(img.empty()) { std::cout << "Could not read the image: " << image_path << std::endl; return 1; }
imshow("Display window", img); int k = waitKey(0);
if(k == 's') { imwrite("starry_night.png", img); }
return 0; }
|
测试图像:
源码中 image_path 改成自己的图像路径
命令行编译
1
| g++ -o test_opencv test_opencv.cpp -L /usr/local/lib/ -lopencv_core -lopencv_highgui -lopencv_imgcodecs
|
执行可执行文件
实现效果
Cmake 编译
创建 CMakeLists.txt
文件,放在 test_opencv.cpp
文件旁边
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #cmake needs this line cmake_minimum_required(VERSION 3.1)
#Define project name project(Opencv_test)
#Find OpenCV, you may need to set OpenCV_DIR variable find_package(OpenCV REQUIRED)
#Enable C++11 set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
#Declare the executable target built from your sources add_executable(Opencv_test test_opencv.cpp)
#Link your application with OpenCV libraries target_link_libraries(Opencv_test PRIVATE ${OpenCV_LIBS})
|
在该文件夹下执行命令
1 2 3 4
| mkdir build && cd build cmake .. make ./Opencv_test
|
执行效果
参考资料
文章链接:
https://www.zywvvd.com/notes/coding/cpp/ubuntu-cpp-opencv47/ubuntu-cpp-opencv47/