环境+工具:
编译器 : aarch64-linux-gnu-g++ aarch64-linux-gnu-gcc
虚拟机 :ubuntu16.04
cmake-gui
cd /usr/local
sudo mkdir opencv
将源码opencv3.4.3 拷贝到此目录下,并新建两个opencv_config, opencv-arm-install文件夹,
cmake-gui
点击configure
Next 如下图填写,compilers,c ,c++选项填写自己要用的交叉编译工具链。Target Root为检查编译工具链所在的路径,根据你的填写,Finish
选择你要安装的路径;我这里是安装在刚刚新建的opencv-arm-install下。以防止将之前在ubuntu下的opencv冲掉,这里我只改了这一个选项,网上有人将动态链接库的选项也去掉了,但是我生成静态链接库执行demo出现问题,所以又选回来了,生成动态链接库,反正这里我只改了这一个地方。
然后点击configure,Generate。
进入opencv_config文件夹下,
执行
make
make install
完成。
中间make的时候我遇到过两个问题:
1.在进度到40%多的时候 opencv-3.4.3/3rdparty/libpng/pngstruct.h:30:18: fatal error: zlib.h: No such file or directory
解决方法
在 opencv-3.4.3/CMakeLists.txt,在514行增加以上代码: ocv_include_directories(./3rdparty/zlib/)下面一行添加,其它类似错误一样的解决方法
......
ocv_include_directories(${OPENCV_CONFIG_FILE_INCLUDE_DIR})
ocv_include_directories(./3rdparty/zlib/)
......
重新cmake一遍,然后在进行make
2.
[ 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
解决方法:
如下修改:~/opencv-3.4.3/3rdparty/libpng/pngpriv.h文件。
将128行注释掉,添加129行内容。
128 /*# if (defined(__ARM_NEON__) || defined(__ARM_NEON)) && \*/
129 # if defined(PNG_ARM_NEON) && (defined(__ARM_NEON__) || defined(__ARM_NEON)) && \
解决了这两个问题之后就make通过了
之后将编译完成的lib include文件夹拷贝到开发板上,网上说拷贝到开发板的/usr/local,但是我拷贝到这个文件夹下执行demo的时候会出现error while loading shared libraries: libopencv_highgui.so.3.4: cannot open shared object file: No such file or directory这个错误,后将lib文件里的.so文件拷贝到lib64文件夹下demo可正常执行。
编译一个调用摄像头并且录像的demo:
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
int main()
{
VideoCapture capture(6);//如果是笔记本,0打开的是自带的摄像头,1 打开外接的相机
capture.set(3,640);
capture.set(4,480);
Mat img;
VideoWriter vw; //新建一个多媒体文件
int fps = capture.get(CAP_PROP_FPS); //获取摄像头的帧率
if (fps <= 0)fps = 16;
//设置视频的格式
// vw.open("Video.avi", VideoWriter::fourcc('h', '2', '6', '4'), fps, Size(640, 480));
vw.open("1.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, Size(640, 480));
if (!capture.isOpened()) //判断摄像头是否打开
{
cout << "open video faild";
getchar();
}
cout << "open video success" << endl;
if (!vw.isOpened()) //判断视频文件是否创建
{
cout << "open vw faild" << endl;
}
cout << "open vw success" << endl;
while (1)
{
capture.read(img); //读取视频帧
if (img.empty())
break;
vw.write(img); //将视频帧写入文件
}
return 0;
}
将lib和include文件拷贝到交叉编译的lib和include的文件夹下。并执行以下命令。
aarch64-linux-gnu-g++ camera.cpp -o camera -I/usr/local/opencv/opencv-arm/include -L/usr/local/opencv/opencv-arm/lib -lopencv_highgui -lopencv_core -lopencv_imgproc -lpthread -lrt -ldl -lopencv_imgcodecs -lopencv_videoio
会出现以下警告:
/usr/local/linaro-aarch64-2017.11-gcc7.2/bin/…/lib/gcc/aarch64-linux-gnu/7.2.1/…/…/…/…/aarch64-linux-gnu/bin/ld: warning: …/…/lib/libopencv_imgproc.so, needed by /home/zy/cv22_1.0_rc/cv22_auto_linux_sdk/ambarella/prebuild/third-party/armv8-a/opencv24/usr/lib/libopencv_highgui.so, not found (try using -rpath or -rpath-link)
/usr/local/linaro-aarch64-2017.11-gcc7.2/bin/…/lib/gcc/aarch64-linux-gnu/7.2.1/…/…/…/…/aarch64-linux-gnu/bin/ld: warning: …/…/lib/libopencv_core.so, needed by /home/zy/cv22_1.0_rc/cv22_auto_linux_sdk/ambarella/prebuild/third-party/armv8-a/opencv24/usr/lib/libopencv_highgui.so, not found (try using -rpath or -rpath-link)
这个警告不影响目标代码的生成,但想去掉的话,其实只需要把找不到的库放到路径为/usr/local/arm/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/aarch64-linux-gnu/libc/lib/目录下即可。