由于opencv 的highgui使用GTK在zedboard下没有交叉编译成功,所以本程序使用QT做了个简单的界面,在QT中调用Opencv函数,进行人脸检测。
首先建立QT项目,在主界面中加入一个Lable,并设置成一定大小。
mainwindow.h 的代码如下
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <cv.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <QTimer>
#include <QPixmap>
namespace Ui {
class MainWindow;
}
using namespace cv;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void detectAndDraw( Mat& img, CascadeClassifier& cascade,
double scale);
private:
Ui::MainWindow *ui;
CvCapture *capture;
IplImage *iplImg, *iplImg2;
Mat frame,frameCopy;
QTimer *timer;
double scale ;
string cascadeName ;
CascadeClassifier cascade;
private slots:
void getFrame();
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
qDebug()<<CV_VERSION;
ui->setupUi(this);
timer = new QTimer(this);
capture = cvCaptureFromCAM(0);
scale = 1;
cascadeName = "/home/wjj/workplace/test/lbpcascade_frontalface.xml";
if(!cascade.load(cascadeName)){
qDebug()<<"ERROR: Could not load classifier cascade";
}
if(capture == NULL){
qDebug()<<"ERROR: Capture from CAM didn't work";
}
timer->start(50);
connect(timer,SIGNAL(timeout()),this,SLOT(getFrame()));
}
void MainWindow::getFrame()
{
iplImg =cvQueryFrame(capture);
frame = iplImg;
if(iplImg->origin == IPL_ORIGIN_TL)
frame.copyTo(frameCopy);
else
flip(frame,frameCopy,0);
detectAndDraw(frameCopy,cascade,scale);
}
void MainWindow::detectAndDraw( Mat& img, CascadeClassifier& cascade,
double scale){
double t = 0;
int i=0;
vector<Rect> faces;
Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );
cvtColor( img, gray, CV_BGR2GRAY );
cv::resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
equalizeHist( smallImg, smallImg );
t = (double)cvGetTickCount();
cascade.detectMultiScale( smallImg, faces,
1.1, 2, 0
//|CV_HAAR_FIND_BIGGEST_OBJECT
//|CV_HAAR_DO_ROUGH_SEARCH
|CV_HAAR_SCALE_IMAGE
,
Size(30, 30) );
t = (double)cvGetTickCount() - t;
printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
{
rectangle( img, cvPoint(cvRound(r->x*scale), cvRound(r->y*scale)),
cvPoint(cvRound((r->x + r->width-1)*scale), cvRound((r->y + r->height-1)*scale)),
Scalar(CV_RGB(0,0,255)), 1, 8, 0);
}
QImage image = QImage((const uchar*)(img.data), img.cols, img.rows, QImage::Format_RGB888).rgbSwapped();
ui->label->setPixmap(QPixmap::fromImage(image));
}
MainWindow::~MainWindow()
{
timer->stop();
cvReleaseCapture(&capture);
delete ui;
}
在PC上运行该文件时,需要修改后缀为pro的文件
加入如下语句
INCLUDEPATH +=/usr/local/include/opencv \
/usr/local/include/opencv2
LIBS += /usr/local/lib/libopencv_core.so\
/usr/local/lib/libopencv_highgui.so\
/usr/local/lib/libopencv_imgproc.so\
/usr/local/lib/libopencv_objdetect.so
可以查看下效果。
将QT和opencv一起移植到板卡里
修改qmake.conf该文件位于所编译的嵌入式QT文件夹下
/opt/zedboard/qt/install/mkspecs/qws/linux-arm-gnueabi-g++/qmake.conf
在此文件中加入
INCLUDEPATH += /opt/zedboard/opencv_zed/opencv-2.4.4/install/include \
/opt/zedboard/opencv_zed/opencv-2.4.4/install/include/opencv \
/opt/zedboard/opencv_zed/opencv-2.4.4/install/include/opencv2
LIBS += /opt/zedboard/opencv_zed/opencv-2.4.4/install/lib/libopencv_highgui.so \
/opt/zedboard/opencv_zed/opencv-2.4.4/install/lib/libopencv_core.so \
/opt/zedboard/opencv_zed/opencv-2.4.4/install/lib/libopencv_imgproc.s o \
/opt/zedboard/opencv_zed/opencv-2.4.4/install/lib/*.s
这里opencv的链接库是交叉编译好了的链接库
运行qmake -project
qmake
make 生成可执行文件