ubuntu18.04下编译OpenCV3.4.9

2022-01-12 11:01:58 浏览数 (1)

一、环境介绍

ubuntu版本: VM虚拟机运行ubuntu18.04 64位

OpenCV版本: 3.4.9

二、相关源码包下载地址

OpenCV源码下载地址: https://opencv.org/releases/

选择3.4.9版本下载。

三、编译OpenCV

3.1 安装cmake

代码语言:javascript复制
wbyq@wbyq:~/work$ cmake-gui
 
Command 'cmake-gui' not found, but can be installed with:
 
sudo snap install cmake         # version 3.16.5, or
sudo apt  install cmake-qt-gui
 
See 'snap info cmake' for additional versions.
 
wbyq@wbyq:~/work$

没有cmake就执行下的命令安装。

代码语言:javascript复制
wbyq@wbyq:~/work/opencv-3.4.9/_install$ sudo apt-get install cmake
wbyq@wbyq:~/work/opencv-3.4.9/_install$ sudo apt  install cmake-qt-gui
wbyq@wbyq:~/work/opencv-3.4.9/_install$ sudo apt-get install libgtk2.0-dev
wbyq@wbyq:~/work/opencv-3.4.9/_install$ sudo apt-get install pkg-config

3.2 解压OpenCV源码

代码语言:javascript复制
wbyq@wbyq:~/work_pc$ unzip /mnt/hgfs/linux-share-dir/opencv-3.4.9.zip 
wbyq@wbyq:~/work_pc$ ls
arm-linux-gcc  opencv-3.4.9  rk3399
wbyq@wbyq:~/work_pc$ cd opencv-3.4.9/
wbyq@wbyq:~/work_pc/opencv-3.4.9$ ls
3rdparty  apps  cmake  CMakeLists.txt  CONTRIBUTING.md  data  doc  include  LICENSE  modules  platforms  README.md  samples

3.3 配置cmake环境

在源码目录下可以先创建一个_install文件夹用于存放编译之后的文件,再打开cmake配置页面。

代码语言:javascript复制
wbyq@wbyq:~/work_pc/opencv-3.4.9$ cmake-gui

选择OpenCV源码文件夹和存放编译文件的文件夹。

完成步骤之后,关闭图像配置界面,进入到源码安装目录下,看是否生成了Makefile文件,没有问题就开始编译安装。

代码语言:javascript复制
wbyq@wbyq:~/work_pc/opencv-3.4.9$ cd _install/
wbyq@wbyq:~/work_pc/opencv-3.4.9/_install$ ls
3rdparty              cmake_uninstall.cmake    cvconfig.h       modules                        opencv_python_config.cmake  unix-install
apps                  CMakeVars.txt            cv_cpu_config.h  opencv2                        opencv_python_tests.cfg     version_string.tmp
bin                   configured               data             OpenCVConfig.cmake             opencv_tests_config.hpp
CMakeCache.txt        CPackConfig.cmake        doc              OpenCVConfig-version.cmake     python_loader
CMakeDownloadLog.txt  CPackSourceConfig.cmake  include          opencv_data_config.hpp         setup_vars.sh
CMakeFiles            CTestTestfile.cmake      lib              OpenCVGenPkgConfig.info.cmake  test-reports
cmake_install.cmake   custom_hal.hpp           Makefile         OpenCVModules.cmake            tmp
wbyq@wbyq:~/work_pc/opencv-3.4.9/_install$ make && make install

为了方便使用OpenCV,可以写一个配置文件,比如:opencv_3.4.9.profile

内容如下:

代码语言:javascript复制
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/wbyq/work_pc/opencv-3.4.9/_install/install/lib
export PATH=$PATH:/home/wbyq/work_pc/opencv-3.4.9/_install/install/bin

然后在命令行执行:source opencv_3.4.9.profile

代码语言:javascript复制
wbyq@wbyq:~/work_pc/opencv-3.4.9/_install/install$ pwd
/home/wbyq/work_pc/opencv-3.4.9/_install/install
wbyq@wbyq:~/work_pc/opencv-3.4.9/_install/install$ gedit opencv_3.4.9.profile
wbyq@wbyq:~/work_pc/opencv-3.4.9/_install/install$ source opencv_3.4.9.profile 
wbyq@wbyq:~/work_pc/opencv-3.4.9/_install/install$

在opencv-3.4.9/samples/cpp/example_cmake目录里官方已经给出了一个example可以拿来测试opencv,使用编译器编译,编译时加上OpenCV相关的库和头文件。

编译示例:

代码语言:javascript复制
wbyq@wbyq:~/work_pc/opencv-3.4.9/samples/cpp/example_cmake$ g   example.cpp -L /home/wbyq/work_pc/opencv-3.4.9/_install/install/lib/ -I /home/wbyq/work_pc/opencv-3.4.9/_install/install/include/ -lopencv_calib3d -lopencv_objdetect -lopencv_core -lopencv_photo -lopencv_dnn -lopencv_shape -lopencv_features2d  -lopencv_stitching -lopencv_flann -lopencv_superres -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs  -lopencv_video -lopencv_imgproc -lopencv_videostab -lopencv_ml

运行效果: 这份例子代码里默认打开的是/dev/video0设备节点。

0 人点赞