主要参考:https://blog.csdn.net/zhenglie110/article/details/83009675
版本说明:
ubuntu 16.04
opencv 3.4.1
cmake 3.5.1
1.下载源码
下载opencv3.4.1 Linux版源码,
https://opencv.org/opencv-3-4-1.html
2.通过下面的sources,直接下载。
3.安装cmake与cake-gui
sudo apt-get install cmake
sudo apt-get install cmake-gui
3.2使用cmake-gui 进行配置(ps 看到好像有直接使用cmake命令进行配置的,不知道我为什么失败了,后期再查查看!)
运行cmake-gui,增加参数
CMAKE_EXE_LINKER_FLAGS -lpthread -lrt -ldl
CMAKE_EXE_LINKER_FLAGS_DEBUG -lpthread -lrt -ldl
支持C11,选中支持
ENABLE_CXX11 YES
设置编译安装目录,可修改,也可默认安装
CMAKE_INSTALL_PREFIX /usr/local
4.编译和安装
make -j8
make install -j8
或者: sudo make install -j8 (ps: /usr/local/ 需要给予root权限)
4.1出现编译错误:
opencv-3.4.1/3rdparty/protobuf/src/google/protobuf/stubs/common.cc:52:2: error: #error "No suitable threading library available."
解决办法:在common.cc 增加#define HAVE_PTHREAD
#define HAVE_PTHREAD //新增
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN // We only need minimal includes
#include <windows.h>
#define snprintf _snprintf // see comment in strutil.cc
#elif defined(HAVE_PTHREAD)
#include <pthread.h>
#else
#error "No suitable threading library available."
#endif
#if defined(__ANDROID__)
#include <android/log.h>
#endif
4.2继续make,出现错误:
opencv-3.4.1/3rdparty/libpng/pngstruct.h:30:18: fatal error: zlib.h: No such file or directory
~/opencv-3.4.1/CMakeLists.txt,在514行增加以上代码: ocv_include_directories(./3rdparty/zlib/)
......
ocv_include_directories(${OPENCV_CONFIG_FILE_INCLUDE_DIR})
ocv_include_directories(./3rdparty/zlib/)
......
4.3继续make,出现错误:
[ 43%] Building CXX object apps/visualisation/CMakeFiles/opencv_visualisation.dir/opencv_visualisation.cpp.obj
[ 43%] Linking CXX executable ../../bin/opencv_annotation
../../lib/libopencv_core.so: undefined reference to `pthread_mutexattr_destroy'
../../lib/libopencv_imgcodecs.so: undefined reference to `pthread_create'
../../lib/libopencv_core.so: undefined reference to `pthread_key_create'
../../lib/libopencv_imgcodecs.so: undefined reference to `png_init_filter_functions_neon'
../../lib/libopencv_core.so: undefined reference to `pthread_getspecific'
../../lib/libopencv_core.so: undefined reference to `dlopen'
../../lib/libopencv_core.so: undefined reference to `pthread_mutex_trylock'
../../lib/libopencv_core.so: undefined reference to `pthread_key_delete'
../../lib/libopencv_core.so: undefined reference to `dlclose'
../../lib/libopencv_core.so: undefined reference to `dlsym'
../../lib/libopencv_core.so: undefined reference to `pthread_mutexattr_settype'
../../lib/libopencv_imgcodecs.so: undefined reference to `pthread_join'
../../lib/libopencv_core.so: undefined reference to `pthread_setspecific'
../../lib/libopencv_core.so: undefined reference to `pthread_mutexattr_init'
collect2: error: ld returned 1 exit status
解决办法:CMakeCache.txt上约273行,CMAKE_EXE_LINKER_FLAGS和CMAKE_EXE_LINKER_FLAGS_DEBUG,
添加: -lpthread -lrt -ldl
......
//Flags used by the linker.
CMAKE_EXE_LINKER_FLAGS:STRING=-lpthread -lrt -ldl
//Flags used by the linker during debug builds.
CMAKE_EXE_LINKER_FLAGS_DEBUG:STRING=-lpthread -lrt -ldl
......
(PS:前面设置cmake-gui,配置失败了)
4.4继续make,出现错误:
[ 43%] Linking CXX executable ../../bin/opencv_annotation
../../lib/libopencv_imgcodecs.so: undefined reference to `png_init_filter_functions_neon'
collect2: error: ld returned 1 exit status
apps/annotation/CMakeFiles/opencv_annotation.dir/build.make:99: recipe for target 'bin/opencv_annotation' failed
make[2]: *** [bin/opencv_annotation] Error 1
CMakeFiles/Makefile2:4431: recipe for target 'apps/annotation/CMakeFiles/opencv_annotation.dir/all' failed
make[1]: *** [apps/annotation/CMakeFiles/opencv_annotation.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
解决如下:~/opencv-3.4.1/3rdparty/libpng/pngpriv.h
128 /*# if (defined(__ARM_NEON__) || defined(__ARM_NEON)) && \*/
129 # if defined(PNG_ARM_NEON) && (defined(__ARM_NEON__) || defined(__ARM_NEON)) && \
5.copy lib 和 include到工具链下
sudo cp -rv opencv/output/lib/* /opt/hisi-linux/x86-arm/aarch64-himix100-linux/aarch64-linux-gnu/lib/
sudo cp -rv opencv/output/lincude/* /opt/hisi-linux/x86-arm/aarch64-himix100-linux/include/
(ps: 理论应该放到/opt/hisi-linux/x86-arm/aarch64-himix100-linux/aarch64-linux-gnu/include/,后面有时间再试下!)
6. 编译face.cpp
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/reboot.h>
#include <sys/time.h>
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "opencv2/highgui.hpp"
using namespace cv;
using namespace std;
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */
/******************for time mesurement*************************/
struct timeval tpstart,tpend;
unsigned long timeuses;
void timeRec()
{
gettimeofday(&tpstart,0);
}
int timeRep()
{
gettimeofday(&tpend,0);
timeuses=(tpend.tv_sec-tpstart.tv_sec)*1000000+tpend.tv_usec-tpstart.tv_usec;
printf("use time: %uus\n",timeuses);
return timeuses;
}
/********************end**************************************/
int main(int argc, char* argv[])
{
IplImage* img = NULL;
IplImage* cutImg = NULL;
CvMemStorage* storage = cvCreateMemStorage(0);
CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad("/root/haarcascade_frontalface_alt2.xml", 0, 0, 0);
CvSeq* faces;
Mat dest;
img = cvLoadImage(argv[1], 0);
timeRec();
faces = cvHaarDetectObjects(img, cascade, storage, 1.2, 2, 0, cvSize(25,25) );
timeRep();
if (faces->total == 0){
printf("no face!\n");
}
cvSetImageROI(img, *((CvRect*)cvGetSeqElem( faces, 0)));
//cvSaveImage("face.bmp", img);
dest = cvarrToMat(img);
imwrite("face.bmp", dest);
cvResetImageROI(img);
printf("face detected! in face.bmp!\n");
}
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
6.1编译命令如下:
aarch64-himix100-linux-g++ face.cpp -o face -I/usr/local/include -L/usr/local/lib -lopencv_highgui -lopencv_core -lopencv_imgproc -lpthread -lrt -ldl -lopencv_objdetect -lopencv_imgcodecs
会出现如下的警告warning:
chad@ubuntu:~$ aarch64-himix100-linux-g++ face.cpp -o face -I/usr/local/include -L/usr/local/lib -lopencv_highgui -lopencv_core -lopencv_imgproc -lpthread -lrt -ldl -lopencv_objdetect -lopencv_imgcodecs
/opt/hisi-linux/x86-arm/aarch64-himix100-linux/host_bin/../lib/gcc/aarch64-linux-gnu/6.3.0/../../../../aarch64-linux-gnu/bin/ld: warning: ../../lib/libopencv_videoio.so, needed by /usr/local/lib/libopencv_highgui.so, not found (try using -rpath or -rpath-link)
/opt/hisi-linux/x86-arm/aarch64-himix100-linux/host_bin/../lib/gcc/aarch64-linux-gnu/6.3.0/../../../../aarch64-linux-gnu/bin/ld: warning: ../../lib/libopencv_imgcodecs.so, needed by /usr/local/lib/libopencv_highgui.so, not found (try using -rpath or -rpath-link)
/opt/hisi-linux/x86-arm/aarch64-himix100-linux/host_bin/../lib/gcc/aarch64-linux-gnu/6.3.0/../../../../aarch64-linux-gnu/bin/ld: warning: ../../lib/libopencv_imgproc.so, needed by /usr/local/lib/libopencv_highgui.so, not found (try using -rpath or -rpath-link)
/opt/hisi-linux/x86-arm/aarch64-himix100-linux/host_bin/../lib/gcc/aarch64-linux-gnu/6.3.0/../../../../aarch64-linux-gnu/bin/ld: warning: ../../lib/libopencv_core.so, needed by /usr/local/lib/libopencv_highgui.so, not found (try using -rpath or -rpath-link)
实际上已编译完成,因为include和warning不在编译默认的include,lib,故报warning,可设置warning等级即可(后面找时间再确认下)。
注意:需要将/usr/local/lib文件copy到 对应hi3559的目录下:/lib/,
拷贝配置文件 :./opencv-3.4.1/data/haarcascades_cuda/haarcascade_frontalface_alt2.xml 到face.cpp制定的路径,我这里是/root/
copy jpg人脸文件
运行 ./face 即可。
7. 执行 ./face 0_1_1_bk.jpg,结果如下:
~ # ./face 0_1_1_bk.jpg
use time: 1241561us
face detected! in face.bmp!
表明已正常加载库并检测人脸成功,到这里可以说明移植opencv库已基本成功 ^ . ^!
参考其他人的操作,主要目的是记录实际移植中遇到的问题和解决方法。
更新中