OpenCV包含了大量的机器视觉处理算法,Qt是一个跨平台的C++图形用户界面库,一般通过OpenCV完成图像处理算法,用Qt实现人机交互接口。本文主要实现 Qt+Opencv在ARM-A9 4412上实现图像采集拍照的功能。
Opencv移植到ARM上参考上篇博客 Ubuntu14.04 Opencv2.4.9交叉编译
Qt4.7.1库采用讯为公司提供的,Qtcreator之前使用Qtcreator5.3.2,编译过程中出现各种奇葩的错误,也不知道为什么,后来使用Qtcreator2.6.1就基本没出现那
些奇怪的问题了。本人目前对Qt还不是很熟,出现这样问题留给以后解决…
摄像头采用某宝上普通的USB摄像头。
打开Qtcreator,新建一个Qt Gui工程,首先修改.pro文件,包含Opencv的头文件和库文件路径。
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = DIalogCam TEMPLATE = app UI_DIR=./UI SOURCES += main.cpp\ dialog.cpp HEADERS += dialog.h FORMS += dialog.ui #INCLUDEPATH += /opt/qt-4.7.1/include #INCLUDEPATH += /opt/qt-4.7.1/include/QtGui #INCLUDEPATH += /usr/local/include #INCLUDEPATH += /usr/local/include/opencv #INCLUDEPATH += /usr/local/include/opencv2 #LIBS += /usr/local/lib/libopencv_highgui.so #LIBS += /usr/local/lib/libopencv_legacy.so #LIBS += /usr/local/lib/libopencv_imgproc.so #LIBS += /usr/local/lib/libopencv_flann.so #LIBS += /usr/local/lib/libopencv_ml.so #LIBS += /usr/local/lib/libopencv_core.so #LIBS += /usr/local/lib/libopencv_photo.so #LIBS += /usr/local/lib/libopencv_contrib.so #LIBS += /usr/local/lib/libopencv_video.so #LIBS += /usr/local/lib/libopencv_objdetect.so #LIBS += /usr/local/lib/libopencv_nonfree.so #LIBS += /usr/local/lib/libopencv_calib3d.so #LIBS += /usr/local/lib/libopencv_features2d.so INCLUDEPATH += /usr/local/arm/opencv-install/include/ INCLUDEPATH += /usr/local/arm/opencv-install/include/opencv INCLUDEPATH += /usr/local/arm/opencv-install/include/opencv2 LIBS += /usr/local/arm/opencv-install/lib/libopencv_highgui.so LIBS += /usr/local/arm/opencv-install/lib/libopencv_legacy.so LIBS += /usr/local/arm/opencv-install/lib/libopencv_imgproc.so LIBS += /usr/local/arm/opencv-install/lib/libopencv_flann.so LIBS += /usr/local/arm/opencv-install/lib/libopencv_ml.so LIBS += /usr/local/arm/opencv-install/lib/libopencv_core.so LIBS += /usr/local/arm/opencv-install/lib/libopencv_photo.so LIBS += /usr/local/arm/opencv-install/lib/libopencv_contrib.so LIBS += /usr/local/arm/opencv-install/lib/libopencv_video.so LIBS += /usr/local/arm/opencv-install/lib/libopencv_objdetect.so LIBS += /usr/local/arm/opencv-install/lib/libopencv_nonfree.so LIBS += /usr/local/arm/opencv-install/lib/libopencv_calib3d.so LIBS += /usr/local/arm/opencv-install/lib/libopencv_features2d.so
上面Opencv路径是安装在ubuntu上x86架构opencv,下面是交叉编译的ARM架构opencv,在ubuntu上或者ARM上运行分别包含对应的路径。一般先在PC上调试,再在板子上运行。
在dialog.ui加上一个lable和四个button
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); cam = NULL; timer = new QTimer(this); imag = new QImage(); /*信号和槽*/ connect(timer, SIGNAL(timeout()), this, SLOT(readFarme())); // 时间到,读取当前摄像头信息 connect(ui->openCam, SIGNAL(clicked()), this, SLOT(openCamara())); connect(ui->closeCam, SIGNAL(clicked()), this, SLOT(closeCamara())); connect(ui->savePic, SIGNAL(clicked()), this, SLOT(takingPictures())); connect(ui->openPic, SIGNAL(clicked()), this, SLOT(OpenPictures())); } void Dialog::openCamara() { cam = cvCreateCameraCapture(204);//204 timer->start(33); // 开始计时,超时则发出timeout()信号 } void Dialog::readFarme() { frame = cvQueryFrame(cam); QImage image = QImage((const uchar*)frame->imageData, frame->width, frame->height, QImage::Format_RGB888).rgbSwapped(); ui->label->setPixmap(QPixmap::fromImage(image)); // ui->label->resize(image.width(),image.height()); } void Dialog::closeCamara() { timer->stop(); // 停止读取数据。 cvReleaseCapture(&cam);//释放内存; ui->label->setPixmap(NULL); } void Dialog::takingPictures() { cvSaveImage("saveImage.jpg",frame); } void Dialog::OpenPictures() { timer->stop(); // 停止读取数据。 cvReleaseCapture(&cam);//释放内存; ui->label->setPixmap(NULL); QString filename; filename=QFileDialog::getOpenFileName(this, tr("Choose Picture"), "", tr("Images (*.png *.bmp *.jpg *.tif *.GIF )")); if(filename.isEmpty()) { return ; } else { if(! ( imag->load(filename) ) ) //加载图像 { return ; } ui->label->setPixmap(QPixmap::fromImage(*imag)); } } Dialog::~Dialog() { delete ui; delete timer; delete imag; delete frame; }
我的4412板子USB摄像头设备节点是/dev/video4,其中 cam =cvCreateCameraCapture(204) 部分,这个函数意思打开video4,网上讨论括号里参数可以是0 -1 202 ,需要根据自己实际情况修改,我在ubuntu运行 0 -1都可以。
dialog.h部分
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QImage> #include <QTimer> // 设置采集数据的间隔时间 #include "highgui.h" //包含opencv库头文件 #include "cv.h" namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); private slots: void openCamara(); // 打开摄像头 void readFarme(); // 读取当前帧信息 void closeCamara(); // 关闭摄像头 void takingPictures(); // 拍照 void OpenPictures(); private: Ui::Dialog *ui; QTimer *timer; QImage *imag; CvCapture *cam;// 视频获取结构, 用来作为视频获取函数的一个参数 IplImage *frame;//申请IplImage类型指针,就是申请内存空间来存放每一帧图像 }; #endif // DIALOG_H
使用qtcreator在release目录下生成可执行文件,通过file命令查看是x86-64架构,不能在ARM上运行。
root@ubuntu:/home/itop/Qtwork/DIalogCam/DIalogCam-build-Desktop-Release# file DIalogCam
DIalogCam: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=21ccf5af51609796c148650ece5a45903c4b99ac, not stripped
在工程目录下运行qt-4.7.1的qmake,生成makefile文件,最后再make生成ARM可运行文件
root@ubuntu:/home/itop/Qtwork/DIalogCam# /opt/qt-4.7.1/bin/qmake
root@ubuntu:/home/itop/Qtwork/DIalogCam# make
root@ubuntu:/home/itop/Qtwork/DIalogCam# file DIalogCam
DIalogCam: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.14, not stripped
哈哈,至此就可以把生成可运行文件拷贝到板子上run了...
./DIalogCam -qws &
看下实际效果图