OpenCV人脸识别技术基础知识2Day/4Day

一。预备知识:定时器事件

       如何 使用定时器呢?引什么文件?如何开始?如何关闭?

/**************************头文件**************************/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include<QTimerEvent>
#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void timerEvent (QTimerEvent *);
private slots:
    void on_begin_clicked();

    void on_stop_clicked();

private:
    Ui::MainWindow *ui;
    int count;//显示计数的变量
    int timerId;
};

#endif // MAINWINDOW_H

/**************************cpp文件用于使用***********************/
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    count=0;//初始化
    //开启定时器事件


}

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

void MainWindow::timerEvent(QTimerEvent *)
{
    count++;
    ui->label->setNum(count);
}

void MainWindow::on_begin_clicked()
{
     timerId=startTimer (1000);
}

void MainWindow::on_stop_clicked()
{
    killTimer(timerId);
}

 实现视频中间出现一个白色方框,为接下来的人脸识别做准备:

/****************************.h文件************************/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include<opencv2/opencv.hpp>
#include <QMainWindow>
#include<QTimerEvent>
using namespace cv;
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    void timerEvent (QTimerEvent* );
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    VideoCapture vc;//定义读取摄像头的类
    Mat frame;//处理图像类
};

#endif // MAINWINDOW_H
/****************************.cpp***********************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    if(vc.open(0)){
        qDebug()<<"fail";
    }
    startTimer(50);
}
void MainWindow::timerEvent (QTimerEvent *){
    vc>>frame;
    //翻转
    flip(frame,frame,1);
    //色彩空间转换
    cvtColor (frame,frame,CV_BGR2RGB);
    //创建矩形类
    cv::resize (frame,frame,Size(600,600));
    Rect r (frame.cols/2-100,frame.rows/2-100,200,200);
    //画矩形
    rectangle(frame,r,Scalar(255,255,255),3);
    //Mat-->QImage
    QImage img(frame.data,frame.cols,frame.rows,frame.cols*frame.channels (),QImage::Format_RGB888);
    //QImage-->QPixmap
    QPixmap p=QPixmap::fromImage (img);
    ui->label->setPixmap (p);
}

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

实现输入数值,改变亮度

/*****************************mainwindow.h**********************/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include<opencv2/opencv.hpp>
#include <QMainWindow>
using namespace cv;
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void showImge(Mat src);
private slots:
    void on_pushButton_clicked();


private:
    Ui::MainWindow *ui;
    Mat src;
};

#endif // MAINWINDOW_H
/*****************************mainwindow.cpp**********************/
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    src=imread("D:/heads/head1.jpg");
    showImge(src);
}

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

void MainWindow::showImge(Mat src)
{
    Mat rgb;
    //色彩空间转换
    cvtColor (src,rgb,CV_BGR2RGB);
    //Mat-->QImage
    QImage img(rgb.data,rgb.cols,rgb.rows,rgb.cols*rgb.channels (),QImage::Format_RGB888);
    //QImage-->Pixmap
    QPixmap p=QPixmap::fromImage (img);
    //显示
    ui->label->setPixmap (p);

}

void MainWindow::on_pushButton_clicked()
{
    QString re=ui->lineEdit->text ();
    int a=re.toInt ();
    Mat dat=Mat::zeros (src.size(),src.type ());
    for(int i=0;i<src.cols;i++){
        for(int j=0;j<src.rows;j++){
            dat.at<Vec3b>(Point(i,j))[0]=
                    saturate_cast<uchar>(src.at<Vec3b>(Point(i,j))[0]+a);
            dat.at<Vec3b>(Point(i,j))[1]=
                    saturate_cast<uchar>(src.at<Vec3b>(Point(i,j))[1]+a);
            dat.at<Vec3b>(Point(i,j))[2]=
                    saturate_cast<uchar>(src.at<Vec3b>(Point(i,j))[2]+a);
        }
    }
    showImge (dat);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值