#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
namespace Ui {
class MainWindow;
}
using namespace cv;
class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void detectFaces(Mat );
void detectEyes(Mat );
void getImage();
QImage cvMat2QImage(const cv::Mat& mat);
cv::Mat QImage2cvMat(QImage image);
private slots:
void on_start_pushButton_clicked();

void on_stop_pushButton_clicked();

private:
Ui::MainWindow *ui;
Mat image;
bool state;
CascadeClassifier face_cascade, eyes_cascade;
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QImage>
#include<QDebug>
#include<QThread>
std::vector<Rect> faces;
String window_name = "Face Detection";
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
state==true;
bool b=face_cascade.load("F:\\Qt_opencv\\third_part\\etc\\haarcascades\\haarcascade_frontalface_alt.xml");
bool a=eyes_cascade.load("F:\\Qt_opencv\\third_part\\etc\\haarcascades\\haarcascade_eye_tree_eyeglasses.xml");

}

void MainWindow::detectFaces(Mat frame) {
//定义一些不同的颜色表征出不同的人脸
const static Scalar colors[] = {
CV_RGB(0, 0, 255),
CV_RGB(0, 128, 255),
CV_RGB(0, 255, 255),
CV_RGB(0, 255, 0),
CV_RGB(255, 128, 0),
CV_RGB(255, 255, 0),
CV_RGB(255, 0, 0),
CV_RGB(255, 0, 255) };
Mat frame_gray;
// 灰度变换
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
// 直方图均衡
equalizeHist(frame_gray, frame_gray);
// 多尺度人脸检测
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 3, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
//需要把:x,y,width,height 四个参数单独提取出来。提高速度
Mat roiImage(frame.rows, frame.cols, CV_8UC3);
// 人脸检测结果判定
for (size_t i = 0; i < faces.size(); i++)
{
// 检测到人脸中心
Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);//定义2D点
ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);

frame(cv::Rect(faces[i].x, faces[i].y, faces[i].width,
faces[i].height)).copyTo(roiImage);
}
}
void MainWindow::detectEyes(Mat frame) {
std::vector<Rect> faces;
//定义一些不同的颜色表征出不同的人脸
const static Scalar colors[] = {
CV_RGB(0, 0, 255),
CV_RGB(0, 128, 255),
CV_RGB(0, 255, 255),
CV_RGB(0, 255, 0),
CV_RGB(255, 128, 0),
CV_RGB(255, 255, 0),
CV_RGB(255, 0, 0),
CV_RGB(255, 0, 255) };
Mat frame_gray;
// 灰度变换
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
// 直方图均衡
equalizeHist(frame_gray, frame_gray);
// 多尺度人脸检测
eyes_cascade.detectMultiScale(frame_gray, faces, 1.1, 3, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
//需要把:x,y,width,height 四个参数单独提取出来。提高速度
Mat roiImage(frame.rows, frame.cols, CV_8UC3);

// 人脸检测结果判定
for (size_t i = 0; i < faces.size(); i++)
{
// 检测到人脸中心
Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);//定义2D点
ellipse(frame, center, Size(10, 10), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);

frame(cv::Rect(faces[i].x, faces[i].y, faces[i].width,
faces[i].height)).copyTo(roiImage);
}
}
void MainWindow::getImage()
{
char c;
VideoCapture inputVideo(0); //0为外部摄像头的ID,1为笔记本内置摄像头的ID
Mat src;
while (1)
{
inputVideo >> src;
detectEyes(src);//瞳孔检测
detectFaces(src);//人脸检测
imshow("input", src);
if (state == false)
{
break;
}
c = waitKey(10);
if (c == 27){ break;}
}

}
MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_start_pushButton_clicked()
{
getImage();
}

void MainWindow::on_stop_pushButton_clicked()
{
state == false;
}
QImage MainWindow::cvMat2QImage(const cv::Mat& mat)
{

if(mat.type() == CV_8UC1)
{
QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
image.setColorCount(256);
for(int i = 0; i < 256; i++)
{
image.setColor(i, qRgb(i, i, i));
}
uchar *pSrc = mat.data;
for(int row = 0; row < mat.rows; row ++)
{
uchar *pDest = image.scanLine(row);
memcpy(pDest, pSrc, mat.cols);
pSrc += mat.step;
}
return image;
}
else if(mat.type() == CV_8UC3)
{
const uchar *pSrc = (const uchar*)mat.data;
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return image.rgbSwapped();
}
else if(mat.type() == CV_8UC4)
{
const uchar *pSrc = (const uchar*)mat.data;
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
return image.copy();
}
else
{

return QImage();
}
}
cv::Mat MainWindow::QImage2cvMat(QImage image)
{
cv::Mat mat;
switch(image.format())
{
case QImage::Format_ARGB32:
case QImage::Format_RGB32:
case QImage::Format_ARGB32_Premultiplied:
mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
break;
case QImage::Format_RGB888:
mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
cv::cvtColor(mat, mat, CV_BGR2RGB);
break;
case QImage::Format_Indexed8:
mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
break;
}
return mat;
}
源码下载地址:http://download.csdn.net/detail/cqltbe131421/9905203