-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmainwindow.cpp
341 lines (268 loc) · 9.12 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "photo_frame.h"
using namespace std;
using namespace cv;
Mat image[100];
int currentstep=0;
String face_cascade_name = "C://opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "C://opencv/sources/data/lbpcascades/haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
//Photo_frame.setEnabled(false);
ui->setupUi(this);
this->setWindowTitle(tr("Tool Box"));
//image[0]=imread("D://Photos/BB8/BB8.jpg");
//image[0]=imread("D://Photos/BB8/15609246_1219648224796806_584233906_o.jpg");
//cvtColor(image,image,CV_RGB2BGR);
//imshow("new",image[0]);
//-- 1. Load the cascade
if(!face_cascade.load("C:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml") )
ui->label_2->setText("--(!)Error loading face cascade\n");
if(!eyes_cascade.load("C:/opencv/sources/data/haarcascades/haarcascade_eye_tree_eyeglasses.xml") )
ui->label_2->setText("--(!)Error loading eyes cascade\n");
ui->brightness_slider->setMinimum(-10);
ui->brightness_slider->setMaximum(10);
ui->contrast_slider->setMinimum(-10);
ui->contrast_slider->setMaximum(10);
ui->brightness_slider->setValue(0);
ui->contrast_slider->setValue(0);
myTimer = new QTimer(this);
myTimer->start(500); //以1000毫秒為週期起動定時器
connect(myTimer,SIGNAL(timeout()),this,SLOT(un_redo()));
}
MainWindow::~MainWindow()
{
QMessageBox message(QMessageBox::NoIcon, "Save", "Do you want to save photo?", QMessageBox::Yes | QMessageBox::No, NULL);
if(message.exec() == QMessageBox::Yes)
{
if(!image[currentstep].empty()){
QString Simgpath=QFileDialog::getSaveFileName(this, tr("Save File"),"/",tr("PNG(*.png);;JPG(*.jpg)"));
if(!Simgpath.isNull()){
imwrite(Simgpath.toStdString(),image[currentstep]);
}
}
}
for(int i=0;i<100;i++){
image[i].release();
}
delete ui;
}
void MainWindow::photo_window_update(Mat frame,int r){
static photo_frame P_frame;
P_frame.frame_update(frame);
P_frame.show();
if(r==1){
P_frame.frame_load(frame);
}
else if(r==0){
P_frame.frame_update(frame);
}
}
void MainWindow::on_blur_btn_clicked()
{
GaussianBlur(image[currentstep],image[currentstep+1],Size(9,9),0,0);
currentstep++;
photo_window_update(image[currentstep],0);
un_redo();
}
void MainWindow::un_redo(){
if(image[currentstep].empty()){
ui->blur_btn->setEnabled(false);
ui->brightness_slider->setEnabled(false);
ui->contrast_slider->setEnabled(false);
ui->magicbox->setEnabled(false);
ui->magic_set_btn->setEnabled(false);
ui->set_bright_btn->setEnabled(false);
ui->set_con_btn->setEnabled(false);
ui->shapen_btn->setEnabled(false);
}
else{
ui->blur_btn->setEnabled(true);
ui->brightness_slider->setEnabled(true);
ui->contrast_slider->setEnabled(true);
ui->magicbox->setEnabled(true);
ui->magic_set_btn->setEnabled(true);
ui->set_bright_btn->setEnabled(true);
ui->set_con_btn->setEnabled(true);
ui->shapen_btn->setEnabled(true);
}
if(currentstep>0)
ui->actionUndo->setEnabled(true);
else
ui->actionUndo->setEnabled(false);
if(!image[currentstep+1].empty())
ui->actionRedo->setEnabled(true);
else
ui->actionRedo->setEnabled(false);
ui->label->setText("Step: "+QString::number(currentstep));
}
void MainWindow::on_actionOpen_File_triggered()
{
QString filename=QFileDialog::getOpenFileName(this,tr("Select an Image"),"/home","Images(*.png *.bmp *.jpg)");
if(!filename.isNull()){
for(int i=0;i<100;i++){
image[i].release();
}
image[0]=imread(filename.toStdString());
currentstep=0;
photo_window_update(image[currentstep],1);
un_redo();
}
}
void MainWindow::on_actionUndo_triggered()
{
//image[0]=imread("D://Photos/BB8/15609246_1219648224796806_584233906_o.jpg");
currentstep--;
un_redo();
photo_window_update(image[currentstep],0);
}
void MainWindow::on_actionRedo_triggered()
{
//image[0]=imread("D://Photos/BB8/BB8.jpg");
currentstep++;
un_redo();
photo_window_update(image[currentstep],0);
}
void MainWindow::on_actionSave_File_triggered()
{
if(image[currentstep].empty()){
QMessageBox msgBox;
msgBox.setText("No flie loaded");
msgBox.exec();
}
else{
QString Simgpath=QFileDialog::getSaveFileName(this, tr("Save File"),"/",tr("PNG(*.png);;JPG(*.jpg)"));
if(!Simgpath.isNull()){
imwrite(Simgpath.toStdString(),image[currentstep]);
}
}
}
void MainWindow::on_shapen_btn_clicked()
{
Mat kernel = (Mat_<float>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(image[currentstep], image[currentstep+1], image[currentstep].depth(), kernel);
//Laplacian(image[currentstep],image[currentstep+1],image[currentstep].type());
currentstep++;
photo_window_update(image[currentstep],0);
un_redo();
}
void MainWindow::on_magic_set_btn_clicked()
{
int index;
index= ui->magicbox->currentIndex();
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
switch(index){
case 0:
if(image[currentstep].channels()!=1){
cvtColor(image[currentstep],image[currentstep+1], COLOR_BGR2GRAY,1);
currentstep++;
}
break;
case 1:
if(image[currentstep].channels()==1){
equalizeHist(image[currentstep],image[currentstep+1]);
currentstep++;
}
else{
//msg=new QMessageBox(this);
QMessageBox::about(NULL,"Attention","Convert to gray image first.");
}
break;
case 2:
//求X方向梯度
Sobel( image[currentstep], grad_x, CV_16S, 1, 0, 3, 1, 1, BORDER_DEFAULT );
convertScaleAbs( grad_x, abs_grad_x );
//求Y方向梯度
Sobel( image[currentstep], grad_y, CV_16S, 0, 1, 3, 1, 1, BORDER_DEFAULT );
convertScaleAbs( grad_y, abs_grad_y );
//合併梯度(近似)
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, image[currentstep+1] );
currentstep++;
break;
case 3:
detectAndDisplay(image[0]);
break;
default:
break;
}
//ui->magic_set_btn->setText(QString::number(index));
photo_window_update(image[currentstep],0);
un_redo();
}
void MainWindow::on_contrast_slider_sliderReleased()
{
int value=ui->contrast_slider->value();
double alpha=double(value+10)/10;
int beta=0;
Mat preview;
image[currentstep].convertTo(preview, -1, alpha, beta);
photo_window_update(preview,0);
un_redo();
}
void MainWindow::on_brightness_slider_sliderReleased()
{
int value=ui->brightness_slider->value();
double alpha=1;
int beta=value*5;
Mat preview;
image[currentstep].convertTo(preview, -1, alpha, beta);
photo_window_update(preview,0);
un_redo();
}
void MainWindow::on_set_bright_btn_clicked()
{
int value=ui->brightness_slider->value();
double alpha=1;
int beta=value*5;
image[currentstep].convertTo(image[currentstep+1], -1, alpha, beta);
currentstep++;
photo_window_update(image[currentstep],0);
un_redo();
ui->brightness_slider->setValue(0);
}
void MainWindow::on_set_con_btn_clicked()
{
int value=ui->contrast_slider->value();
double alpha=double(value+10)/10;
int beta=0;
image[currentstep].convertTo(image[currentstep+1], -1, alpha, beta);
currentstep++;
photo_window_update(image[currentstep],0);
un_redo();
ui->contrast_slider->setValue(0);
}
void MainWindow::detectAndDisplay(Mat frame)
{
std::vector<Rect> faces;
Mat frame_gray;
cvtColor( frame, frame_gray, COLOR_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
//-- Detect faces
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(10, 10) );
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 );
Mat faceROI = frame_gray( faces[i] );
std::vector<Rect> eyes;
//-- In each face, detect eyes
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CASCADE_SCALE_IMAGE, Size(30, 30) );
for ( size_t j = 0; j < eyes.size(); j++ )
{
Point eye_center( faces[i].x + eyes[j].x + eyes[j].width/2, faces[i].y + eyes[j].y + eyes[j].height/2 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, eye_center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
}
}
//-- Show what you got
//imshow( "window_name", frame );
image[currentstep+1]=frame.clone();
currentstep++;
/*photo_window_update(image[currentstep],0);
un_redo();*/
}