qt利⽤opencv3.4进⾏⼈脸识别和特征点提取qt利⽤opencv3.4进⾏⼈脸识别和特征点提取
1
本⽂是⽤qt5.6.3和opencv3.4、opencv_contrib3.4在windows平台下实现⼈脸识别以及特征值提取
**
2
PRO⽂件
QT+= core gui sql
greaterThan(QT_MAJOR_VERSION,4):QT+= widgets
TARGET= CameraManage
TEMPLATE= app
SOURCES+= main.cpp\
cameramanage.cpp
HEADERS+= cameramanage.h
FORMS+= cameramanage.ui
INCLUDEPATH+=D:/Qt/release/install/include \
D:/Qt/release/install/include/opencv \
D:/Qt/release/install/include/opencv2
LIBS+=-L D:/Qt/release/install/x86/mingw/lib/libopencv_*
LIBS+=-L D:/Qt/release/bin/libopencv_*.dll
.h⽂件
#ifndef CAMERAMANAGE_H
#define CAMERAMANAGE_H
#include <QMainWindow>
#include <QWidget>
#include <QImage>
#include <QLabel>
#include <QTimer>// 设置采集数据的间隔时间
#include <iostream>
#include <iterator>
#include <vector>
#include "opencv2/highgui/highgui.hpp"//包含opencv库头⽂件
#include "opencv/cv.h"
#include <opencv2/opencv.hpp>
#include <opencv2/face.hpp>
using namespace std;
using namespace cv;
using namespace cv::face;
#define COLOR Scalar(255,200,0)
namespace Ui {
class CameraManage;
class CameraManage;
}
class CameraManage :public QMainWindow
{
Q_OBJECT
public:
explicit CameraManage(QWidget *parent =0);
~CameraManage();
QImage  Mat2QImage(Mat& cvImg);
QImage  ScaleImage2Label(QImage qImage, QLabel* qLabel);
private slots:
void on_open_clicked();// 打开摄像头
void readFarme();// 读取当前帧信息
void on_close_clicked();// 关闭摄像头
void on_takePhono_clicked();// 拍照
void on_faceDete_clicked();// ⼈脸检测
void on_videoDete_clicked();
void drawPolyline
(
Mat &im,
const vector<Point2f>&landmarks,
const int start,
const int end,
bool isClosed =false
);
void drawLandmarks(Mat &im, vector<Point2f>&landmarks); private:
/
/    void detectAndDraw(Mat& img);
void detectAndDraw( Mat& img, CascadeClassifier& cascade,
CascadeClassifier& nestedCascade,
double scale, bool tryflip );
//    void detectAndDraw( Mat& img, CascadeClassifier& cascade,
//                        CascadeClassifier& nestedCascade,
//                        double scale, bool tryflip );
private:
Ui::CameraManage *ui;
QTimer    *timer;
QTimer    *timer_1;
QImage image;
CvCapture *cam;// 视频获取结构,⽤来作为视频获取函数的⼀个参数    Mat frame;
Mat fram_dete;
VideoCapture cap;
VideoCapture capDete;
CascadeClassifier face_cascade;
Ptr<face::Facemark> facemark;
};
#endif // CAMERAMANAGE_H
.h⽂件
#include "cameramanage.h"
#include "cameramanage.h"
#include "ui_cameramanage.h"
#include <QPixmap>
#include <QFileDialog>
#include <QPicture>
#include <opencv2/objdetect.hpp>
#include <iostream>
//static CvHaarClassifierCascade* cascade = 0;
//static CvMemStorage* storage = 0;
const char* cascade_name ="haarcascade_l";//⼈脸检测要⽤到的分类器const char* cascade_name_class ="haarcascade_eye_l";
CameraManage::CameraManage(QWidget *parent):
QMainWindow(parent),
ui(new Ui::CameraManage)
{
ui->setupUi(this);
// Create an instance of Facemark
facemark = face::FacemarkLBF::create();
// Load landmark detector
facemark->loadModel("E:\\QT_Work\\CameraManage1\\lbfmodel.yaml");
if(!face_cascade.load("E:\\QT_Work\\CameraManage1\\haarcascade_l"))        cout <<"--(!)Error loading face cascade\n";
rectangle函数opencvcam    =NULL;
timer  =new QTimer(this);
timer_1 =new QTimer(this);
//  image    = new QImage();        // 初始化
/*信号和槽*/
connect(timer,SIGNAL(timeout()),this,SLOT(readFarme()));// 时间到,读取当前摄像头信息connect(timer,SIGNAL(timeout()),this,SLOT(readFarme()));// 时间到,读取当前摄像头信息//    connect(timer_1, SIGNAL(timeout()), this, SLOT(on_videoDete_clicked()));
connect(ui->open,SIGNAL(clicked()),this,SLOT(on_open_clicked()));
connect(ui->close,SIGNAL(clicked()),this,SLOT(on_close_clicked()));
connect(ui->takePhono,SIGNAL(clicked()),this,SLOT(on_takePhono_clicked()));
connect(ui->videoDete,SIGNAL(clicked()),this,SLOT(on_videoDete_clicked()));
}
CameraManage::~CameraManage()
{
delete ui;
delete timer;
// delete image;
}
QImage  CameraManage::Mat2QImage(Mat& cvImg)
{
QImage qImg;
if(cvImg.channels()==3)//3 channels color image
{
cv::cvtColor(cvImg,cvImg,CV_BGR2RGB);
qImg =QImage((const unsigned char*)(cvImg.data),
QImage::Format_RGB888);
}
else if(cvImg.channels()==1)//grayscale image
{
qImg =QImage((const unsigned char*)(cvImg.data),
QImage::Format_Indexed8);
}
else
{
qImg =QImage((const unsigned char*)(cvImg.data),
QImage::Format_RGB888);
}
return qImg;
}
QImage CameraManage::ScaleImage2Label(QImage qImage, QLabel* qLabel) {
QImage qScaledImage;
QSize qImageSize = qImage.size();
QSize qLabelSize = qLabel->size();
double dWidthRatio =1.0*qImageSize.width()/ qLabelSize.width();
double dHeightRatio =1.0*qImageSize.height()/ qLabelSize.height();
if(dWidthRatio>dHeightRatio)
{
qScaledImage = qImage.scaledToWidth(qLabelSize.width());
}
else
{
qScaledImage = qImage.scaledToHeight(qLabelSize.height());
}
return qScaledImage;
}
void CameraManage::on_open_clicked()
{
cap.open(0);
timer->start(33);// 开始计时,超时则发出timeout()信号
}
void CameraManage::readFarme()
{
cap >> frame;
/*
//    imshow("nihao",frame);
image = Mat2QImage(frame);
//    image = QImage(image.scaled(240, 120));  //  change for the usb camera
QImage scaleImage = ScaleImage2Label( image, ui->lable );  // 显⽰到label上    ui->lable->setPixmap(QPixmap::fromImage(scaleImage));
ui->lable->setAlignment(Qt::AlignCenter);
ui->lable->show();*/
Mat gray, frame1, frame2;
vector<Rect> faces;
cvtColor(frame, gray,COLOR_BGR2GRAY);
cvtColor(frame, frame1,COLOR_BGR2RGB);
QImage img((const uchar*)frame1.data,
QImage::Format_RGB888);
//ui->lable->setPixmap(QPixmap::fromImage(img));
QImage scaleImage =ScaleImage2Label( img, ui->lable );
ui->lable->setPixmap(QPixmap::fromImage(scaleImage));
ui->lable->setAlignment(Qt::AlignCenter);
ui->lable->show();
ui->lable->show();
equalizeHist(gray, gray);
//-- Detect faces
face_cascade.detectMultiScale( gray, faces,1.1,2,0|CASCADE_SCALE_IMAGE,Size(60,60));
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);
ellipse( frame, center,Size( faces[i].width/2, faces[i].height/2),0,0,360,Scalar(255,0,255),4,8,0);
}
cvtColor(frame, frame2,COLOR_BGR2RGB);
QImage img1((const uchar*)frame2.data,
QImage::Format_RGB888);
//ui->lable_2->setPixmap(QPixmap::fromImage(img1));
QImage scaleImage1 =ScaleImage2Label( img1, ui->lable_2 );
ui->lable_2->setPixmap(QPixmap::fromImage(scaleImage1));
ui->lable_2->setAlignment(Qt::AlignCenter);
ui->lable_2->show();
vector< vector<Point2f>> landmarks;
// Run landmark detector
bool success = facemark->fit(frame,faces,landmarks);
if(success)
{
// If successful, render the landmarks on the face
for(int i =0; i < landmarks.size(); i++)
{
drawLandmarks(frame, landmarks[i]);
drawFacemarks(frame, landmarks[i],Scalar(0,0,255));
}
}
imshow("Facial Landmark Detection", frame);
}
void CameraManage::on_close_clicked()
{
timer->stop();// 停⽌读取数据。
ui->lable->setText("video closed");
}
void CameraManage::on_takePhono_clicked()
{
bool b = image.save("01.jpg","JPG");//保存为图⽚⽂件
if(!b)
return;
}
void CameraManage::on_faceDete_clicked()
{
Mat image_dete;
const char* filename ="01.jpg";//待检测图像(包含绝对路径)
image_dete =imread(filename,CV_LOAD_IMAGE_COLOR);//加载图像
//  detectAndDraw( image_dete);
}
/*************************

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。