-
Notifications
You must be signed in to change notification settings - Fork 53
/
image.h
374 lines (313 loc) · 8.31 KB
/
image.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
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
#pragma once
#include "png.hpp"
#include "itensor/util/stdx.h"
#include "itensor/global.h"
#include "itensor/util/args.h"
#include "itensor/indextype.h"
using std::array;
using itensor::range;
using itensor::Args;
using itensor::Global;
using itensor::printfln;
using itensor::Real;
using itensor::Cplx;
using itensor::Pi;
using itensor::sqr;
using Pix = uint8_t;
using ImgLabel = long;
class RGBPixel
{
array<Real,3> rgb;
public:
RGBPixel() : rgb{} { }
RGBPixel(Real r, Real g, Real b) : rgb{{r,g,b}} { }
Real
operator[](size_t n) const { return rgb[n]; }
Real &
operator[](size_t n) { return rgb[n]; }
size_t
size() const { return 3ul; }
RGBPixel&
operator/=(Real x)
{
rgb[0] /= x;
rgb[1] /= x;
rgb[2] /= x;
return *this;
}
RGBPixel&
operator*=(Real x)
{
rgb[0] *= x;
rgb[1] *= x;
rgb[2] *= x;
return *this;
}
RGBPixel&
operator+=(RGBPixel p)
{
rgb[0] += p.rgb[0];
rgb[1] += p.rgb[1];
rgb[2] += p.rgb[2];
return *this;
}
};
RGBPixel inline
operator+(RGBPixel p1, RGBPixel const& p2) { return (p1+=p2); }
enum ImageType { None, Train, Test};
ImageType inline
imageType(long t) { return (t==(long)Train) ? Train : Test; }
template<typename PixT>
class ImgStore
{
std::vector<PixT> pix_;
size_t nrows_ = 0;
size_t ncols_ = 0;
long num_ = -1;
ImageType type_ = None;
long label_ = -1;
public:
using value_type = PixT;
ImgStore() { }
ImgStore(size_t nrows,
size_t ncols,
ImageType type = None,
long num = -1,
long label = -1)
: pix_(nrows*ncols),
nrows_(nrows),
ncols_(ncols),
num_(num),
type_(type),
label_(label)
{ }
ImgStore(size_t len,
ImageType type = None,
long num = -1,
long label = -1)
: pix_(len*len),
nrows_(len),
ncols_(len),
num_(num),
type_(type),
label_(label)
{ }
explicit operator bool() const { return pix_.empty(); }
size_t
size() const { return pix_.size(); }
long
label() const { return label_; }
value_type const&
operator[](size_t n) const { return pix_.at(n); }
value_type&
operator[](size_t n) { return pix_.at(n); }
long
num() const { return num_; }
ImageType
type() const { return type_; }
size_t
width() const { return nrows_; }
size_t
height() const { return ncols_; }
auto
begin() -> decltype(pix_.begin()) const { return pix_.begin(); }
auto
end() -> decltype(pix_.end()) const { return pix_.end(); }
auto
data() -> decltype(pix_.data()) const { return pix_.data(); }
};
using Pixels = ImgStore<Pix>;
using RGBStore = ImgStore<RGBPixel>;
template<typename Storage>
class ImageT
{
private:
Storage store_;
public:
using value_type = stdx::remove_const_t<typename Storage::value_type>;
ImageT() { }
template<typename... SArgs>
ImageT(SArgs&&... sargs)
: store_(std::forward<SArgs>(sargs)...)
{
assert(type()==Train || type()==Test);
}
explicit operator bool() const { return store_; }
size_t
size() const { return store_.size(); }
size_t
width() const { return store_.width(); }
size_t
height() const { return store_.height(); }
//0-indexed
value_type
operator[](size_t n) const { return store_[n]; }
//1-indexed
value_type
operator()(long n) const { return store_[n-1]; }
//0-indexed
value_type
pixel(size_t x, size_t y) const
{
return store_[x+width()*y];
}
//template<typename V>
//auto
//set(size_t x, size_t y, V const& v)
// -> stdx::if_compiles_return<void,decltype(store_[0]=value_type(v))>
// {
// store_[x+width()*y] = v;
// }
void
set(size_t x, size_t y, value_type v)
{
store_[x+width()*y] = v;
}
ImgLabel
label() const { return store_.label(); }
long
num() const { return store_.num(); }
ImageType
type() const { return store_.type(); }
auto
begin() -> decltype(store_.begin()) const { return store_.begin(); }
auto
end() -> decltype(store_.end()) const { return store_.end(); }
auto
data() -> decltype(store_.data()) const { return store_.data(); }
};
using RGBImage = ImageT<RGBStore>;
template<typename N, typename S>
ImageT<N>
resize(ImageT<S> const& oimg,
long newlen,
bool debug = false)
{
using Sizet = decltype(oimg.width());
using PixT = decltype(oimg.pixel(0,0));
assert(oimg.width()==oimg.height());
long oldlen = oimg.width();
auto nimg = ImageT<N>(newlen,oimg.type(),oimg.num(),oimg.label());
if(oldlen < newlen)
{
Sizet pad = (newlen-oldlen)/2;
for(auto nx : range(nimg.width()))
for(auto ny : range(nimg.height()))
{
if(nx < pad || ny < pad ||
nx >= pad+oldlen || ny >= pad+oldlen)
{
nimg.set(nx,ny,PixT{});
}
else
{
auto ox = nx-pad;
auto oy = ny-pad;
nimg.set(nx,ny,oimg.pixel(ox,oy));
}
}
}
else if(oldlen > newlen)
{
//Credit Mark Ransom for posting algorithm
//below on Stack Overflow
Real scale = (1.*newlen)/oldlen;
PixT threshold = 0.5/(scale*scale);
Real yend = 0.0;
for(int f = 0; f < newlen; ++f) // y on output
{
Real ystart = yend;
yend = (f+1)/scale;
if(yend >= oldlen) yend = oldlen - 0.000001;
Real xend = 0.0;
for(int g = 0; g < newlen; ++g) // x on output
{
Real xstart = xend;
xend = (g+1)/scale;
if(xend >= oldlen) xend = oldlen - 0.000001;
auto sum = PixT{};
for(int y = (int)ystart; y <= (int)yend; ++y)
{
Real yportion = 1.0;
if(y == (int)ystart) yportion -= ystart - y;
if(y == (int)yend) yportion -= y+1 - yend;
for(int x = (int)xstart; x <= (int)xend; ++x)
{
Real xportion = 1.0;
if(x == (int)xstart) xportion -= xstart - x;
if(x == (int)xend) xportion -= x+1 - xend;
sum += oimg.pixel(x,y) * yportion * xportion;
}
}
//auto val = (sum > threshold) ? 255 : 0;
nimg.set(g,f,sum);
}
}
}
else //oldlen == newlen
{
assert(oldlen == newlen);
for(auto x : range(oimg.width()))
for(auto y : range(oimg.height()))
{
nimg.set(x,y,oimg.pixel(x,y));
}
}
return nimg;
}
template<typename N, typename S>
ImageT<N>
reduce(ImageT<S> const& oimg,
long newlen)
{
using PixT = decltype(oimg.pixel(0,0));
assert(oimg.width()==oimg.height());
auto bsize = oimg.width()/newlen;
auto rem = oimg.width()%bsize;
auto nimg = ImageT<N>(newlen,oimg.type(),oimg.num(),oimg.label());
for(auto nx : range(nimg.width()))
for(auto ny : range(nimg.height()))
{
auto avg = PixT{};
long cnt = 0;
auto oxs = rem+bsize*nx;
auto oys = rem+bsize*ny;
//printfln("nx,ny = %d,%d",nx,ny);
for(auto ox : range(oxs,oxs+bsize))
for(auto oy : range(oys,oys+bsize))
{
//printfln(" ox,oy = %d,%d",ox,oy);
avg += oimg.pixel(ox,oy);
++cnt;
}
avg /= cnt;
nimg.set(nx,ny,avg);
}
return nimg;
}
template<class S>
void
writeGray(ImageT<S> const& image,
std::string fname)
{
auto I = png::image<png::gray_pixel>(image.width(),image.height());
for(auto y : range(I.get_height()))
for(auto x : range(I.get_width()))
{
I[y][x] = png::gray_pixel((255-image.pixel(x,y)));
}
I.write(fname.c_str());
}
void inline
writeColor(ImageT<RGBStore> const& image,
std::string fname)
{
auto I = png::image<png::rgb_pixel>(image.width(),image.height());
for(auto y : range(I.get_height()))
for(auto x : range(I.get_width()))
{
auto p = image.pixel(x,y);
I[y][x] = png::rgb_pixel(p[0],p[1],p[2]);
}
I.write(fname.c_str());
}