[转]arm-linux下qt + opencv开发环境的搭建(Altera DE1 Soc)
系统: ubuntu-14.04-desktop-i386
ARM 系统: DE1_SoC_FB
虚拟机工具: VMware-workstation-full-10.0.1
opencv: 3.2.0
qt: qt-linux-opensource-5.2.0-x86offline.run
交叉编译器: gcc-linaro-arm-linux-gnueabihf-4.7-2012.11-20121123_linux
cmake: 3.7.1
一.准备工作:
1.安装虚拟机的步骤无需多言,装完后记得重启;
2. $sudo apt-get update
$sudo apt-get upgrade
3.安装x86的工具链
$sudo apt-get install build-essential libgl1-mesa-dev
二. 配置交叉编译器
我云盘中提供的是gcc-linaro-arm-linux-gnueabihf-4.7-2012.11-20121123_linux.tar.bz2,版本为4.7.3
cd到相应目录
$tar xjf gcc-linaro-arm-linux-gnueabihf-4.7-2012.11-20121123_linux.tar.bz2
修改PATH
$gedit ~/.profile
在文件末尾加上
export PATH=/home/yulebron/gcc-linaro-arm-linux-gnueabihf-4.7-2012.11-20121123_linux/bin:$PATH
一定要根据你的安装目录修改路径(下同)
保存退出后$source ~/.profile
三.安装QT
1.下载QT
http://download.qt-project.org/official_releases/qt/5.2/5.2.0/ 选择qt-linux-opensource-5.2.0-x86offline.run (也可以下载我云盘中提供的,下同)
2.安装
进入相应目录
$chmod +x qt-linux-opensource-5.2.0-x86-offline.run
$./qt-linux-opensource-5.2.0-x86-offline.run
一路next,安装完毕
3.构建QT的ARM库
下载地址:http://qt-project.org/downloads 选择Qt libraries 4.8.6 for embedded Linux
cd到相应目录,解压
$tar -zxvf qt-everywhere-opensource-src-4.8.5.tar.gz
4.为ARM创建mkspecs
cd到qt-everywhere-opensource-src-4.8.5/mkspecs/qws 目录下
以下步骤根据自己的编译器名称进行修改!!!
重命名编译器linux-arm-gnueabi-g++
$cp -r linux-arm-gnueabi-g++ linux-arm-gnueabihf-g++
修改配置
$cd linux-arm-gnueabihf-g++
$gedit qmake.conf
将所有的arm-none-linux-gnueabi用linux-arm-gnueabihf替换,如图
安装QT库
$source ~/.profile
$cd ~/Downloads/qt-everywhere-opensource-src-4.8.6
$ ./configure -prefix /usr/local/qt-4.8.6-altera-soc -release -shared -nomake examples -nomake tools -nomake docs -make demos -xplatform qws/linux-arm-gnueabihf-g++ -embedded arm -little-endian -no-pch -v
弹出选项中选择o,随后选择yes
$make
$sudo make install
注意make这一步会跑数小时
5.QT中设置ARM的开发套件
选择Tools—Options ...
Name: Altera SoC FPGA Kit
Device Type: Select “Generic Linux
Device”
Compiler: Select “GCC (Altera SoC)”
Qt Version: Select “(Qt 4.8.6
(qt-4.8.6-atera-soc)”
以后的编程中如下选择即可生成ARM上可用的可执行文件,在相应文件夹中找到然后拷到ARM中即可
5.拷贝QT库到ARM上
$ cd /usr/local
$sudo tar -jcv -f
qt-4.8.6-altera-soc.tar.bz2 qt-4.8.6-altera-soc
将生成的的压缩文件拷贝到ARM的相应目录下解压即可
四.安装opencv的准备
1.安装Synaptic Package Manager
2.启动Synaptic
$sudo synaptic
依次点击Reload, Mark All Upgrades, Apply
3.安装zlib,png,jpeg
在搜索栏中输入png,mark libpng 12-0 为安装,点击apply,如图
输入zlib, 安装zlib1g
输入cmake, 安装cmake, cmake-qt-gui
五.安装opencv
下载opencv源码,并解压,新建一个文件夹用来包含创建编译opencv库时候的配置文件(/home/yulebron/work/opencvARM),另外在在/usr/local中创建一个opencvARM文件夹
$cmake-gui
一切ok, 点击generate
进入/home/yulebron/work/opencvARM
$vim CMakeCache.txt
修改 CMAKE_C_FLAGS:STRING=-fPIC
CMAKE_EXE_LINKER_FLAGS:STRING=-pthread
-lrt
保存并退出
$make
$sudo make install
将生成的/usr/local/opencvARM/lib中的文件拷贝到ARM的相应目录中
六.QT中包含opencv库
在.pro文件中添加如下语句,由于对opencv的库不是很了解,我将所有的库都包含了进去
INCLUDEPATH += /usr/local/opencvARM/include/opencv \
/usr/local/opencvARM/include/opencv2 \
/usr/local/opencvARM/include
LIBS += /usr/local/opencvARM/lib/libopencv_calib3d.so \
/usr/local/opencvARM/lib/libopencv_contrib.so
\
/usr/local/opencvARM/lib/libopencv_core.so
\
/usr/local/opencvARM/lib/libopencv_features2d.so \
/usr/local/opencvARM/lib/libopencv_flann.so
\
/usr/local/opencvARM/lib/libopencv_gpu.so
\
/usr/local/opencvARM/lib/libopencv_highgui.so
\
/usr/local/opencvARM/lib/libopencv_imgproc.so
\
/usr/local/opencvARM/lib/libopencv_legacy.so
\
/usr/local/opencvARM/lib/libopencv_ml.so
\
/usr/local/opencvARM/lib/libopencv_nonfree.so
\
/usr/local/opencvARM/lib/libopencv_objdetect.so
\
/usr/local/opencvARM/lib/libopencv_photo.so
\
/usr/local/opencvARM/lib/libopencv_stitching.so
\
/usr/local/opencvARM/lib/libopencv_superres.so
\
/usr/local/opencvARM/lib/libopencv_video.so
\
/usr/local/opencvARM/lib/libopencv_videostab.so
至此,一切工作已经完成
七.编程测试
1.新建swith.cpp, 复制上IplImage与QImage间转换用代码(这个非常有用)
#include "switch.h" ImageCVtoQT::ImageCVtoQT(IplImage *_srcImage) :srcImage(_srcImage) { assert(srcImage != NULL); width = srcImage -> width; height = srcImage -> height; channel = srcImage -> nChannels; } ImageCVtoQT::~ImageCVtoQT() { cvReleaseImage(&srcImage); } const QImage ImageCVtoQT::getQtImage() { QImage desImage = QImage(width, height, QImage::Format_RGB32); for(int i=0; i { for(int j=0;j { int r,g,b; if(RGB_TYPE == channel) { b = (int)CV_IMAGE_ELEM(srcImage, uchar , i, j*3+0); g = (int)CV_IMAGE_ELEM(srcImage, uchar , i, j*3+1); r = (int)CV_IMAGE_ELEM(srcImage, uchar , i, j*3+2); } else if(GRAY_TYPE == channel) { b = (int)CV_IMAGE_ELEM(srcImage, uchar, i, j); g = b; r = b; } desImage.setPixel(j, i, qRgb(r, g, b)); } } return desImage; } ///////////////////////////////////////////////////////////////////// ImageQTtoCV::ImageQTtoCV(QImage _srcImage) :srcImage(_srcImage) { assert(!srcImage.isNull()); width=srcImage.width(); height=srcImage.height(); } ImageQTtoCV::~ImageQTtoCV() { } IplImage *ImageQTtoCV::getCvImage() { IplImage *desImage=cvCreateImage(cvSize(width,height),8,3); for(int i=0;i { for(int j=0;j { QRgb rgb=srcImage.pixel(j,i); CV_IMAGE_ELEM(desImage,uchar,i,j*3+0)=qBlue(rgb); CV_IMAGE_ELEM(desImage,uchar,i,j*3+1)=qGreen(rgb); CV_IMAGE_ELEM(desImage,uchar,i,j*3+2)=qRed(rgb); } } return desImage; }
2.新建switch.h头文件
#ifndef SWITCH_H #define SWITCH_H //#include "highgui.h" #include "cv.h" #include "cxcore.h" #include #define RGB_TYPE 3 #define GRAY_TYPE 1 class ImageCVtoQT { public: ImageCVtoQT(IplImage *_srcImage); ~ImageCVtoQT(); const QImage getQtImage(void); private: IplImage *srcImage; //QImage desImage; int width; int height; int channel; }; class ImageQTtoCV { public: ImageQTtoCV(QImage _srcImage); ~ImageQTtoCV(); IplImage *getCvImage(void); private: QImage srcImage; int width; int height; //int channel; }; #endif // SWITCH_H
3.在界面文件中拉入一个lable,将其展开与窗体同大小
4.在dialog的构造函数中进行图像的导入和转换,过程是QImage导入图片-> 原始IplImage->opencv对原始IplImage进行处理->处理后IplImage->QImage->使用QImage借助qt窗体上显示图片
5.dialog.cpp
#include "dialog.h" #include "ui_dialog.h" #include "switch.h" #include "QtGui" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); //声明IplImage指针 IplImage *pImg = NULL; QImage *qImg = new QImage; //载入图片 if(!(qImg->load("/home/project/sao22.bmp"))) // 我的开发板支持bmp格式,小心路径 { return; } //switch ImageQTtoCV qtc(*qImg); pImg = qtc.getCvImage(); if(!pImg) return; IplImage *pGrayImg = NULL; pGrayImg = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1); cvCvtColor(pImg, pGrayImg, CV_BGR2GRAY); ImageCVtoQT ctq(pGrayImg); *qImg = ctq.getQtImage(); if(!qImg) return; ui->label->setPixmap(QPixmap::fromImage(*qImg)); } Dialog::~Dialog() { delete ui; }
从一切完成,从Release目录中拷贝相应的elf文件和即可在开发板上运行,miao..........