交叉编译OpenCV与OpenCV_contrib到ARM的经验分享

继上次完成NumCpp的移植之后,这些我们来进行OpenCV与OpenCV_contrib的移植。

1. OpenCV、OpenCV_contrib源码下载

GitHub - opencv/opencv: Open Source Computer Vision LibraryOpen Source Computer Vision Library. Contribute to opencv/opencv development by creating an account on GitHub.https://github.com/opencv/opencvhttps://github.com/opencv/opencv_contribhttps://github.com/opencv/opencv_contrib

2. 第三方库的准备

交叉编译OpenCV之前,一般需要libz, libjpeg, libpng, yasm, libx264, libxvid, ffmpeg这些第三方库的编译。

具体的编译过程,我就不多说了,基本上就是检查编译器是否为交叉编译器,然后进行编译即可。这里重点注意ffmpeg,一会儿会提到。

3. 编译opencv与opencv_contrib

编译之前,设置后pkg的路径,输入一下命令

export PKG_CONFIG_PATH=/home/xinzhe/Documents/opencv_depend/lib/pkgconfig:$PKG_CONFIG_PATH

首先从GitHub中clone得到两个文件夹之后,进入到opencv文件夹,进行以下操作

mkdir build
cd build
cmake-gui

之后,弹出cmake-gui的界面。

我们首先设置好source code与build的路径,然后点击configure,然后选择Unix Makefiles里的specify options for cross-compiling,然后进入到新的窗口,填入linux、arm、编译器、依赖库路径,点击finish。

然后configure开始运行。

运行结束后,我们从下面的log里,检查Video I/O:这一项,发现里面的ffmpeg处是NO,也就是没有检测到ffmpeg。这个时候我们需要增加一个值,添加OPENCV_ENABLE_PKG_CONFIG变量,并设其值为true,然后我们再configure,此时发现ffmpeg处为YES。

这样,我们编译出来的opencv能够读视频文件,否则不能够读取成功。

然后我们搜索设置以下内容,这里的selected文件夹中的modules是我从opencv_contrib中的modules下截取的,我只使用了opencv_contrib/modules中的bgsem,因此我把它放到selected/modules里。不把整个opencv_contrib/modules设置为extra_modules是因为,其中的某些库还需要另外的第三方库,我就懒得弄了,如果大家想编译整个opencv_contrib/modules,也是可以的。

OPENCV_EXTRA_MODULES_PATH=/home/xinzhe/Documents/selected/modules

并且设置好CMAKE_INSTALL_PREFIX安装路径之后 ,将以下这些值取消勾选

WITH_TIFF
WITH_GTK
WITH_CUDA
WITH_OPENCL
WITH_OPENEXR
WITH_1394

设置CMAKE_BUILD_TYPE=Release

之后,我们重新点击configure,完成后点击Generate生成Makefile。

关闭cmake-gui,并在build目录下

make
make install

这样我们就得到了opencv与opencv_contrib的交叉编译文件。

4. 交叉编译自己的cpp文件

在自己的cpp文件下,创建Compile.cmake与CMakeLists.txt文件。

其中CMakeLists.txt如下

cmake_minimum_required(VERSION 2.8)

Project(CVWorld)

set(CMAKE_FIND_ROOT_PATH /home/xinzhe/Documents/opencv_v853)
find_package(OpenCV REQUIRED)

##下面是输出信息
message(STATUS "Opnecv library status: ")
message(STATUS "> version: ${OpenCV_VERSION} ")
message(STATUS ">libraries: ${OpenCV_LIBS} ")
message(STATUS "> include: ${OpenCV_INCLUDE_DIRS}  ")


include_directories(${OpenCV_INCLUDE_DIRS}) 


add_executable(VideoHitMan main.cpp)

target_link_libraries(VideoHitMan ${OpenCV_LIBS})

然后,我们创建build文件夹,按以往的操作编译,运行可执行文件,发现缺少一些.so动态库文件。

这个时候,我们将opencv_depend/lib下的所有库文件cp到opencv_v853/lib下,这样就可以编译成功了。

5. 在V853开发板上运行编译得到的程序

得到可执行文件后,将其与opencv_v853 push到板子上,然后输入

export LD_LIBRARY_PATH=/root/opencv_v853/lib:$LD_LIBRARY_PATH

运行程序,发现一大堆库有问题。这个时候就重新建立指向链接就行。

最后就可以运行成功啦。

root@TinaLinux:/# cd root
root@TinaLinux:~# export LD_LIBRARY_PATH=/root/opencv_v853/lib:$LD_LIBRARY_PATH
root@TinaLinux:~# ./VideoHitMan
the video is /root/4.mp4
Height:720 Width:1280
This is the start row :226
This is the end row :712
col_mid = 108
Height:720 Width:1280
This is the start row :226
This is the end row :704
col_mid = 108
Height:720 Width:1280
This is the start row :228
This is the end row :687
col_mid = 112
Height:720 Width:1280
This is the start row :229
This is the end row :672
col_mid = 115
Height:720 Width:1280
This is the start row :230
This is the end row :664
col_mid = 116
Height:720 Width:1280
This is the start row :231
This is the end row :663
col_mid = 116
Height:720 Width:1280
This is the start row :235
This is the end row :663
col_mid = 117
Height:720 Width:1280
This is the start row :238
This is the end row :663
col_mid = 117
Height:720 Width:1280
This is the start row :243
This is the end row :662
col_mid = 118
Height:720 Width:1280
This is the start row :246
This is the end row :661
col_mid = 118
Height:720 Width:1280
This is the start row :250
This is the end row :660
col_mid = 120
Height:720 Width:1280
This is the start row :252
This is the end row :656
col_mid = 121
Height:720 Width:1280
This is the start row :255
This is the end row :650
col_mid = 123
Height:720 Width:1280
This is the start row :256
This is the end row :642
col_mid = 126
Height:720 Width:1280
This is the start row :257
This is the end row :634
col_mid = 128
Height:720 Width:1280
This is the start row :257
This is the end row :621
col_mid = 132
Height:720 Width:1280
This is the start row :256
This is the end row :602
col_mid = 139
Height:720 Width:1280
This is the start row :256
This is the end row :601
col_mid = 138
Height:720 Width:1280
This is the start row :257
This is the end row :599
col_mid = 139
Height:720 Width:1280
This is the start row :258
This is the end row :596
col_mid = 140
Height:720 Width:1280
This is the start row :261
This is the end row :595
col_mid = 142
Height:720 Width:1280
This is the start row :263
This is the end row :595
col_mid = 143
Height:720 Width:1280
This is the start row :266
This is the end row :594
col_mid = 143
Height:720 Width:1280
This is the start row :269
This is the end row :594
col_mid = 145
Height:720 Width:1280
This is the start row :271
This is the end row :594
col_mid = 145
Height:720 Width:1280
This is the start row :273
This is the end row :593
col_mid = 146
location = 73216
Generated binary tensor from silhouette
root@TinaLinux:~# 

今天就到这里啦,感谢看到的每个小伙伴!

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
1. 安装交叉编译工具链 首先需要安装交叉编译工具链,可以根据目标平台的架构选择相应的工具链。以ARM架构为例,可以使用ARM的官方工具链或者使用Ubuntu提供的工具链: ```bash sudo apt-get install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf ``` 2. 下载并编译opencv 下载opencv源码,解压后进入源码目录,执行以下命令: ```bash mkdir build cd build cmake -DCMAKE_TOOLCHAIN_FILE=../platforms/linux/arm-gnueabi.toolchain.cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules -DBUILD_SHARED_LIBS=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/arm-linux-gnueabihf .. make -j4 ``` 其中,`-DCMAKE_TOOLCHAIN_FILE`指定了交叉编译工具链,`-DOPENCV_EXTRA_MODULES_PATH`指定了opencv_contrib的路径,`-DBUILD_SHARED_LIBS=OFF`表示编译静态库,`-DBUILD_TESTS=OFF`、`-DBUILD_PERF_TESTS=OFF`、`-DBUILD_EXAMPLES=OFF`表示不编译测试和示例程序,`-DCMAKE_BUILD_TYPE=RELEASE`表示编译优化,`-DCMAKE_INSTALL_PREFIX`指定了安装目录。 编译完成后,执行以下命令安装opencv: ```bash sudo make install ``` 3. 链接静态库 在编译自己的程序时,需要链接opencv的静态库。以CMake为例,可以在`CMakeLists.txt`中添加以下代码: ```cmake set(OpenCV_STATIC ON) find_package(OpenCV REQUIRED) target_link_libraries(your_target ${OpenCV_LIBS}) ``` 其中,`set(OpenCV_STATIC ON)`指定链接静态库,`find_package(OpenCV REQUIRED)`查找opencv库,`target_link_libraries(your_target ${OpenCV_LIBS})`链接静态库。需要注意的是,交叉编译时需要指定正确的库路径和头文件路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

where_are_u

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值