opencv应用程序移植到hi3559板卡
opencv在hi3559平台的移植网络上有很多文章,此处不做赘述。很多新手将opencv使用交叉编译工具编译好后,不知道怎么通过交叉编译将opencv应用程序正常编译,且在板卡上运行,本文章就是为新手做个指引,高手可以忽略。
应用程序
本人在hi3559上移植了opencv3.4.1版本;此处以柱面投影算法作为例子:
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <math.h>
// remapping an image by creating cylind effects
void cylind_projection(const cv::Mat &image, cv::Mat &result) {
float angle = CV_PI/4;
float f = image.rows / (tan(angle/2) * 2);
// the map functions
cv::Mat srcX(image.rows,image.cols,CV_32F); // x-map
cv::Mat srcY(image.rows,image.cols,CV_32F); // y-map
// creating the mapping
for (int i=0; i<image.rows; i++) {
for (int j=0; j<image.cols; j++) {
srcX.at<float>(i,j)= f * tan(j/f - angle/2) + image.cols/2;
srcY.at<float>(i,j)= (i - image.rows/2) / cos(j/f - angle/2) + image.rows/2;
}
}
// applying the mapping
cv::remap(image, // source image
result, // destination image
srcX, // x map
srcY, // y map
cv::INTER_LINEAR); // interpolation method
}
int main()
{
// open image
cv::Mat image= cv::imread("./boldt.jpg",0);
if(image.data == NULL)
{
printf(“imread failed\n”);
return -1;
}
// remap image
cv::Mat result;
cylind_projection(image,result);
cv::imwrite("cylind_boldt.jpg", result);
return 0;
}
较为关键的是Makefile,因为当前的opencv程序是c++版本,所以需要Makefile稍作修改:
CURDIR=$(shell cd ./; pwd)
TARGET = opencv-test
CROSS_COMPILE = aarch64-himix100-linux-
PPATH = $(CURDIR)
INCLUDE = -I $(CURDIR)/include/ -I $(CURDIR)
LIB += $(PPATH)/lib
LIBS += -L$(LIB) -ldl -lpthread -lm -pthread -lopencv_imgproc -lopencv_core -lopencv_imgcodecs
CFLG = -g -O0 -Wall -lrt -lstdc++ -Wstrict-aliasing
src :=$(wildcard $(shell ls *.cpp))
dir := $(notdir $(src))
CC=$(CROSS_COMPILE)gcc
objs := $(patsubst %cpp, %o, $(src))
(
T
A
R
G
E
T
)
:
(TARGET):
(TARGET):(objs)
$(CC) $(INCLUDE) $(CFLG) -o $@ $^ $(LIBS)
$(MAKE) --no-print-directory post-build
%.o:%.cpp
$(CC) $(INCLUDE) $(CFLG) -c -o $@ $< $(LIBS)
post-build:
cp $(TARGET) /home/nfs
clean:
rm -f $(TARGET) *.all .o $(PROJECT_SRC_DIR)/.o
将交叉编译好的库文件放到lib目录下,头文件放到include目录下,进行编译,编译出来的程序可以在海思板卡上直接运行。