-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
431 lines (376 loc) · 9.83 KB
/
Copy pathmain.cpp
File metadata and controls
431 lines (376 loc) · 9.83 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include "src/hello.h"
#include "src/tps.h"
#include "src/interpolation.h"
#include "src/Matrix.h"
#include "src/bsplines.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <fstream>
#include <string>
#include "dlib/image_processing/frontal_face_detector.h"
#include "dlib/image_processing/render_face_detections.h"
#include "dlib/image_processing.h"
#include "dlib/gui_widgets.h"
#include "dlib/image_io.h"
using namespace dlib;
using namespace std;
using namespace cv;
Mat Clip(Mat original_img);
void landmark_detect(string img_path,string file_path);
int main( int argc, char **argv)
{
//HelloFunc();
// input the basic info of the problem
string target_num;
cout<<"please input target_num, which means you will transform this picture"<<endl;
while(1)
{
cin>>target_num;
if(target_num>"0"&&target_num<":"&&target_num.length()==1)
{
break;
}
else
{
cout<<"no picture found, please choose again"<<endl;
cout<<"you can choose 1 ~ 9"<<endl;
}
}
string control_num;
cout<<"please input control_num, which means you will use the structure of this picture"<<endl;
while(1)
{
cin>>control_num;
if(control_num>"0"&&control_num<":"&&control_num.length()==1)
{
break;
}
else
{
cout<<"no picture found, please choose again"<<endl;
cout<<"you can choose 1 ~ 9"<<endl;
}
}
string method_name;
cout<<"please input the method to mix the faces"<<endl;
cout<<"you can choose TPS or B_Splines"<<endl;
while(1)
{
cin>>method_name;
if(method_name=="TPS"||method_name=="B_Splines")
{
break;
}
else
{
cout<<"no method found, please choose again"<<endl;
cout<<"you can choose TPS or B_Splines"<<endl;
}
}
string interpotation_name;
cout<<"please input the interpotation method"<<endl;
cout<<"you can choose nearest, bilinear or bicubic"<<endl;
while(1)
{
cin>>interpotation_name;
if(interpotation_name=="nearest"||interpotation_name=="bilinear"||interpotation_name=="bicubic")
{
break;
}
else
{
cout<<"no method found, please choose again"<<endl;
cout<<"you can choose nearest, bilinear or bicubic"<<endl;
}
}
//string target_num="6";
//string control_num="8";
//string method_name="B_Splines";
string control_file_path="../picture/"+control_num+".txt";
string control_img_path="../picture/"+control_num+".jpg";
string target_file_path="../picture/"+target_num+".txt";
string target_img_path="../picture/"+target_num+".jpg";
landmark_detect(control_img_path,control_file_path);
landmark_detect(target_img_path,target_file_path);
// input control info
cout<<"opening control_file from "<<control_file_path<<endl;
FILE *control_file;
control_file=fopen(control_file_path.c_str(),"rt+");
double control_point[68][2];
for(int i=0;i<68;i++)
{
fscanf(control_file,"%lf",&control_point[i][1]);
fscanf(control_file,"%lf",&control_point[i][0]);
}
fclose(control_file);
cout<<"opening control_img from "<<control_img_path<<endl;
Mat img_control = imread(control_img_path.c_str());
if( img_control.empty() )
{
cout<<"control img is empty!"<<endl;
return 0;
}
// input target info
cout<<"opening target_file from "<<target_file_path<<endl;
FILE *target_file;
target_file=fopen(target_file_path.c_str(),"rt+");
double target_point[68][2];
for(int i=0;i<68;i++)
{
fscanf(target_file,"%lf",&target_point[i][1]);
fscanf(target_file,"%lf",&target_point[i][0]);
}
fclose(target_file);
cout<<"opening target_img from "<<target_img_path<<endl;
Mat img = imread(target_img_path.c_str());
if( img.empty() )
{
cout<<"target img is empty!"<<endl;
return 0;
}
// regulate the control_point with the target_point
double control_point_regulate[68][2];
for(int i=0;i<68;i++)
{
control_point_regulate[i][0] = control_point[i][0];
control_point_regulate[i][1] = control_point[i][1];
}
FaceScale(target_point,control_point_regulate);
// using TPS / B_Splines method to calculate the coresponding coordinates of each point in converted pictures
int width=img.cols;
int height=img.rows;
double **newx = new double*[height];
for (int i = 0; i < height; i++)
newx[i] = new double[width];
double **newy = new double*[height];
for (int i = 0; i < height; i++)
newy[i] = new double[width];
if(method_name=="B_Splines")
{
//using B_Splines method
B_Splines(target_point,control_point_regulate, newx, newy,height,width);
}
else
{
// using TPS method
TPS(target_point,control_point_regulate, newx, newy,height,width);
}
// create the converted picture
cout<<"begin to create the converted picture"<<endl;
Mat img_convert(img.rows,img.cols,CV_8UC3,Scalar::all(1));
double value_point[2];
double value[3];
for(int i=0;i<img.rows;i++)
for(int j=0;j<img.cols;j++)
{
// calculate the corresponding double point in original picture
value_point[0]=newx[i][j];
value_point[1]=newy[i][j];
// calculate the value of the double point
if(interpotation_name=="nearest")
InterpolateNearest(value_point,value,img);
else if(interpotation_name=="bicubic")
InterpolateBicubic(value_point,value,img);
else
InterpolateBilinear(value_point,value,img);
// build the converted picture
img_convert.at<Vec3b>(i,j)[0]=value[0];
img_convert.at<Vec3b>(i,j)[1]=value[1];
img_convert.at<Vec3b>(i,j)[2]=value[2];
}
// mark the control points in pictures
for(int i=0;i<68;i++)
{
for(int m=-1;m<1;m++)
for(int n=-1;n<1;n++)
img.at<Vec3b>((int)target_point[i][0]+n,(int)target_point[i][1]+m)=0;
}
for(int i=0;i<68;i++)
{
for(int m=-1;m<1;m++)
for(int n=-1;n<1;n++)
img_control.at<Vec3b>((int)control_point[i][0]+n,(int)control_point[i][1]+m)=255;
}
// clip the converted picture
Mat img_new=Clip(img_convert);
// display the pictures
cout<<"display the original picture"<<endl;
imshow("Original",img);
cout<<"display the controling picture"<<endl;
imshow("Controling",img_control);
cout<<"display the mixed picture"<<endl;
imshow("Mixed",img_convert);
cout<<"display the clipped picture"<<endl;
imshow("Clipped",img_new);
waitKey();
return 0;
}
Mat Clip(Mat img_convert)
{
int width=img_convert.cols;
int height=img_convert.rows;
int up,down,left,right;
up=1;
down=height-2;
left=1;
right=width-2;
double threshold=0.5;
//calculating the border
cout<<"begin to clip"<<endl;
for(int i=0;i<height;i++)
{
int num=0;
for(int j=0;j<width;j++)
{
double add=img_convert.at<Vec3b>(i,j)[0]+img_convert.at<Vec3b>(i,j)[1]+img_convert.at<Vec3b>(i,j)[2];
if(add==0)
{
num++;
}
}
if(num>(int)(width*threshold))
{
up=i;
}
else
{
break;
}
}
cout<<"up = "<<up<<endl;
for(int i=height-1;i>-1;i--)
{
int num=0;
for(int j=0;j<width;j++)
{
double add=img_convert.at<Vec3b>(i,j)[0]+img_convert.at<Vec3b>(i,j)[1]+img_convert.at<Vec3b>(i,j)[2];
if(add==0)
{
num++;
}
}
if(num>(int)(width*threshold))
{
down=i;
}
else
{
break;
}
}
cout<<"down = "<<down<<endl;
for(int j=0;j<width;j++)
{
int num=0;
for(int i=0;i<height;i++)
{
double add=img_convert.at<Vec3b>(i,j)[0]+img_convert.at<Vec3b>(i,j)[1]+img_convert.at<Vec3b>(i,j)[2];
if(add==0)
{
num++;
}
}
if(num>(int)(height*threshold))
{
left=j;
}
else
{
break;
}
}
cout<<"left = "<<left<<endl;
for(int j=width-1;j>-1;j--)
{
int num=0;
for(int i=0;i<height;i++)
{
double add=img_convert.at<Vec3b>(i,j)[0]+img_convert.at<Vec3b>(i,j)[1]+img_convert.at<Vec3b>(i,j)[2];
if(add==0)
{
num++;
}
}
if(num>(int)(height*threshold))
{
right=j;
}
else
{
break;
}
}
cout<<"right = "<<right<<endl;
// draw the border
for(int i=1;i<height-1;i++)
{
for(int m=-1;m<1;m++)
for(int n=-1;n<1;n++)
{
img_convert.at<Vec3b>(i+n,left+m)=255;
img_convert.at<Vec3b>(i+n,right+m)=255;
}
}
for(int j=1;j<width-1;j++)
{
for(int m=-1;m<1;m++)
for(int n=-1;n<1;n++)
{
img_convert.at<Vec3b>(up+n,j+m)=255;
img_convert.at<Vec3b>(down+n,j+m)=255;
}
}
// create a new clipped picture
int new_height=down - up-2;
int new_width = right -left-2;
Mat new_img(new_height,new_width,CV_8UC3,Scalar::all(1));
for(int i=0;i<new_height;i++)
for(int j=0;j<new_width;j++)
{
new_img.at<Vec3b>(i,j)=img_convert.at<Vec3b>(i+up+1,j+left+1);
}
cout<<"clip finished"<<endl;
return new_img;
}
void landmark_detect(string img_path,string file_path)
{
try
{
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor sp;
deserialize("../shape_predictor_68_face_landmarks.dat") >> sp;
image_window win, win_faces;
cout << "processing image " << img_path << endl;
array2d<rgb_pixel> img;
load_image(img, img_path);
std::vector<dlib::rectangle> dets = detector(img);
cout << "Number of faces detected: " << dets.size() << endl;
std::vector<full_object_detection> shapes;
if(dets.size()!=1)
{
cout<<"detecting no or more than one face!!!"<<endl;
return;
}
for (unsigned long j = 0; j < dets.size(); ++j)
{
full_object_detection shape = sp(img, dets[j]);
ofstream fout;
fout.open(file_path);
if (fout) { // 如果创建成功
for (int i = 0; i < shape.num_parts(); i++)
{
fout <<shape.part(i).x()<<" "<<shape.part(i).y()<< endl; // 使用与cout同样的方式进行写入
}
fout.close(); // 执行完操作后关闭文件句柄
}
shapes.push_back(shape);
}
cout<<"detectiong finished"<<endl;
}
catch (exception& e)
{
cout << "\nexception thrown!" << endl;
cout << e.what() << endl;
}
}
// ----------------------------------------------------------------------------------------