OpenCV3.4.x移植到嵌入式ARM板
一、交叉编译需要嵌入开发的ARM板环境
二、交叉编译openCV
1、Opencv源码下载
*下载opencv3.4+ 源码
- 解压opencv 源码
2、cmake配置
在源码目录下 打开终端 安装cmake ,运行 cmake-gui
在 cmake界面进行配置
第一步:
第二步:
第三步:
1),下图为网上流传非常广泛的配置方法:在这个配置中填写的目标系统的名称会导致opencv库中V4L无法使用(因为在本次工程中没有使用opencv的V4L驱动库,可以忽略),应该为“Linux”
2),注意编译器的选择为交叉编译器,与下图中的区别。
3),target root 注意事项与下图相同
第四步:
去掉WITH_TIFF WITH_CUDA //不去掉也行,
修改CMAKE_BUILD_TYPE为Release //不选也行,
CMAKE_INSTALL_PREFIX 选择安装目录;
建议使用静态库编译,选择动态库编译需要把动态库拷贝到arm板中,比较麻烦。
第五步:
然后进入编译目录,执行 make 进行编译,编译通过后make install
查看安装目录下的文件,将bin目录下的文件拷贝到开发板/bin目录下
Lib目录下的文件拷贝到开发板/lib目录下
移植过程中还遇到了其他的一些警告信息
比如会遇到
In file included from /home/dyjun/opencv/git_opencv/opencv/3rdparty/libpng/pngpriv.h:904:0,
from /home/dyjun/opencv/git_opencv/opencv/3rdparty/libpng/png.c:14:
/home/dyjun/opencv/git_opencv/opencv/3rdparty/libpng/pngstruct.h:30:18: fatal error: zlib.h: No such file or directory
#include "zlib.h"
^
compilation terminated.
make[2]: *** [3rdparty/libpng/CMakeFiles/libpng.dir/png.c.o] Error 1
解决:
则找到 3rdparty/libpng/CMakeLists.txt
在ocv_include_directories 前面添加 set(ZLIB_INCLUDE_DIRS “/home/dyjun/opencv/git_opencv/opencv/3rdparty/zlib/”)
其它类似的错误也是如此解决
三、Qt如何使用Opencv
Linux安装:
安装libopencv-dev依赖包
sudo apt install libopencv-dev
在终端下面使用命令
pkg-config --cflags opencv
pkg-config --libs opencv
将其显示的路径拷贝到Qt pro文件
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d
-lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml
-lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching
-lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -lrt -lpthread -lm -ldl
LIBS可根据实际的需求进行添加
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qdebug.h>
#include <QLabel>
#include <opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QImage img;
CreateOpenCV(img);
QLabel *mLable = new QLabel(this,Qt::SplashScreen|Qt::WindowStaysOnTopHint);
mLable->setPixmap(QPixmap(QPixmap::fromImage(img)));
QSize imageSize = mLable->pixmap()->size();
mLable->resize(imageSize);
mLable->move(0,0);
}
void MainWindow::CreateOpenCV(QImage &img )
{
cv::Mat image;
std::string imagePath = ":/image/photo.png";
image = cv::imread(imagePath,cv::IMREAD_COLOR);
cv::Mat rgbImage;
cv::cvtColor(image,rgbImage,CV_BGR2RGB);
img = QImage((const unsigned char*)rgbImage.data,rgbImage.cols,rgbImage.rows,
rgbImage.cols*rgbImage.channels(),QImage::Format_RGB888);
}
MainWindow::~MainWindow()
{
delete ui;
}