-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.cpp
201 lines (147 loc) · 5.26 KB
/
utils.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
#include <utils.h>
int fsign(double d) {
if (fabs(d) < eps) {
return 0;
} else if (d > eps) {
return 1;
} else return -1;
}
double avg_vec3b(const cv::Vec3b &v) {
double avg = 0;
for (int i = 0; i < 3; i++) {
avg += v[i];
}
return avg / 3;
}
int check_pure(double c) {
if (c < 3) return -1;
if (c > 253) return 1;
return 0;
}
double diff_vec3b(const cv::Vec3b & v0, const cv::Vec3b & v1) {
double avg = 0;
for (int i = 0; i < 3; i++) {
avg += (v0[i] - v1[i]) * (v0[i] - v1[i]);
}
return sqrt(avg);
}
double m_metric_pixel(const cv::Mat & piece0, const cv::Mat & piece1, bool shift_flag, int idx) {
assert(piece0.rows == piece1.rows);
int x0 = piece0.cols - 1;
int x1 = 0;
if (shift_flag) {
merge_imgs(piece0, piece1, x0, x1, shift_flag);
}
double m_score = 0;
double avg_pixel_color0 = 0;
double avg_pixel_color1 = 0;
for (int y = 0; y < piece0.rows; y++) {
m_score += diff_vec3b(piece0.at<cv::Vec3b>(y, x0), piece1.at<cv::Vec3b>(y, x1));
avg_pixel_color0 += avg_vec3b(piece0.at<cv::Vec3b>(y, x0));
avg_pixel_color1 += avg_vec3b(piece1.at<cv::Vec3b>(y, x1));
}
avg_pixel_color0 /= piece0.rows;
avg_pixel_color1 /= piece0.rows;
m_score /= piece0.rows;
#ifdef DEBUG
printf("IdxP: %d, metric (%d, %d), avg0: %.3lf, avg1: %.3lf\n", idx, idx / 60, idx % 60, avg_pixel_color0, avg_pixel_color1);
#endif
if (abs(check_pure(avg_pixel_color0) + check_pure(avg_pixel_color1)) >= 1) {
return 3;
} else {
#ifdef DEBUG
cout << "pixel metric: " << m_score << endl;
#endif
return exp(-max(0.0, m_score / 20 - metric_bias_pixel));
}
}
cv::Mat merge_imgs( const cv::Mat & in_img0,
const cv::Mat & in_img1,
int & splice_x0,
int & splice_x1,
bool shift_flag) {
if (in_img0.empty()) return in_img1;
assert(in_img0.rows == in_img1.rows);
cv::Size out_img_size(in_img0.cols + in_img1.cols, in_img0.rows);
cv::Mat out_img = cv::Mat::zeros(out_img_size, CV_8UC3);
cv::Rect in_img0_roi(0, 0, in_img0.cols, in_img0.rows);
in_img0.copyTo(out_img(in_img0_roi));
// cout << in_img0.size() << endl;
// cout << in_img1.size() << endl;
if (shift_flag) {
const int color_thres = 200;
splice_x0 = 0;
splice_x1 = 0;
int block_h = 20;
int shift_x0 = 0;
int shift_x1 = 0;
int block_cnt = 0;
int out_width = 0;
for (int y = block_h; y < in_img1.rows - block_h; y += block_h) {
if (in_img0.cols > 0) {
int x0;
for (x0 = in_img0.cols - 1; x0 >= 0; x0--) {
const cv::Vec3b & color = in_img0.at<cv::Vec3b>(y, x0);
if (color[0] > color_thres && color[1] > color_thres && color[2] > color_thres) break;
}
x0++;
if (y == block_h) {
shift_x0 = x0;
} else {
shift_x0 = int(round(0.8 * shift_x0 + 0.2 * x0));
}
}
if (in_img1.cols > 0) {
int x1;
for (x1 = 0; x1 < in_img1.cols; x1++) {
const cv::Vec3b & color = in_img1.at<cv::Vec3b>(y, x1);
if (color[0] > color_thres && color[1] > color_thres && color[2] > color_thres) break;
}
if (y == block_h) {
shift_x1 = x1;
} else {
shift_x1 = int(round(0.8 * shift_x1 + 0.2 * x1));
}
}
splice_x0 += shift_x0 - 1;
splice_x1 += shift_x1;
block_cnt++;
block_h = min(block_h, in_img1.rows - y);
cv::Rect roi_src(shift_x1, y, in_img1.cols-shift_x1, block_h);
cv::Rect roi_dst(shift_x0, y, in_img1.cols-shift_x1, block_h);
out_width = max(out_width, shift_x0 + in_img1.cols-shift_x1);
// cout << roi_src << endl;
// cout << roi_dst << endl;
in_img1(roi_src).copyTo(out_img(roi_dst));
}
splice_x0 /= block_cnt;
splice_x1 /= block_cnt;
out_img = out_img(cv::Rect(0, 0, out_width, out_img.rows));
// cv::imshow("in0", in_img0);
// cv::imshow("in1", in_img1);
// cv::imshow("out", out_img);
// cv::waitKey();
} else {
cv::Rect in_img1_roi(in_img0.cols, 0, in_img1.cols, in_img1.rows);
in_img1.copyTo(out_img(in_img1_roi));
}
return out_img;
}
bool cross_seam(const cv::Rect & bbox, int seam_x) {
if (bbox.x < seam_x && bbox.x + bbox.width > seam_x) {
return true;
} else {
return false;
}
}
void print_timestamp() {
auto now = chrono::system_clock::now();
time_t cur_time = chrono::system_clock::to_time_t(now);
cout << endl << "Current timestamp: " << ctime(&cur_time) << endl;
}
bool valid_symbol(char ch) {
if (ch >= '0' && ch <= '9') return true;
if (ch >= 'A' && ch <= 'Z') return true;
if (ch >= 'a' && ch <= 'z') return true;
return false;
}