在zedboard板子上运行人脸检测例子

由于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


mainwindow.cpp 中代码如下

#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

可以查看下效果。


QTopencv一起移植到板卡里

修改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 生成可执行文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值