ubuntu版本号: Ubuntu 18.04.5 LTS
opencv版本号:opencv-3.4.1
opencv3.4.1下载链接: opencv-3.4.1相关库-C++文档类资源-CSDN下载
1. 编译opencv3.4.1
在opencv3.4.1下创建build,并创建arm-himix410-linux.cmake文件;粘贴如下内容;
set( CMAKE_SYSTEM_NAME Linux )
set( CMAKE_SYSTEM_PROCESSOR arm )
set( CMAKE_C_COMPILER arm-himix410-linux-gcc )
set( CMAKE_CXX_COMPILER arm-himix410-linux-g++ )
具体工具链名字自行更改;
2. 编译
cmake -D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=../output \
-D CMAKE_C_COMPILER=arm-himix410-linux-gcc \
-D BUILD_SHARED_LIBS=ON \
-D BUILD_ZLIB=ON \
-D WITH_ITT=OFF \
-D BUILD_opencv_highgui=ON \
-D ZLIB_INCLUDE_DIR=../3rdparty/zlib ../
其中: BUILD_SHARED_LIBS可控制是否生成动态库; BUILD_opencv_highgui控制是否生成opencv-highgui.so库;
make -j8
此过程可能会遇到一些错误,各个虚拟机编译结果不尽相同;
3. 调用接口编译时出错;
错误1:undefined reference to `__itt_api_version_ptr__3_0'
解决: -D WITH_ITT=OFF \
错误2:opencv undefined reference to `jpeg_CreateCompress'
原因:缺少jpeg库; 并需要 -ljpeg 链接;
编译: ./configure CC=arm-himix410-linux-gcc --prefix=/usr/local/jpegsrc.v8c/install --host=arm-linux
错误3: Unable to locate package libjasper-dev, 就是和jasper库相关的, 网上说apt-get install libjasper-dev类似的;甚至下载了jasper开源库交叉编译也不行;
解决: 加上LIBS += -L/usr/local/opencv-3.4.1/output/share/OpenCV/3rdparty/lib
分析: 在编译生成的output库路径下有一些库!
错误4:undefined reference to `TIFFWriteDirectory'
原因: 和tiff库相关, 并链接 -ltiff; 虽然上面 output/share/OpenCV/3rdparty/lib路径下有,但是还是报错,只有交叉编译3方库了;
编译tiff-4.0.4库,依赖与zlib和jpeg库;
./configure CC=arm-himix410-linux-gcc \
CXX=arm-himix410-linux-g++ \
--prefix=$(pwd)/install \
--host=arm-linux \
--with-zlib-include-dir=/usr/local/zlib-1.2.3/install/include \
--with-zlib-lib-dir=/usr/local/zlib-1.2.3/install/lib \
--with-jpeg-include-dir=/usr/local/jpeg-8c/install/include \
--with-jpeg-lib-dir=/usr/local/jpeg-8c/install/lib
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/tiff-4.4.0/install/lib/pkgconfig
还有其他错误,并未完全记录...
4. Qt调用接口环境搭建
INCLUDEPATH += $$(PWD)/include \
$$(PWD)/include/opencv2 \
$$(PWD)/include/opencv
INCLUDEPATH += /usr/local/jpeg-8c/install/include
INCLUDEPATH += /usr/local/tiff-4.4.0/install/include
INCLUDEPATH += /usr/local/opencv-3.4.1/output/include
LIBS += -L$$(PWD)/lib #包含了海思相关库
LIBS += -L/usr/local/opencv-3.4.1/output/share/OpenCV/3rdparty/lib
LIBS += -L/usr/local/jpeg-8c/install/lib
LIBS += -L/usr/local/tiff-4.4.0/install/lib
LIBS += -L/usr/local/opencv-3.4.1/output/lib
LIBS += -lmpi -live -lupvqe -ldnvqe -lVoiceEngine -lsecurec -fopenmp -lQtNetwork -lQtCore -lpthread -ldl #hisi
LIBS += -lopencv_ml \
-lopencv_objdetect \
-lopencv_shape \
-lopencv_stitching \
-lopencv_superres \
-lopencv_videostab \
-lopencv_calib3d \
-lopencv_videoio \
-lopencv_imgcodecs \
-lopencv_features2d \
-lopencv_video \
-lopencv_photo \
-lopencv_imgproc \
-lopencv_flann \
-lopencv_core
LIBS += -llibjasper -llibtiff -ltiff -lzlib -ljpeg -lavcodec -lavformat -lavutil -lswscale -lm -lrt
编译过程参考了很多网上博客,就不贴了,记录一下编译过程;
5. 总结:
编译不过一个可以换其他版本的opencv库,可从低版本换到高版本等;
编译过仅仅是第一步, 运行时出错也可能需要添加些东西再从新编译;
动态库需要自己部署带板子/lib下并软链接;
6. 补充
补充1:下面两个警告,虽然不报错,但是很不爽;
(a) warning: libopencv_highgui.so.3.4, needed by /usr/local/opencv-3.4.1/output/lib/libopencv_stitching.so, not found (try using -rpath or -rpath-link)
(b) warning: libz.so.1, needed by /usr/local/tiff-4.4.0/install/lib/libtiff.so, not found (try using -rpath or -rpath-link)
翻译一下就是:libopencv_highgui.so.3.4, 被xxlibopencv_stitching.so需要,not found ;明明链接了库啊...
-lopencv_highgui \ -lopencv_stitching在lopencv_stitching前面添加上lopencv_highgui就ok了,或许是依赖的原因把;
同理: libtiff.so需要libz.so.1;
交叉编译了三方库:zlib-1.2.3
LIBS += -L/usr/local/zlib-1.2.3/install/lib
同时在ltiff前链接-lz:
LIBS += -lz -llibjasper -llibtiff -ltiff
7. 开发板上错误
错误: error while loading shared libraries: libopencv_imgproc.so.3.4: cannot open shared object file
首先我已经确保/lib下以及存在libopencv_imgproc.so.3.4.1库,并用ln -s 软链接了libopencv_imgproc.so.3.4; 执行demo程序时报上述错误;
增加: export LD_LIBRARY_PATH=/lib:$LD_LIBRARY_PATH
导出开发板上的库路径到LD_LIBRARY_PATH;