Ubuntu下源码编译 OpenCV(测试有效)

文章目录

    • 下载源码
    • 安装依赖项
    • 开始编译
    • 测试示例
    • 运行示例


下载源码

opencv:https://github.com/opencv/opencv
opencv-contrib:https://github.com/opencv/opencv_contrib
如下图所示的红色框内,Release 各种版本的【opencv】,【opencv-contrib】也有对应的【Release】版本。
Ubuntu下源码编译 OpenCV(测试有效)_第1张图片
下载 OpenCV 4.4.0 的 Source Code,如下图所示,
Ubuntu下源码编译 OpenCV(测试有效)_第2张图片

安装依赖项

# 安装系统依赖
sudo apt install build-essential cmake git libgtk2.0-dev \
                 pkg-config libavcodec-dev libavformat-dev libswscale-dev \
                 libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev \
                 libjasper-dev libdc1394-22-dev
# 上述命令执行过程中,会出现无法定位到安装包【libjasper-dev】

# 添加源以继续安装依赖 libjasper-dev
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"

# 安装系统依赖(可以根据需要,去掉部分依赖项)
sudo apt install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev \
                 libjasper-dev libdc1394-22-dev libsnappy-dev libboost-all-dev \
                 python3-dev python3-numpy ffmpeg cmake-qt-gui libopenblas-dev \
                 tesseract-ocr libtesseract-dev libprotobuf-dev libleveldb-dev \
                 libhdf5-serial-dev protobuf-compiler libatlas-base-dev \
                 libgflags-dev libgoogle-glog-dev liblmdb-dev libfaac-dev \
                 gfortran libgstreamer1.0-dev libatlas-base-dev libxvidcore-dev \
                 libpng-dev libopenexr-dev libtiff-dev libwebp-dev \
                 libmp3lame-dev libtheora-dev libvorbis-dev  \
                 libopencore-amrwb-dev x264 v4l-utils libgdk-pixbuf2.0-dev \
                 manpages-dev libopencore-amrnb-dev libgstreamer-plugins-base1.0-dev \
                 libqt5widgets5 libqt5gui5 libqt5dbus5 libqt5network5 libqt5core5a \
                 qtcreator qt5-default

# 修复可能安装出错的依赖
sudo apt install -f

开始编译

# 在 opencv4.4.0源码解压后的文件夹下面,创建编译文件夹
mkdir build && cd build

# 配置编译选项
# CMAKE_INSTALL_PREFIX: 是最终OpenCV的安装位置,可以自行修改安装位置
# OPENCV_ENABLE_NONFREE:指示是否开启Non-free的算法
# OPENCV_EXTRA_MODULES_PATH: 指示扩展算法的源码文件夹
# WITH_CUDA: 该选项需要确保自己已安装显卡驱动和cuda
# 可以关注下命令的输出,可以从中找到哪些模块没有配置成功
cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4.4 \
      -D OPENCV_ENABLE_NONFREE=ON \
      -D OPENCV_EXTRA_MODULES_PATH=/home/zfb/opencv_contrib-4.4.0/modules \
      -D OPENCV_GENERATE_PKGCONFIG=YES \
      -D WITH_QT=ON \
      -D WITH_OPENGL=ON \
      -D WITH_CUDA=ON \
      -D BUILD_EXAMPLES=ON \
      -D INSTALL_PYTHON_EXAMPLES=ON \
      -D INSTALL_C_EXAMPLES=ON ..

# 开启12个线程同时编译源码
make -j12

# 安装OpenCV到指定位置
sudo make install

测试示例

参考网址:https://docs.opencv.org/4.x/db/df5/tutorial_linux_gcc_cmake.html

测试代码

#include 
#include 
using namespace cv;
int main(int argc, char** argv )
{
    if ( argc != 2 )
    {
        printf("usage: DisplayImage.out \n");
        return -1;
    }
    Mat image;
    image = imread( argv[1], IMREAD_COLOR );
    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}

CMakeLists.txt(官方提供)

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

编译程序

mkdir build && cd build
cmake ..
make

cmake … 会出错,如下图所示

Ubuntu下源码编译 OpenCV(测试有效)_第3张图片
按照提示,在【find_packages】之前添加下面的内容,也即是添加opencv的安装路径(install prefix),

list(APPEND CMAKE_PREFIX_PATH "/usr/local/opencv4.3")

完整的 CMakeLists.txt 如下

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
list(APPEND CMAKE_PREFIX_PATH "/usr/local/opencv4.3")
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

或者按照下面的写法也可以

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV 4.3 REQUIRED ) # 添加具体安装版本的Tag 4.3
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

运行示例

./DisplayImage lena.jpg

Ubuntu下源码编译 OpenCV(测试有效)_第4张图片

你可能感兴趣的:(OpenCV-C++,opencv,ubuntu,源码编译)