在Ubuntu16.04和Ubuntu18.04环境下移植过opencv-3.4.5及opencv-contrib-3.4.5,并结合cortex-A9和cortex-A72对opencv&Qt5.14.2进行过移植,期间遇到过很多的问题,实属让人头疼,本次移植借助网友的一些参考及自己的移植经验实现了在ubuntu 20.04移植OpenCV-3.4.5,以下是安装记录过程
提示:所有需要文件均在以下连接中提供
链接:https://pan.baidu.com/s/1XDupTIOHSE-YDuMbhsGJkg
提取码:hell
opencv 下载 https://opencv.org/releases/
opencv_contrib 下载 GitHub - opencv/opencv_contrib: Repository for OpenCV’s extra modules
1、常见安装目录
常见目录用于OpenCV移植操作,本人创建路径为管理员家目录下
sudo mkdir ~/opencv
2、切换ubuntu 20.04系统的安装源为主安装源
3、安装依赖文件
1)创建脚本文件shell.sh
sudo gedit shell.sh
2)将下面指令写入脚本
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install qt5-default ccache libv4l-dev libavresample-dev libgphoto2-dev libopenblas-base libopenblas-dev doxygen openjdk-8-jdk pylint libvtk6-dev
sudo apt-get install pkg-config
3)保存退出
ctrl s
ctrl q
4)添加执行权限
sudo chmod +x shell.sh
5)执行脚本
sudo ./shell.sh
提示:在更新和下载依赖文件的时候可能会出现无法下载或者地址无效问题,因系统而异,可私信解决
网友运到问题,本人未曾遇到
$ sudo apt-get install libjasper-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libjasper-dev
解决方案
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
4、安装opencv和opencv_contrib
先解压下载的opencv-3.4.5及opencv-contrib-3.4.5,本人一直过程中将两个压缩文件存放至家目录下的opencv目录下(~/opencv)
1)解压指令如下:
sudo unzip opencv-3.4.5.zip
sudo tar zxvf opencv_contrib-3.4.5.tar.gz
2)将opencv_contrib-3.4.5拷贝至opencv-3.4.5目录
sudo cp -r opencv_contrib-3.4.5 opencv-3.4.5
3)创建build文件夹 — 用于存放生成的CMake文件
cd opencv-3.4.5 #进入opencv文件夹
sudo mkdir build #建立build文件夹方便后续操作
4)cmake编译
cd build
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv-3.4.5/opencv_contrib-3.4.5/modules/ -D OPENCV_ENABLE_NONFREE=True ..
注意 OPENCV_EXTRA_MODULES_PATH 代表你放置opencv_contrib的modules文件夹的真实地址,如果直接将opencv放在home下那地址与上述参数相同,只需要注意版本号即可。
网友提供可能遇到的问题:
ippicv和face_landmark_model.dat下载失败的解决方案
下载文件
文件的下载地址可以在提示文件中找到,直接去该地址下载。或者通过以下链接
链接:https://pan.baidu.com/s/1XDupTIOHSE-YDuMbhsGJkg
提取码:hell
打开
sudo gedit ~/opencv/opencv-3.4.5/3rdparty/ippicv/ippicv.cmake
提示:根据自己的存放路径查找指定的文件
将ippicv.cmake文件下的第47行替换为本地路径
"改为相应地址" #本地路径,与你自己下载存放的地址相关
#"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/" #原下载地址
#记得改为本地ippcv的地址
更改完ippicv之后,再更改face_landmark_model配置文件,方法大致相同
同样打开
/root/opencv/opencv-3.4.5/opencv_contrib-3.4.5/modules/face/CMakeLists.txt
然后将CMakeLists.txt文件的第19行修改为本地路径,即将原来的网址修改为下载的文件保存的路径。这个行数不定,与文件相关,主要是找到网址地址。
"本地地址" #本地路径,与你自己下载存放的地址相关
#"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${__commit_hash}/" #原下载地址
这样, ippicv和face_landmark_model.dat下载失败的问题就得以解决。
生成完毕后会生成如下的类似界面
-- Install path: /usr/local
--
-- cvconfig.h is in: /home/ubuntu/opencv/opencv-3.4.5/build
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ubuntu/opencv/opencv-3.4.5/build
5)编译
sudo make -j8 #j后面的数字代表用几个线程编译,此为8线程
然后大概率会出现以下错误
fatal error: boostdesc_bgm.i: vgg_generated_80. vgg_generated_120.i没有那个文件或目录
这个问题是一堆文件的缺失,包括
boostdesc_bgm.i
boostdesc_bgm_bi.i
boostdesc_bgm_hd.i
boostdesc_lbgm.i
boostdesc_binboost_064.i
boostdesc_binboost_128.i
boostdesc_binboost_256.i
vgg_generated_120.i
vgg_generated_64.i
vgg_generated_80.i
vgg_generated_48.i
所以只需要将上述.i文件放置到/home/ubuntu/opencv/opencv-3.4.5opencv_contrib-3.4.5/modules/xfeatures2d/src/ 路径下即可。
提示:文件已在下载包中提供,也可通过链接下载
链接:https://pan.baidu.com/s/1XDupTIOHSE-YDuMbhsGJkg
提取码:hell
这些问题解决之后,还会遇见最后一个问题,就是opencv的stitiching模块中对xfeatures2d的调用地址问题。
opencv安装opencv_contrib出现无法打开包括文件: “opencv2/xfeatures2d/cuda.hpp”的解决方法:
这里比较简单的就是直接改掉头文件
解决办法: 在CMakeLists.txt增加以下内容:
INCLUDE_DIRECTORIES("/home/ubuntu/opencv/opencv-3.4.5/opencv_contrib-3.4.5/modules/xfeatures2d/include")
CMakeLists.txt文件所在路径为: <你的路径>/opencv-3.4.5/modules/stitching/ CMakeLists.txt
解决完这些错误,可以继续
sudo make -j8
出现以下信息表明编译成功
[ 99%] Building CXX object modules/stitching/CMakeFiles/opencv_stitching.dir/src/seam_finders.cpp.o
[ 99%] Building CXX object modules/stitching/CMakeFiles/opencv_stitching.dir/src/stitcher.cpp.o
[ 99%] Building CXX object modules/stitching/CMakeFiles/opencv_stitching.dir/src/timelapsers.cpp.o
[ 99%] Building CXX object modules/stitching/CMakeFiles/opencv_stitching.dir/src/util.cpp.o
[ 99%] Building CXX object modules/stitching/CMakeFiles/opencv_stitching.dir/src/warpers.cpp.o
[ 99%] Linking CXX executable ../../bin/opencv_perf_ximgproc
[ 99%] Building CXX object modules/stitching/CMakeFiles/opencv_stitching.dir/src/warpers_cuda.cpp.o
[ 99%] Linking CXX executable ../../bin/opencv_test_tracking
[ 99%] Built target opencv_perf_ximgproc
[ 99%] Building CXX object modules/stitching/CMakeFiles/opencv_stitching.dir/opencl_kernels_stitching.cpp.o
[ 99%] Built target opencv_test_tracking
[ 99%] Linking CXX executable ../../bin/opencv_test_calib3d
[ 99%] Linking CXX executable ../../bin/opencv_perf_xfeatures2d
[ 99%] Built target opencv_test_calib3d
[ 99%] Built target opencv_perf_xfeatures2d
[ 99%] Linking CXX executable ../../bin/opencv_test_xfeatures2d
[ 99%] Built target opencv_test_xfeatures2d
[100%] Linking CXX shared library ../../lib/libopencv_stitching.so
[100%] Built target opencv_stitching
6)安装opencv
sudo make install
7)配置环境变量
打开文件
sudo gedit /etc/ld.so.conf.d/opencv.conf
在空白文档中添加
/usr/local/lib
接下来配置库
sudo ldconfig
更改环境变量
sudo gedit /etc/bash.bashrc
在文件的最后添加
export PKG_CONFIG_PATH=/usr/local/opencv/lib/pkgconfig
export LD_LIBRARY_PATH=/usr/local/opencv/lib
使环境失效
source /etc/bash.bashrc
使用qt ,这个比较简单,也方便
如图,需要注意以下pro文件的写法。
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
INCLUDEPATH += /usr/local/opencv/include
/usr/local/opencv/include/opencv
/usr/local/opencv/include/opencv2
LIBS += /usr/local/lib/*.so.*
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
using namespace cv;
using namespace std;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
VideoCapture cap(0);
while (1) {
Mat frame;
cap>>frame;
imshow("1",frame);
if(waitKey(1)=='q')
break;
}
}
MainWindow::~MainWindow()
{
delete ui;
}