-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
410 lines (382 loc) · 13.7 KB
/
Copy pathmainwindow.cpp
File metadata and controls
410 lines (382 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QTimer.h"
#include <QFileDialog>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
VideoCapture capture;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->label->setText("welcome!");
image.load("./vedioProcess.jpg");
ui->label->resize(image.width(),image.height());
ui->label->setPixmap(QPixmap::fromImage(image));
ui->pushButton_backward->setFixedSize(30,30);
ui->pushButton_forward->setFixedSize(30,30);
ui->pushButton_pause->setFixedSize(60,30);
ui->pushButton_detect->setFixedSize(120,30);
ui->pushButton_noDetect->setFixedSize(120,30);
ui->pushButton_backward->setVisible(false);
ui->pushButton_forward->setVisible(false);
ui->pushButton_pause->setVisible(false);
ui->pushButton_detect->setVisible(false);
ui->pushButton_noDetect->setVisible(false);
ui->horizontalSlider->setVisible(false);
ui->horizontalSlider->setMaximum(1000000);
ui->horizontalSlider->setMinimum(0);
ui->horizontalSlider->setSingleStep(1);
ui->now->setVisible(false);
ui->end->setVisible(false);
timer = new QTimer(this);
this->setWindowTitle("Vedio Processor");
}
MainWindow::~MainWindow()
{
delete ui;
}
QImage Mat2QImage(cv::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),
cvImg.cols, cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_RGB888);
}
else if(cvImg.channels()==1) //grayscale image
{
qImg =QImage((const unsigned char*)(cvImg.data),
cvImg.cols,cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_Indexed8);
}
else
{
qImg =QImage((const unsigned char*)(cvImg.data),
cvImg.cols,cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_RGB888);
}
return qImg;
}
QString MainWindow::formatTime(int ms)
{
int ss = 1000;
int mi = ss * 60;
int hh = mi * 60;
int dd = hh * 24;
long day = ms / dd;
long hour = (ms - day * dd) / hh;
long minute = (ms - day * dd - hour * hh) / mi;
long second = (ms - day * dd - hour * hh - minute * mi) / ss;
long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
QString hou = QString::number(hour,10);
QString min = QString::number(minute,10);
QString sec = QString::number(second,10);
QString msec = QString::number(milliSecond,10);
//qDebug() << "minute:" << min << "second" << sec << "ms" << msec <<endl;
return hou + ":" + min + ":" + sec ;
}
void MainWindow::nextFrame()
{
capture >> frame;
now=capture.get(CV_CAP_PROP_POS_MSEC);
ui->now->setText(formatTime(now));
ui->horizontalSlider->setValue(now/end*1000000);
//ui->now->setText(QString::number(ui->horizontalSlider->value()));
if (!frame.empty())
{
image = Mat2QImage(frame);
ui->label->setPixmap(QPixmap::fromImage(image));
//this->update();
}
}
void MainWindow::detectFrame()
{
CascadeClassifier faceDetector;
string faceCascadeFilename = "haarcascade_frontalface_default.xml";
//友好错误信息提示
try{
faceDetector.load(faceCascadeFilename);
}
catch (cv::Exception e){}
if (faceDetector.empty())
{
cerr << "脸部检测器不能加载 (";
cerr << faceCascadeFilename << ")!" << endl;
exit(1);
}
capture >> frame;
Mat frameBGR;
cvtColor(frame,frameBGR,CV_RGB2BGR);
if (!frame.empty())
{
Mat gray;
cvtColor(frame,gray,CV_BGR2GRAY);
//直方图均匀化(改善图像的对比度和亮度)
Mat equalizedImg;
equalizeHist(gray, equalizedImg);
int flags = CASCADE_FIND_BIGGEST_OBJECT|CASCADE_DO_ROUGH_SEARCH; //只检测脸最大的人
//int flags = CASCADE_SCALE_IMAGE; //检测多个人
Size minFeatureSize(50, 50);
float searchScaleFactor = 1.1f;
int minNeighbors = 4;
vector<Rect> faces;
faceDetector.detectMultiScale(equalizedImg, faces, searchScaleFactor, minNeighbors, flags, minFeatureSize);
//画矩形框
for (size_t i = 0; i < faces.size(); i++)
{
if (faces[i].height > 0 && faces[i].width > 0)
{
//cout<<"("<<faces[i].x<<","<<faces[i].y<<")"<<endl;
rectangle(equalizedImg, faces[i], cv::Scalar(255, 0, 0), 1, 8, 0);
rectangle(gray, faces[i], cv::Scalar(255, 0, 0), 1, 8, 0);
rectangle(frame, faces[i], cv::Scalar(255, 0, 0), 1, 8, 0);
}
}
//rectangle (frame, Point(10,50), Point(70, 120), Scalar(0, 255, 255), 1, 8, 0);
image = Mat2QImage(frame);
ui->label->setPixmap(QPixmap::fromImage(image));
//this->update();
}
//delete faceDetector;
}
void MainWindow::writeNextFrame()
{
Mat frameBGR;
cvtColor(frame,frameBGR,CV_RGB2BGR);
writer.write(frameBGR);
}
void MainWindow::on_pushButton_clicked()
{
if (capture.isOpened())
capture.release(); //decide if capture is already opened; if so,close it
delete timer;
QString filename =QFileDialog::getOpenFileName(this,tr("Open Video File"),".",tr("Video Files(*.avi *.mp4 *.flv *.mkv)"));
capture.open(filename.toLocal8Bit().data());
if (capture.isOpened())
{
ui->pushButton_backward->setVisible(true);
ui->pushButton_forward->setVisible(true);
ui->pushButton_pause->setVisible(true);
ui->horizontalSlider->setVisible(true);
ui->pushButton_detect->setVisible(false);
ui->pushButton_noDetect->setVisible(false);
ui->now->setVisible(true);
ui->end->setVisible(true);
rate= capture.get(CV_CAP_PROP_FPS);
fcount=capture.get(CV_CAP_PROP_FRAME_COUNT);
end=1000/rate*fcount;
ui->end->setText(formatTime(end));
ui->now->setText(formatTime(0));
capture >> frame;
if (!frame.empty())
{
image = Mat2QImage(frame);
ui->label->resize(image.width(),image.height());
ui->label->setPixmap(QPixmap::fromImage(image));
timer = new QTimer(this);
timer->setInterval(1000/rate); //set timer match with FPS
connect(timer, SIGNAL(timeout()), this, SLOT(nextFrame()));
timer->start();
}
}
}
void MainWindow::on_pushButton_2_clicked()
{
if (capture.isOpened())
capture.release(); //decide if capture is already opened; if so,close it
capture.open(0); //open the default camera
//打开摄像头
delete timer;
if (capture.isOpened())
{
ui->pushButton_backward->setVisible(false);
ui->pushButton_forward->setVisible(false);
ui->pushButton_pause->setVisible(false);
ui->horizontalSlider->setVisible(false);
ui->now->setVisible(false);
ui->end->setVisible(false);
ui->pushButton_detect->setVisible(true);
ui->pushButton_noDetect->setVisible(true);
capture >> frame;
if (!frame.empty())
{
rate=24.0;
image = Mat2QImage(frame);
ui->label->resize(image.width(),image.height());
ui->label->setPixmap(QPixmap::fromImage(image));
timer = new QTimer(this);
timer->setInterval(1000/rate); //set timer match with FPS
connect(timer, SIGNAL(timeout()), this, SLOT(nextFrame()));
timer->start();
}
}
}
void MainWindow::on_pushButton_6_clicked()
{
this->close();
}
void MainWindow::on_pushButton_3_clicked()
{
cv::Mat cannyImg ;
cv::Canny(frame, cannyImg, 50, 80, 3);
cv::namedWindow("Canny");
cv::imshow("Canny", cannyImg);
}
void MainWindow::on_pushButton_4_clicked()
{
writer.open("./myrec.avi",CV_FOURCC('M', 'J', 'P', 'G') ,rate, cv::Size(frame.cols, frame.rows),true);
ui->pushButton_4->setDisabled(true); //if successfully start videoWriter, disable the button
if (!frame.empty())
{
timer = new QTimer(this);
timer->setInterval(1000/rate); //set timer match with FPS
connect(timer, SIGNAL(timeout()), this, SLOT(writeNextFrame()));
timer->start();
}
}
void MainWindow::on_pushButton_5_clicked()
{
writer.release();
ui->pushButton_4->setDisabled(false);
}
void MainWindow::on_pushButton_pause_clicked()
{
if (ui->pushButton_pause->text()==QString("pause"))
{
timer->stop();
ui->pushButton_pause->setText("continue");
}
else
{
timer->start();
ui->pushButton_pause->setText("pause");
}
}
void MainWindow::on_horizontalSlider_sliderMoved(int position)
{
double resetTime=position/double(1000000)*end;
ui->horizontalSlider->setValue(position);
capture.set(CV_CAP_PROP_POS_MSEC,resetTime);
ui->now->setText(formatTime(resetTime));
}
void MainWindow::on_horizontalSlider_sliderReleased()
{
timer->start();
}
void MainWindow::on_horizontalSlider_sliderPressed()
{
timer->stop();
}
void MainWindow::on_pushButton_forward_clicked()
{
double resetTime=capture.get(CV_CAP_PROP_POS_MSEC)+5000;
capture.set(CV_CAP_PROP_POS_MSEC,resetTime);
ui->now->setText(formatTime(resetTime));
ui->horizontalSlider->setValue(resetTime/end*1000000);
}
void MainWindow::on_pushButton_backward_clicked()
{
double resetTime=capture.get(CV_CAP_PROP_POS_MSEC)-5000;
capture.set(CV_CAP_PROP_POS_MSEC,resetTime);
ui->now->setText(formatTime(resetTime));
ui->horizontalSlider->setValue(resetTime/end*1000000);
}
void MainWindow::on_pushButton_detect_clicked()
{
delete timer;
if (capture.isOpened())
{
ui->pushButton_backward->setVisible(false);
ui->pushButton_forward->setVisible(false);
ui->pushButton_pause->setVisible(false);
ui->horizontalSlider->setVisible(false);
ui->now->setVisible(false);
ui->end->setVisible(false);
ui->pushButton_detect->setVisible(true);
ui->pushButton_noDetect->setVisible(true);
CascadeClassifier faceDetector;
string faceCascadeFilename = "haarcascade_frontalface_default.xml";
//友好错误信息提示
try{
faceDetector.load(faceCascadeFilename);
}
catch (cv::Exception e){}
if (faceDetector.empty())
{
cerr << "脸部检测器不能加载 (";
cerr << faceCascadeFilename << ")!" << endl;
exit(1);
}
rate= 24.0;
capture >> frame;
if (!frame.empty())
{
Mat gray;
cvtColor(frame,gray,CV_BGR2GRAY);
//直方图均匀化(改善图像的对比度和亮度)
Mat equalizedImg;
equalizeHist(gray, equalizedImg);
int flags = CASCADE_FIND_BIGGEST_OBJECT|CASCADE_DO_ROUGH_SEARCH; //只检测脸最大的人
//int flags = CASCADE_SCALE_IMAGE; //检测多个人
Size minFeatureSize(30, 30);
float searchScaleFactor = 1.1f;
int minNeighbors = 4;
std::vector<Rect> faces;
faceDetector.detectMultiScale(equalizedImg, faces, searchScaleFactor, minNeighbors, flags, minFeatureSize);
//画矩形框
cv::Point text_lb;
for (size_t i = 0; i < faces.size(); i++)
{
if (faces[i].height > 0 && faces[i].width > 0)
{
text_lb = cv::Point(faces[i].x, faces[i].y);
cv::rectangle(frame, faces[i], cv::Scalar(255, 0, 0), 1, 8, 0);
}
}
image = Mat2QImage(frame);
ui->label->resize(image.width(),image.height());
ui->label->setPixmap(QPixmap::fromImage(image));
timer = new QTimer(this);
timer->setInterval(1000/rate); //set timer match with FPS
connect(timer, SIGNAL(timeout()), this, SLOT(detectFrame()));
timer->start();
}
}
//delete faceDetector;
}
void MainWindow::on_pushButton_noDetect_clicked()
{
delete timer;
if (capture.isOpened())
{
ui->pushButton_backward->setVisible(false);
ui->pushButton_forward->setVisible(false);
ui->pushButton_pause->setVisible(false);
ui->horizontalSlider->setVisible(false);
ui->now->setVisible(false);
ui->end->setVisible(false);
ui->pushButton_detect->setVisible(true);
ui->pushButton_noDetect->setVisible(true);
rate= 24.0;
capture >> frame;
if (!frame.empty())
{
image = Mat2QImage(frame);
ui->label->resize(image.width(),image.height());
ui->label->setPixmap(QPixmap::fromImage(image));
timer = new QTimer(this);
timer->setInterval(1000/rate); //set timer match with FPS
connect(timer, SIGNAL(timeout()), this, SLOT(nextFrame()));
timer->start();
}
}
}