我正在尝试使用opencv和qt实现一个实时人脸检测程序。代码是mainwindow.h
#include <QMainWindow>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <QLabel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Ui::MainWindow* _ui;
CvCapture* _capture;
IplImage* _img;
CvHaarClassifierCascade* _cascade;
CvMemStorage* _storage;
QList<CvScalar> _colors;
QPixmap* _pixmap;
QTimer* _timer;
QLabel* labelCapture;
private slots:
void on_actionCapture_triggered();
};
//#endif // MAINWINDOW_H
主文件
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
主窗口.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "opencv/cvaux.h"
#include <QTimer>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
_ui(new Ui::MainWindow)
{
_ui->setupUi(this);
QLabel* labelCapture;
_capture = cvCaptureFromCAM( 0 );
_cascade = (CvHaarClassifierCascade*)cvLoad("haarcascade_frontalface_alt2.xml");
_storage = cvCreateMemStorage(0);
_colors << cvScalar(0.0,0.0,255.0) << cvScalar(0.0,128.0,255.0)
<< cvScalar(0.0,255.0,255.0) << cvScalar(0.0,255.0,0.0)
<< cvScalar(255.0,128.0,0.0) << cvScalar(255.0,255.0,0.0)
<< cvScalar(255.0,0.0,0.0) << cvScalar(255.0,0.0,255.0);
_timer = new QTimer(this);
connect(_timer, SIGNAL(timeout()), this, SLOT(on_actionCapture_triggered()));
_timer->start(10);
labelCapture=new QLabel();
}
MainWindow::~MainWindow()
{
cvReleaseImage(&_img);
cvReleaseCapture(&_capture);
delete _ui;
}
void MainWindow::on_actionCapture_triggered()
{
// Query camera for next frame
_img = cvQueryFrame( _capture );
if (_img)
{
// Detect objects
cvClearMemStorage( _storage );
CvSeq* objects = cvHaarDetectObjects(_img,
_cascade,
_storage,
1.1,
3,
CV_HAAR_DO_CANNY_PRUNING,
cvSize( 100, 100 ));
int n = (objects ? objects->total : 0);
CvRect* r;
// Loop through objects and draw boxes
for( int i = 0; i < n; i++ )
{
r = ( CvRect* )cvGetSeqElem( objects, i );
cvRectangle( _img,
cvPoint( r->x, r->y ),
cvPoint( r->x + r->width, r->y + r->height ),
_colors[i%8]
);
}
// Convert IplImage to QImage
QImage image = QImage((const uchar *)_img->imageData,
_img->width,
_img->height,
QImage::Format_RGB888).rgbSwapped();
_pixmap = new QPixmap(QPixmap::fromImage(image));
_ui->labelCapture->setPixmap(*_pixmap);
}
}
但是我收到以下错误...关于函数 on_action_capture_triggered() 类 ui::mainWindow 没有名为“labelCapture”的成员请帮助...。