win10+qt+vs2013+opencv人脸检测详细步骤
2017-12-11 22:46
232 查看
(1)布局ui界面,改变对象名称
(2)facedetection.h中代码为
#ifndef FACEDETECTION_H
#define FACEDETECTION_H
#include <QtWidgets/QMainWindow>
#include "ui_facedetection.h"
#include<QImage>
#include<QTimer>
#include<opencv2/opencv.hpp>
using namespace cv;
class facedetection : public QMainWindow
{
Q_OBJECT
public:
facedetection(QWidget *parent = 0);
~facedetection();
void detect_and_draw(IplImage* img);
private:
Ui::facedetectionClass ui;
QImage *imag;
QTimer *timer;
CvCapture *cam;
IplImage *frame;
private slots:
void openCamaraSlot();
void readFarmeSlot();
void takePhotoSlot();
void closeCamaraSlot();
void faceDetectionSlot();
};
#endif // FACEDETECTION_H
(3)facedetection.cpp中代码为
#include "facedetection.h"
static CvHaarClassifierCascade* cascade = 0;
static CvMemStorage* storage = 0;
const char* cascade_name = "haarcascade_frontalface_alt.xml"; //人脸检测要用到的分类器,在安装opencv的文件中
facedetection::facedetection(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
cam = NULL; // 初始化
timer = new QTimer(this);
imag = new QImage();
connect(timer, SIGNAL(timeout()), this, SLOT(readFarmeSlot())); // timeout,读取当前摄像头信息
connect(ui.openCameraButton, SIGNAL(clicked()), this, SLOT(openCamaraSlot()));
connect(ui.takePhotoButton, SIGNAL(clicked()), this, SLOT(takePhotoSlot()));
connect(ui.closeCameraButton, SIGNAL(clicked()), this, SLOT(closeCamaraSlot()));
connect(ui.faceDetection, SIGNAL(clicked()), this, SLOT(faceDetectionSlot()));
}
facedetection::~facedetection()
{
}
void facedetection::openCamaraSlot()
{
cam = cvCreateCameraCa
4000
pture(0);//打开摄像头
timer->start(33); // 开始计时,超时则发出timeout()信号
}
void facedetection::readFarmeSlot()
{
frame = cvQueryFrame(cam);// 从摄像头中抓取并返回每一帧
QImage image = QImage((const uchar*)frame->imageData, frame->width, frame->height,
QImage::Format_RGB888).rgbSwapped();//转换为QImage格式
ui.videoWindow->setPixmap(QPixmap::fromImage(image)); // 将图片显示到视频显示上
}
void facedetection::takePhotoSlot()
{
frame = cvQueryFrame(cam);// 从摄像头中抓取并返回每一帧
QImage qimage = QImage((const uchar*)frame->imageData, frame->width, frame->height,
QImage::Format_RGB888).rgbSwapped();//将抓取到的帧,转换为QImage格式
ui.imageWindow->setPixmap(QPixmap::fromImage(qimage));
bool b = qimage.save("01.jpg", "JPG");//保存为01.jpg
if (!b)
return;
}
void facedetection::closeCamaraSlot()
{
timer->stop();
cvReleaseCapture(&cam);//释放内存;
ui.videoWindow->setText("video closed");
}
void facedetection::detect_and_draw(IplImage *img)
{
static CvScalar colors[] =
{
{ { 0, 0, 255 } },
{ { 0, 128, 255 } },
{ { 0, 255, 255 } },
{ { 0, 255, 0 } },
{ { 255, 128, 0 } },
{ { 255, 255, 0 } },
{ { 255, 0, 0 } },
{ { 255, 0, 255 } }
};
double scale = 1.3;
IplImage* gray = cvCreateImage(cvSize(img->width, img->height), 8, 1);
IplImage* small_img = cvCreateImage(cvSize(cvRound(img->width / scale),
cvRound(img->height / scale)), 8, 1);
cvCvtColor(img, gray, CV_BGR2GRAY);
cvResize(gray, small_img, CV_INTER_LINEAR);
cvEqualizeHist(small_img, small_img);
cvClearMemStorage(storage);
if (cascade)
{
CvSeq* faces = cvHaarDetectObjects(small_img, cascade, storage, 1.1, 2, 0,
cvSize(30, 30));
for (int i = 0; i < (faces ? faces->total : 0); i++)
{
CvRect* r = (CvRect*)cvGetSeqElem(faces, i);
CvPoint center;
int radius;
center.x = cvRound((r->x + r->width*0.5)*scale);
center.y = cvRound((r->y + r->height*0.5)*scale);
radius = cvRound((r->width + r->height)*0.25*scale);
cvCircle(img, center, radius, colors[i % 8], 3, 8, 0);
}
}
cvShowImage("result", img);
// cvResizeWindow( "result", 800, 600 );
cvReleaseImage(&gray);
cvReleaseImage(&small_img);
}
void facedetection::faceDetectionSlot()
{
cascade = (CvHaarClassifierCascade*)cvLoad(cascade_name, 0, 0, 0); //加载人脸检测所用的分类器
if (!cascade)
{
fprintf(stderr, "ERROR: Could not load classifier cascade\n");
return;
}
storage = cvCreateMemStorage(0); //动态存储结构,用来存储人脸在图像中的位置
cvNamedWindow("result", 1);
const char* filename = "01.jpg";//待检测图像(包含绝对路径)
IplImage* image = cvLoadImage(filename, 1); //加载图像
detect_and_draw(image); //对加载的图像进行检测
cvWaitKey(0);
cvReleaseImage(&image);
cvDestroyWindow("result");
}
(4)F5运行打开摄像头,拍照
(5)点击人脸检测
(2)facedetection.h中代码为
#ifndef FACEDETECTION_H
#define FACEDETECTION_H
#include <QtWidgets/QMainWindow>
#include "ui_facedetection.h"
#include<QImage>
#include<QTimer>
#include<opencv2/opencv.hpp>
using namespace cv;
class facedetection : public QMainWindow
{
Q_OBJECT
public:
facedetection(QWidget *parent = 0);
~facedetection();
void detect_and_draw(IplImage* img);
private:
Ui::facedetectionClass ui;
QImage *imag;
QTimer *timer;
CvCapture *cam;
IplImage *frame;
private slots:
void openCamaraSlot();
void readFarmeSlot();
void takePhotoSlot();
void closeCamaraSlot();
void faceDetectionSlot();
};
#endif // FACEDETECTION_H
(3)facedetection.cpp中代码为
#include "facedetection.h"
static CvHaarClassifierCascade* cascade = 0;
static CvMemStorage* storage = 0;
const char* cascade_name = "haarcascade_frontalface_alt.xml"; //人脸检测要用到的分类器,在安装opencv的文件中
facedetection::facedetection(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
cam = NULL; // 初始化
timer = new QTimer(this);
imag = new QImage();
connect(timer, SIGNAL(timeout()), this, SLOT(readFarmeSlot())); // timeout,读取当前摄像头信息
connect(ui.openCameraButton, SIGNAL(clicked()), this, SLOT(openCamaraSlot()));
connect(ui.takePhotoButton, SIGNAL(clicked()), this, SLOT(takePhotoSlot()));
connect(ui.closeCameraButton, SIGNAL(clicked()), this, SLOT(closeCamaraSlot()));
connect(ui.faceDetection, SIGNAL(clicked()), this, SLOT(faceDetectionSlot()));
}
facedetection::~facedetection()
{
}
void facedetection::openCamaraSlot()
{
cam = cvCreateCameraCa
4000
pture(0);//打开摄像头
timer->start(33); // 开始计时,超时则发出timeout()信号
}
void facedetection::readFarmeSlot()
{
frame = cvQueryFrame(cam);// 从摄像头中抓取并返回每一帧
QImage image = QImage((const uchar*)frame->imageData, frame->width, frame->height,
QImage::Format_RGB888).rgbSwapped();//转换为QImage格式
ui.videoWindow->setPixmap(QPixmap::fromImage(image)); // 将图片显示到视频显示上
}
void facedetection::takePhotoSlot()
{
frame = cvQueryFrame(cam);// 从摄像头中抓取并返回每一帧
QImage qimage = QImage((const uchar*)frame->imageData, frame->width, frame->height,
QImage::Format_RGB888).rgbSwapped();//将抓取到的帧,转换为QImage格式
ui.imageWindow->setPixmap(QPixmap::fromImage(qimage));
bool b = qimage.save("01.jpg", "JPG");//保存为01.jpg
if (!b)
return;
}
void facedetection::closeCamaraSlot()
{
timer->stop();
cvReleaseCapture(&cam);//释放内存;
ui.videoWindow->setText("video closed");
}
void facedetection::detect_and_draw(IplImage *img)
{
static CvScalar colors[] =
{
{ { 0, 0, 255 } },
{ { 0, 128, 255 } },
{ { 0, 255, 255 } },
{ { 0, 255, 0 } },
{ { 255, 128, 0 } },
{ { 255, 255, 0 } },
{ { 255, 0, 0 } },
{ { 255, 0, 255 } }
};
double scale = 1.3;
IplImage* gray = cvCreateImage(cvSize(img->width, img->height), 8, 1);
IplImage* small_img = cvCreateImage(cvSize(cvRound(img->width / scale),
cvRound(img->height / scale)), 8, 1);
cvCvtColor(img, gray, CV_BGR2GRAY);
cvResize(gray, small_img, CV_INTER_LINEAR);
cvEqualizeHist(small_img, small_img);
cvClearMemStorage(storage);
if (cascade)
{
CvSeq* faces = cvHaarDetectObjects(small_img, cascade, storage, 1.1, 2, 0,
cvSize(30, 30));
for (int i = 0; i < (faces ? faces->total : 0); i++)
{
CvRect* r = (CvRect*)cvGetSeqElem(faces, i);
CvPoint center;
int radius;
center.x = cvRound((r->x + r->width*0.5)*scale);
center.y = cvRound((r->y + r->height*0.5)*scale);
radius = cvRound((r->width + r->height)*0.25*scale);
cvCircle(img, center, radius, colors[i % 8], 3, 8, 0);
}
}
cvShowImage("result", img);
// cvResizeWindow( "result", 800, 600 );
cvReleaseImage(&gray);
cvReleaseImage(&small_img);
}
void facedetection::faceDetectionSlot()
{
cascade = (CvHaarClassifierCascade*)cvLoad(cascade_name, 0, 0, 0); //加载人脸检测所用的分类器
if (!cascade)
{
fprintf(stderr, "ERROR: Could not load classifier cascade\n");
return;
}
storage = cvCreateMemStorage(0); //动态存储结构,用来存储人脸在图像中的位置
cvNamedWindow("result", 1);
const char* filename = "01.jpg";//待检测图像(包含绝对路径)
IplImage* image = cvLoadImage(filename, 1); //加载图像
detect_and_draw(image); //对加载的图像进行检测
cvWaitKey(0);
cvReleaseImage(&image);
cvDestroyWindow("result");
}
(4)F5运行打开摄像头,拍照
(5)点击人脸检测
相关文章推荐
- win10+qt+vs2013+opencv人脸检测详细步骤
- win10+qt+vs2013+opencv人脸检测详细步骤
- win10+qt+vs2013+opencv人脸检测详细步骤
- win10+qt+vs2013+opencv人脸检测详细步骤
- win10+qt+vs2013+opencv人脸检测详细步骤
- win10+qt+vs2013+opencv人脸检测详细步骤
- win10+qt+vs2013+opencv人脸检测详细步骤
- win10+qt+vs2013+opencv人脸检测详细步骤
- win10+qt+vs2013+opencv人脸检测详细步骤
- win10+qt+vs2013+opencv人脸检测详细步骤
- YOLOv2训练自己的数据来实现人脸检测详细步骤
- openCV实现多人脸检测,多眼部检测,完整代码和详细注释
- 人脸检测FDDB评测的详细步骤
- keras系列︱人脸表情分类与识别:opencv人脸检测+Keras情绪分类(四)
- OpenCV 实践程序14——图片中人脸检测
- 结合dlib与OpenCV的视频流人脸检测
- OpenCVForUnity使用Haar分类器检测人脸和眼睛
- opencv 检测人脸
- 让OpenCV输出人脸检测的得分代码(置信率)
- python+OpenCV进行人脸检测