This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
sh_sc.h
305 lines (268 loc) · 8.89 KB
/
sh_sc.h
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
#ifndef SH_SC_H
#define SH_SC_H
#include "sh_ed.h"
// Content-Aware Image Resizing using Seam Carving
#if 0
typedef basic_matrix<double> float_image_type;
typedef basic_matrix<double> float_image_type;
// gradient image
inline float_image_type sh_sc_impl_gradient_image(image_type const& old_img, scoped_buffer<double*> const& templates, int n)
{
int mid = n >> 1;
int w = (int)old_img.width();
int h = (int)old_img.height();
float_image_type grad_img(w, h);
for (int px = -mid; px < w - mid; ++px)
{
for (int py = -mid; py < h - mid; ++py)
{
scoped_buffer<double> gs(0.0, templates.size());
double g = 0;
for (int i = 0; i < (int)templates.size(); ++i)
{
for (int k = 0; k < n * n; ++k)
{
int xx = px + k % n;
int yy = py + k / n;
if (xx < 0) xx = 0;
if (yy < 0) yy = 0;
if (xx > w - 1) xx = w - 1;
if (yy > h - 1) yy = h - 1;
gs[i] += old_img.at(xx, yy).gray() * templates[i][k];
}
g += fabs(gs[i]);
//g += gs[i] * gs[i];
}
//grad_img.at(px + mid, py + mid) = g;
//grad_img.at(px + mid, py + mid) = xtl_sqrt(g);
//grad_img.at(px + mid, py + mid) = g / (templates.size() * n * n) > 5? g : 0;
grad_img.at(px + mid, py + mid) = g > 256? 255 : g;
}
}
return grad_img;
}
inline scoped_buffer<double*> sh_sc_sobel()
{
static double s1[9] = { -1, -2, -1
, 0, 0, 0
, 1, 2, 1
};
static double s2[9] = { -1, 0, 1
, -2, 0, 2
, -1, 0, 1
};
/*static double s3[9] = { 0, -1, -2
, 1, 0, -1
, 2, 1, 0
};
static double s4[9] = { 0, 1, 2
, -1, 0, 1
, -2, -1, 0
};*/
scoped_buffer<double*> templates;
templates.push_back((double*)s1);
templates.push_back((double*)s2);
//templates.push_back((double*)s3);
//templates.push_back((double*)s4);
return templates;
}
inline scoped_buffer<double*> sh_sc_prewitt()
{
static double s1[9] = { -1, -1, -1
, 0, 0, 0
, 1, 1, 1
};
static double s2[9] = { -1, 0, 1
, -1, 0, 1
, -1, 0, 1
};
scoped_buffer<double*> templates;
templates.push_back((double*)s1);
templates.push_back((double*)s2);
return templates;
}
inline scoped_buffer<double*> sh_sc_isotropic()
{
static double s1[9] = { -1, -1.414213, -1
, 0, 0, 0
, 1, 1.414213, 1
};
static double s2[9] = { -1, 0, 1
, -1.414213, 0, 1.414213
, -1, 0, 1
};
scoped_buffer<double*> templates;
templates.push_back((double*)s1);
templates.push_back((double*)s2);
return templates;
}
inline float_image_type sh_sc_horizontal_energy(float_image_type const& grad_img)
{
float_image_type energy_img(grad_img);
for (int px = 1; px < (int)grad_img.row(); ++px)
{
for (int py = 0; py < (int)grad_img.col(); ++py)
{
if (py == 0) energy_img.at(px, py) += xtl_min(energy_img.at(px - 1, py), energy_img.at(px - 1, py + 1));
else if (py == (int)grad_img.col() - 1) energy_img.at(px, py) += xtl_min(energy_img.at(px - 1, py - 1), energy_img.at(px - 1, py));
else energy_img.at(px, py) += xtl_min(energy_img.at(px - 1, py - 1), energy_img.at(px - 1, py), energy_img.at(px - 1, py + 1));
}
}
return energy_img;
}
inline float_image_type sh_sc_vertical_energy(float_image_type const& grad_img)
{
float_image_type energy_img(grad_img);
for (int py = 1; py < (int)grad_img.col(); ++py)
{
for (int px = 0; px < (int)grad_img.row(); ++px)
{
if (px == 0) energy_img.at(px, py) += xtl_min(energy_img.at(px, py - 1), energy_img.at(px + 1, py - 1));
else if (px == (int)grad_img.row() - 1) energy_img.at(px, py) += xtl_min(energy_img.at(px - 1, py - 1), energy_img.at(px, py - 1));
else energy_img.at(px, py) += xtl_min(energy_img.at(px - 1, py - 1), energy_img.at(px, py - 1), energy_img.at(px + 1, py - 1));
}
}
return energy_img;
}
inline image_type sh_sc_vertical_remove(image_type const& old_img, float_image_type& energy_img, int n)
{
int px, py;
int w = (int)energy_img.row();
int h = (int)energy_img.col();
image_type new_img(old_img);
while (n-- && w > 0)
{
// find the position of minimum energy
double min_pos = 0;
for (px = 0; px < w; ++px)
min_pos = energy_img.at(px, h - 1) < energy_img.at(min_pos, h - 1)? px : min_pos;
image_type tmp_img(w - 1, h);
//float_image_type tmp_enery(w - 1, h);
for (py = h - 1; py >= 0; --py)
{
// find position
if (py == h - 1) ;
else if (min_pos == 0)
min_pos = energy_img.at(min_pos, py) < energy_img.at(min_pos + 1, py)? min_pos : min_pos + 1;
else if (min_pos == w - 1)
min_pos = energy_img.at(min_pos - 1, py) < energy_img.at(min_pos, py)? min_pos - 1 : min_pos;
else
{
int min_pos_2 = energy_img.at(min_pos, py) < energy_img.at(min_pos + 1, py)? min_pos : min_pos + 1;
min_pos = energy_img.at(min_pos_2, py) < energy_img.at(min_pos - 1, py)? min_pos_2 : min_pos - 1;
}
int ppx = 0;
for (px = 0; px < w; ++px)
{
if (px != min_pos)
{
tmp_img.at(ppx, py) = new_img.at(px, py);
//tmp_enery.at(ppx, py) = energy_img.at(px, py);
++ppx;
}
}
}
new_img = tmp_img;
//energy_img = tmp_enery;
--w;
}
return new_img;
}
inline image_type sh_sc_horizontal_remove(image_type const& old_img, float_image_type& energy_img, int n)
{
int px, py;
int w = (int)energy_img.row();
int h = (int)energy_img.col();
image_type new_img(old_img);
while (n-- && h > 0)
{
// find the position of minimum energy
double min_pos = 0;
for (py = 0; py < h; ++py)
min_pos = energy_img.at(h - 1, py) < energy_img.at(h - 1, min_pos)? py : min_pos;
image_type tmp_img(w, h - 1);
for (px = w - 1; px >= 0; --px)
{
// find position
if (px == w - 1) ;
else if (min_pos == 0)
min_pos = energy_img.at(px, min_pos) < energy_img.at(px, min_pos + 1)? min_pos : min_pos + 1;
else if (min_pos == h - 1)
min_pos = energy_img.at(px, min_pos - 1) < energy_img.at(px, min_pos)? min_pos - 1 : min_pos;
else
{
int min_pos_2 = energy_img.at(px, min_pos) < energy_img.at(px, min_pos + 1)? min_pos : min_pos + 1;
min_pos = energy_img.at(px, min_pos_2) < energy_img.at(px, min_pos - 1)? min_pos_2 : min_pos - 1;
}
int ppy = 0;
for (py = 0; py < h; ++py)
{
if (py != min_pos)
{
tmp_img.at(px, ppy) = new_img.at(px, py);
++ppy;
}
}
}
new_img = tmp_img;
--h;
}
return new_img;
}
// seam carving zoom out
inline image_type sh_sc_zoom_out(image_type const& old_img, int w, int h)
{
//float_image_type grad_img = sh_sc_impl_gradient_image(old_img, sh_sc_sobel(), 3);
//float_image_type grad_img = sh_sc_impl_gradient_image(old_img, sh_sc_prewitt(), 3);
//float_image_type grad_img = sh_sc_impl_gradient_image(old_img, sh_sc_isotropic(), 3);
//float_image_type energy_img = sh_sc_vertical_energy(grad_img);
//float_image_type energy_img = sh_sc_horizontal_energy(grad_img);
//image_type new_img = sh_sc_horizontal_remove(old_img, energy_img, 100);
//image_type new_img = sh_sc_vertical_remove(old_img, energy_img, 174);
image_type new_img(old_img);
int zoom_w = old_img.width() - w;
int zoom_h = old_img.height() - h;
while (zoom_w > 0 || zoom_h > 0)
{
image_type tmp_img(new_img);
float_image_type grad_img;
float_image_type energy_img;
if (zoom_w > 0)
{
grad_img = sh_sc_impl_gradient_image(tmp_img, sh_sc_sobel(), 3);
energy_img = sh_sc_vertical_energy(grad_img);
new_img = sh_sc_vertical_remove(tmp_img, energy_img, 1);
--zoom_w;
}
if (zoom_h > 0)
{
tmp_img = new_img;
grad_img = sh_sc_impl_gradient_image(tmp_img, sh_sc_sobel(), 3);
energy_img = sh_sc_horizontal_energy(grad_img);
new_img = sh_sc_horizontal_remove(tmp_img, energy_img, 1);
--zoom_h;
}
}
//int px, py;
//double max_v = 0;
// show grad image
/*image_type new_img(grad_img.row(), grad_img.col());
for (px = 0; px < (int)grad_img.row(); ++px)
for (py = 0; py < (int)grad_img.col(); ++py)
max_v = grad_img.at(px, py) > max_v? grad_img.at(px, py) : max_v;
for (px = 0; px < (int)grad_img.row(); ++px)
for (py = 0; py < (int)grad_img.col(); ++py)
//new_img.at(px, py).gray((int)grad_img.at(px, py) * 256 / max_v);
new_img.at(px, py).gray(((int)grad_img.at(px, py)) > 256? 255 : (int)grad_img.at(px, py));*/
/*image_type new_img(energy_img.row(), energy_img.col());
for (px = 0; px < (int)energy_img.row(); ++px)
for (py = 0; py < (int)energy_img.col(); ++py)
max_v = energy_img.at(px, py) > max_v? energy_img.at(px, py) : max_v;
for (px = 0; px < (int)energy_img.row(); ++px)
for (py = 0; py < (int)energy_img.col(); ++py)
new_img.at(px, py).gray((int)energy_img.at(px, py) * 256 / max_v);*/
//new_img.at(px, py).gray((int)energy_img.at(px, py) > 256? 255 : (int)energy_img.at(px, py));
return new_img;
}
#endif
#endif // SH_SC_H