注释:本简称适用于可以连接网络设备的ARM开发平台(A72\A53)及Ubuntu系统,对于无法连接网络(A9)请关注博主的另外一篇关于交叉编译安装QT和opencv的博客
安装OpenCV-3.2.0
步骤1:更新套件
sudo apt -y update
sudo apt -y upgrade
步骤2:安装OS库
sudo apt-get install libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libdc1394-22-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavutil-dev libavfilter-dev libavresample-dev libgstreamer0.10-dev qt5-default qtcreator libatlas-base-dev libmp3lame-dev x264 v4l-utils libprotobuf-dev protobuf-compiler libgoogle-glog-dev libgflags-dev libgphoto2-dev libhdf5-dev doxygen checkinstall gfortran libjpeg8-dev libpng12-dev libtiff-dev libxine2-dev libv4l-dev
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python3-pyqt5 qtcreator qt5-default
cd /usr/include/linux
sudo ln -s -f ../libv4l1-videodev.h videodev.h
步骤3:安装Python库
apt -y install python3-dev python3-pip
sudo -H pip3 install -U pip numpy
apt -y install python3-testresources
sudo apt-get install python3.5-dev python3-numpy python-numpy libtbb2 libtbb-dev
步骤4:更新工具:
pip install --upgrade setuptools
pip install --upgrade pip
python3 -m pip install --upgrade pip
步骤5:只能装python依赖库:
pip install wheel numpy scipy -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install matplotlib scikit-image ipython dlib -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install scikit-learn==0.20rc1 -i https://pypi.tuna.tsinghua.edu.cn/simple
步骤6:编译并使用contrib模块安装OpenCV
将相关文件opencv_contrib-3.2.0和opencv-3.2.0拷贝到Ubuntu下并解压<相关资源可到博主的上传资源中下载:https://download.csdn.net/download/weixin_41099962/12334975>
- 获取资源并解压资源
root@ubuntus:~/opencv# ls
opencv-3.2.0.zip opencv_contrib-3.2.0.zip
root@ubuntus:~/opencv# unzip opencv-3.2.0.zip
root@ubuntus:~/opencv# unzip opencv_contrib-3.2.0.zip
root@ubuntus:~/opencv# cd opencv-3.2.0
root@ubuntus:~/opencv# mkdir build
root@ubuntus:~/opencv# cd build
- 配置选项:
cmake -D BUILD_TIFF=ON -D WITH_CUDA=OFF \
-D ENABLE_AVX=OFF -D WITH_OPENGL=ON \
-D WITH_OPENCL=OFF -D WITH_IPP=OFF \
-D WITH_TBB=ON -D BUILD_TBB=ON \
-D WITH_EIGEN=OFF -D WITH_V4L=ON \
-D WITH_VTK=OFF -D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF -D WITH_GTK=ON\
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D WITH_QT=ON -D WITH_GTK=ON \
-D OPENCV_PYTHON3_INSTALL_PATH=/usr/local/lib/python3.5/dist-packages/ \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.2.0/modules \
-D BUILD_EXAMPLES=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON ..
在配置过程中无法正常配置:
在配置过程中由于资源下载问题会出现tbb44_20160128oss_src.tgz、protobuf-cpp-3.1.0.tar.gz、*.i等资源无法下载,开发者需要等待出现错误提示后才能把相应的安装源拷贝到对应目录
将文件tbb44_20160128oss_src.tgz拷贝到opencv-3.2.0/3rdparty/tbb目录下
将protobuf-cpp-3.1.0.tar.gz拷贝到opencv_contrib-3.2.0/modules/dnn/.download/bd5e3eed635a8d32e2b99658633815ef/v3.1.0目录下
将*.i文件拷贝到opencv_contrib-3.2.0/modules/xfeatures2d/src
- 编译并安装:
make -j8
make install
- 配置环境变量
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
Package opencv was not found in the pkg-config search path. Perhaps you should add the directory containing `opencv.pc' to the PKG_CONFIG_PATH environment variable No package 'opencv' found
cd /usr/local/lib
sudo mkdir pkgconfig
cd pkgconfig
sudo touch opencv.pc
然后在opencv.pc中添加以下信息,注意这些信息需要与自己安装opencv时的库路径对应:
prefix=/usr/local/opencv3
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib
Name: opencv
Description: The opencv library
Version:3.2.0
Cflags: -I${includedir}/opencv2
Libs: -L${libdir} -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
~
保存退出,然后将文件导入到环境变量:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
至此就配置好opencv.pc啦
再执行 pkg-config --cflags --libs opencv时输出结果如下:
-I/usr/local/opencv3/include/opencv2
-L/usr/local/opencv3/lib -lopencv_shape -lopencv_stitching
-lopencv_objdetect -lopencv_superres -lopencv_videostab
-lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio
-lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml
-lopencv_imgproc -lopencv_flann -lopencv_core
添加环境变量:
1、打开路径
cd /etc/ld.so.conf.d
2、创建文件
touch OpenCV.conf
3、在OpenCV.conf中写入:
/usr/local/opencv3/lib
注: 注意:OpenCV.conf中的路径一定要是自己opencv的路径一致
默认为/usr/local/lib,即lib*.so库文件所在的路径。
4、在终端执行命令
sudo ldconfig
创建一个文件/etc/ld.so.conf.d/opencv.conf并将OpenCV库存储的文件夹的路径写入其中。
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/opencv3/lib