This repository has been archived by the owner on Feb 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_data.cpp
196 lines (169 loc) · 6.27 KB
/
get_data.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
#include "utils.h"
using namespace std;
using namespace cv;
void mouse_callback( int event, int x, int y, int, void*);
bool drawing_box = false;
bool ready_to_process = true;
bool flag_save = false;
bool flag_stop = true;
bool flag_dragging = false;
bool flag_lines = true;
bool flag_dots = true;
int index_dragged_point = 0;
int snapshot_number = 1;
VideoCapture cap;
Mat frame;
Mat temp;
std::vector< cv::Point2f > corners;
int main(int argc, char** argv)
{
Mat triangles = load_triangles();
cap.open(-1);
if(!cap.isOpened())
{
std::cout << "Błąd kamery.";
return 1;
}
Mat shapes = load_2D_mat("data/shapes.txt");
Mat reference_shape = load_2D_mat("data/reference_shape.txt");
for(int i = 0; i < shapes.cols/2; i++) corners.push_back(Point2f(shapes.at<double>(snapshot_number-1, 2*i), shapes.at<double>(snapshot_number-1, 2*i+1)));
flag_stop = true;
stringstream ss;
ss<<"images/"<<snapshot_number<<".jpg";
temp = imread(ss.str(), CV_LOAD_IMAGE_COLOR);
const string window_name = "Create Model";
namedWindow(window_name, CV_WINDOW_AUTOSIZE);
setMouseCallback(window_name, mouse_callback);
while(true)
{
if(flag_save)
{
flag_save = false;
stringstream ss;
ss<<"images/"<<snapshot_number<<".jpg";
imwrite(ss.str(), temp);
for(int i = 0; i < shapes.cols/2; i++)
{
shapes.at<double>(snapshot_number-1, 2*i) = corners.at(i).x;
shapes.at<double>(snapshot_number-1, 2*i + 1) = corners.at(i).y;
}
save_2D_mat("data/shapes.txt", shapes);
}
if(!flag_stop)
{
cap.read(frame);
temp = frame.clone();
}
else
{
frame = temp.clone();
}
// imshow(window_name, temp);
// DISPLAY FACE TRIANGULATION
if(flag_dots) for(unsigned int r = 0; r < corners.size(); r++) circle(frame, corners[r], 1, Scalar(100,255,255), 1);
if(flag_lines) for(int i = 0; i < triangles.rows; i++)
{
line(frame, corners[triangles.at<int>(i,0)], corners[triangles.at<int>(i,1)], Scalar(255,255,255), 1, 4);
line(frame, corners[triangles.at<int>(i,1)], corners[triangles.at<int>(i,2)], Scalar(255,255,255), 1, 4);
line(frame, corners[triangles.at<int>(i,2)], corners[triangles.at<int>(i,0)], Scalar(255,255,255), 1, 4);
}
stringstream ss;
ss<<snapshot_number<<"/"<<shapes.rows;
putText(frame, ss.str(), Point(0, 450), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255,255,255), 2);
imshow(window_name, frame);
// CONTROL SECTION
int c;
c = cvWaitKey(30);
if( (char) c == 27) break;
if( (char) c == 's' ) flag_save = true;
if( (char) c == 'c') {flag_stop = !flag_stop;}
if( (char) c == 'i')
{
for(int i = 0; i < corners.size(); i++)
{
double tx = corners[i].x - 320;
double ty = corners[i].y - 240;
corners[i].x = floor(((0.96) * tx + 0.28 * ty) + 320);
corners[i].y = floor(((-0.28) * tx + 0.96 * ty) + 240);
}
}
if( (char) c == 'u')
{
for(int i = 0; i < corners.size(); i++)
{
double tx = corners[i].x - 320;
double ty = corners[i].y - 240;
corners[i].x = floor(((0.96) * tx + (-0.28) * ty) + 320);
corners[i].y = floor((0.28 * tx + 0.96 * ty) + 240);
}
}
if( (char) c == 'j') for(int i = 0; i < corners.size(); i++) corners[i].y = corners[i].y + 5;
if( (char) c == 'k') for(int i = 0; i < corners.size(); i++) corners[i].y = corners[i].y - 5;
if( (char) c == 'l') for(int i = 0; i < corners.size(); i++) corners[i].x = corners[i].x + 5;
if( (char) c == 'h') for(int i = 0; i < corners.size(); i++) corners[i].x = corners[i].x - 5;
if( (char) c == '>' && snapshot_number < shapes.rows)
{
snapshot_number++;
corners.clear();
for(int i = 0; i < 58; i++) corners.push_back(Point2f(shapes.at<double>(snapshot_number-1, 2*i), shapes.at<double>(snapshot_number-1, 2*i+1)));
flag_stop = true;
stringstream ss;
ss<<"images/"<<snapshot_number<<".jpg";
temp = imread(ss.str(), CV_LOAD_IMAGE_COLOR);
}
if( (char) c == '<' && snapshot_number > 1)
{
snapshot_number--;
corners.clear();
for(int i = 0; i < 58; i++) corners.push_back(Point2f(shapes.at<double>(snapshot_number-1, 2*i), shapes.at<double>(snapshot_number-1, 2*i+1)));
flag_stop = true;
stringstream ss;
ss<<"images/"<<snapshot_number<<".jpg";
temp = imread(ss.str(), CV_LOAD_IMAGE_COLOR);
}
if( (char) c == '+')
{
Mat new_row = Mat::zeros(1, shapes.cols, CV_64F);
shapes.push_back(new_row);
snapshot_number = shapes.rows;
flag_save = true;
flag_stop = true;
}
if( (char) c == 'e')
{
Mat ycrcb;
cvtColor(temp,ycrcb,CV_BGR2YCrCb);
vector<Mat> channels;
split(ycrcb,channels);
equalizeHist(channels[0], channels[0]);
merge(channels,ycrcb);
cvtColor(ycrcb,temp,CV_YCrCb2BGR);
}
if( (char) c == 'h') for(int i = 0; i < corners.size(); i++) corners[i].x = corners[i].x - 5;
if( (char) c == 'z') flag_lines = !flag_lines;
if( (char) c == 'x') flag_dots = !flag_dots;
}
// EXIT
return 0;
}
// MOVE POINTS
void mouse_callback(int event, int x, int y, int, void*)
{
switch( event )
{
case CV_EVENT_MOUSEMOVE:
if(flag_dragging)
{
corners[index_dragged_point].x = x;
corners[index_dragged_point].y = y;
}
break;
case CV_EVENT_LBUTTONDOWN:
index_dragged_point = find_nearest_point(corners, Point2f(x,y));
if (index_dragged_point < corners.size()) flag_dragging = true;
break;
case CV_EVENT_LBUTTONUP:
flag_dragging = false;
break;
}
}