一。预备知识:定时器事件
如何 使用定时器呢?引什么文件?如何开始?如何关闭?
/**************************头文件**************************/
#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);
}