注:本篇博客已经弃用,请不要浪费时间阅读,笔者有空会更新最新的移植教程~ 最新文章移植opencv3.2.0含opencv_contrib
一. 移植依赖库
主要需要移植如下几个依赖库:
- zlib-1.2.11
- jpegsrc.v9b
- libpng-1.6.29
- yasm-1.3.0
- x264-snapshot-20170415-2245
- xvidcore_1.3.3.orig
- ffmpeg-2.8.11
下载地址,链接: https://pan.baidu.com/s/1X_Dogbvo_vY4X8zLBRpVcA 提取码: m6f2
编译器版本:
虚拟机:
安装编译器可以参考:Ubuntu20.04安装arm-linux-gcc4.4.3交叉编译器
1. zlib交叉编译
先确定好依赖库安装的路径,笔者是 /home/incipe/opencv_depend
./configure --prefix=/home/incipe/opencv_depend
修改 Makefile
make && make install
2. libjpeg交叉编译
./configure --host=arm-linux-gnueabi CC=arm-linux-gnueabi-gcc --prefix=/home/incipe/opencv_depend --enable-shared --enable-static
make && make install
3. libpng交叉编译
./configure --host=arm-linux --prefix=/home/incipe/opencv_depend --enable-shared --enable-static
make && install
4. yasm交叉编译
CC=arm-linux-gnueabi-gcc ./configure --enable-shared --host=arm-linux-gnueabi --disable-asm --prefix=/home/incipe/opencv_depend
make && make install
5. libx264交叉编译
CC=arm-linux-gnueabi-gcc ./configure --enable-shared --host=arm-linux-gnueabi --disable-asm --prefix=/home/incipe/opencv_depend
make && make install
6. libxvid交叉编译
解压 xvidcore_1.3.3.orig.tar.gz
进入子文件夹。
cd ./build/generic
./configure --prefix=/home/incipe/opencv_depend --host=arm-linux-gnueabihf --disable-assembly
make && make install
7. ffmpe交叉编译
./configure --prefix=/home/incipe/opencv_depend --enable-shared --disable-static --enable-gpl --enable-cross-compile --arch=arm --disable-stripping --target-os=linux --enable-libx264 --enable-libxvid --cc=arm-linux-gnueabi-gcc --enable-swscale --extra-ldflags=-L/home/incipe/opencv_depend/lib --extra-cflags=-I/home/incipe/opencv_depend/include
make && make install
这个过程有点久~
二. 把依赖库拷贝到arm-linux-gnueabi路径下
cp /home/incipe/opencv_depend/include /usr/local/bin/arm-2014.05/arm-none-linux-gnueabi/ -rf
cp /home/incipe/opencv_depend/lib /usr/local/bin/arm-2014.05/arm-none-linux-gnueabi/ -rf
三. 交叉编译安装opencv3.2.0
官网下载地址:https://opencv.org/releases/
解压后进入 opencv3.2.0
路径,新建 build
文件夹,cd build
vim toolchain.cmake
###########user defined#############
set( CMAKE_SYSTEM_NAME Linux )
set( CMAKE_SYSTEM_PROCESSOR arm )
set( CMAKE_C_COMPILER arm-linux-gnueabi-gcc )
set( CMAKE_CXX_COMPILER arm-linux-gnueabi-g++ )
###########user defined#############
set( CMAKE_FIND_ROOT_PATH "/home/incipe/opencv_depend" )
set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER )
set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
######################################
保存退出后,运行如下命令:
cmake -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake ../
配置cmake
如果没有 cmake-gui
先安装:
sudo apt-get install cmake-qt-gui
在 build
目录下运行 cmake-gui
增加源代码路径,编译路径和安装路径。
接下来取消这些选项:
- 去掉 WITH_CUDA
- 去掉 WITH_GTK
- 去掉 WITH_1394
- 去掉 WITH_GSTREAMER
- 去掉 WITH_LIBV4L
- 去掉 WITH_TIFF
- 去掉 BUILD_OPENEXR
- 去掉 WITH_OPENEXR
- 去掉 BUILD_opencv_ocl
- 去掉 WITH_OPENCL(注意区分OPENGL与OPENCL的区别)
- 去掉 WITH FFMPEG
取消之后点击 Configure
之后再点击 Generate
即可。
关闭 cmake-gui
修改CMakeCache.txt
编译安装
sudo make
sudo make install
这个耗时非常长!!!
安装完毕之后在 build
目录下会有 install
文件夹包含 include
和 lib
,就是我们需要的头文件和库文件了。
把 include
下的内容拷贝到 /usr/local/include
目录下。
把 lib
下的内容拷贝到 /usr/local/bin/arm-2014.05/arm-none-linux-gnueabi/lib
即可。
拷贝的时候记得加上 -rf
选项,这样会把 pkgconfig
也一起拷贝,pkgconfig
目的是为了方便编译的。
详解:https://www.cnblogs.com/woshijpf/articles/3840840.html
四. 编译opencv代码
arm-linux-gnueabi-gcc -o puzzle puzzle.o -I /usr/local/include `pkg-config --cflags --libs opencv` -std=c++11
五. 总结与注意
本博客参考 虚拟机交叉编译openCV详细步骤及bug解决详解
因为这篇博客是后面补上的,所以中间具体遇到什么问题不记得了(问题应该不多,之前是因为安装opencv2.4,所以总是安装不上,换了好几个版本,opencv3.2.0终于成功了),如果有安装问题的,欢迎留言。