您的位置:首页 > 运维架构

基于Hi3559A ARM64位嵌入式平台的OpenCV2.4.9+ffmpeg2.0.7移植及应用

2017-09-07 14:32 801 查看
前言:
     按照上一篇文章的方式移植后发现可以正常操作图片文件,不能正常获取视频文件,后来发现是因为OpenCV中的视频API接口依赖于FFmpeg的支持,因此必须得移植合适版本的FFmpeg库,最新的不一定好,能用即可!
在嵌入式设备上做视频图像处理还是需要依赖强大的工具才行,虽然移植的过程比较痛苦,但是成功,后面的开发任务就轻松很多,事半功倍,可以将宝贵的时间放在更High-Level的应用上。不定期更新具体操作图片,若有疑问,请留言交流!

1、准备文件
     ffmpeg-2.0.7.tar.bz2
     opencv-2.4.9.zip

2、FFmpeg移植
     1、解压
     2、编译配置
           ./configure --prefix=/home/bug1989/opencv/ffmpeg_install --cross-prefix= aarch64-hisiv610-linux- --enable-cross-compile --arch=arm64 --target-os=linux --cc= aarch64-hisiv610-linux- gcc      
           此处会有一个警告,说xxx-pkg-xxx找不到,无视即可,不要被下着了,是warning不是error! 
     3、make 
     4、make install
     5、在prefix中寻找你想要的

3、OpenCV移植
     1、解压
     2、添加之前移植安装完成的FFmpeg库的pkgconfig信息
          export PKG_CONFIG_LIBDIR=/home/bug1989/opencv/ffmpeg_install/lib/
          export PKG_CONFIG_PATH=/home/bug1989/opencv/ffmpeg_install/lib/pkgconfig/
     3、解压后的根路径下运行cmake-gui,配置信息如下,注意“Linux”大写,“Target include”选择only,配置opencv源码路径,配置build路径,
          点击configuration由于不支持CUDA配置报错,手动删除CUDA选项,删除OpenCL相关,删除V4L2相关,禁用动态库,禁用测试用例,设置make install的
          安装路径,根据实际需求微调各种选项^_^!,确保ffmpeg的配置选项开启后点击generation,
     4、进入build文件夹
          make
          make install
     5、在prefix中寻找你想要的

4、测试用例
VideoSample.cpp

#include <opencv2/opencv.hpp>
#include <iostream>

#include <stdio.h>
#include <sys/time.h>
#include <ctype.h>

#define _MAX_FNAME  128

using namespace cv;
using namespace std;

static unsigned long get_current_time(void)
{
struct timeval tv;

gettimeofday(&tv, NULL);

return (tv.tv_sec*1000000 + tv.tv_usec);
}

int main()
{
int frmCnt = 0;
int time_0,time_1;
int width, height;
double dt;

VideoCapture capture("a.mkv");

width  = (int)capture.get(CV_CAP_PROP_FRAME_WIDTH);
height = (int)capture.get(CV_CAP_PROP_FRAME_HEIGHT);

printf("Video size:%d %d\n", width, height);

cv::VideoWriter record;
record.open("record.avi", CV_FOURCC('X','V','I','D'), capture.get(CV_CAP_PROP_FPS), Size(534, 300));

time_0 = get_current_time();
while(1)
{
Mat frame, dispImg;
capture >> frame;
if(frame.empty())
{
break;
}

/* Resize */
cv::resize(frame, dispImg, cv::Size(534,300));

/* Save with picture */
imwrite("resize.jpg", dispImg);

/* Save with video */
char text[_MAX_FNAME] = {'\0'};
sprintf(text, "FrmCnt: %d", frmCnt);
cv::putText(dispImg, string(text), Point(5,15), FONT_HERSHEY_COMPLEX_SMALL, 1, CV_RGB(255,0,0));
cv::rectangle(dispImg, cvPoint(100, 100), cvPoint(150, 150), cvScalar(0, 255, 255), 1, 4, 0 );
frmCnt++;

//save frame and encode
record.write(dispImg);

if(frmCnt == 50)
{
break;
}
}
time_1 = get_current_time();
dt = (time_1 - time_0)/1000000.0;
printf("video end, frmCnt = %d, Consuming time %.3f s, %.1f FPS\n", \
frmCnt, dt, frmCnt/dt);

return 0;
}
Makefile
#This makefile is used to opencv 2.4

CC = aarch64-hisiv610-linux-g++
DIR = /home/ubuntu/share/tangqi_code/opencv/opencv_3559_2.4.9

TARGETS := VideoSample_3559

LIBS = 	-lopencv_highgui \
-lopencv_imgproc \
-lopencv_core \
-lopencv_video \
-ldl \
-lIlmImf \
-llibjasper \
-llibjpeg \
-llibpng \
-llibtiff \
-lzlib \
-lavformat \
-lavcodec \
-lavdevice \
-lavfilter \
-lavutil \
-lswscale \
-lpthread \

INCLUDE = -I$(DIR)/include/

LIB_TARGET_DIR = -L$(DIR)/lib \
-L/$(DIR)/share/OpenCV/3rdparty/lib

CPPFLAGS = --static

all:$(TARGETS)

$(TARGETS):VideoSample.cpp
$(CC) $(CPPFLAGS) $(INCLUDE) $^ $(LIB_TARGET_DIR) $(LIBS)  -o $@

clean:
rm -f *.o $(TARGETS)
     其中Makefile中的DIR路径是第3步中编译好的OpenCV的安装路径

5、有时间再补充细节吧,主要是写给自己看的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  arm opencv 64位 ffmpeg 移植