-
Notifications
You must be signed in to change notification settings - Fork 10
/
filter.js
349 lines (310 loc) · 12.3 KB
/
filter.js
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
var filters = {
grayscale : function( src ) {
return src.map(
function(data, idx) {
var lev = Math.round(data[idx] * 0.299 + data[idx+1] * 0.587 + data[idx+2] * 0.114);
data[idx] = data[idx+1] = data[idx+2] = lev;
}
);
},
invert : function( src ) {
return src.map(function( data, idx ) {
data[idx] = 255 - data[idx]; ++idx;
data[idx] = 255 - data[idx]; ++idx;
data[idx] = 255 - data[idx];
});
},
brightness : function( src, val ) {
var clp = clamp;
return src.map(function( data, idx ) {
data[idx] = clp(data[idx] + val, 0, 255); ++idx;
data[idx] = clp(data[idx] + val, 0, 255); ++idx;
data[idx] = clp(data[idx] + val, 0, 255);
});
},
contrast : function( src, val ) {
var factor = Math.max((128 + val) / 128, 0);
var clp = clamp;
return src.map(function( data, idx ) {
data[idx] = clp( data[idx] * factor, 0, 255); ++idx;
data[idx] = clp( data[idx] * factor, 0, 255); ++idx;
data[idx] = clp( data[idx] * factor, 0, 255);
});
},
brightnesscontrast : function( src, alpha, beta ) {
var factor = Math.max((128 + alpha) / 128, 0);
var clp = clamp;
return src.map(function( data, idx ) {
data[idx] = clp( data[idx] * factor + beta, 0, 255); ++idx;
data[idx] = clp( data[idx] * factor + beta, 0, 255); ++idx;
data[idx] = clp( data[idx] * factor + beta, 0, 255);
});
},
histogram : function( src ) {
// histogram equalization, blended with original image
// amount is between 0 and 1
var h = src.h, w = src.w;
// grayscale image
var gimg = filters.grayscale(src);
// build histogram (pdf)
var hist = histogram(gimg, 0, 0, w, h);
// compute cdf
var cdf = buildcdf( hist );
var cumuhist = normalizecdf(cdf, 255);
var round = Math.round;
var clp = clamp;
// equalize
return src.map(function( data, idx ){
var lev = gimg.data[idx];
var cI = cumuhist[lev];
var ratio = cI / lev;
data[idx] = clp(round(data[idx] * ratio), 0, 255); ++idx;
data[idx] = clp(round(data[idx] * ratio), 0, 255); ++idx;
data[idx] = clp(round(data[idx] * ratio), 0, 255);
});
},
ahe : function( src ) {
// find a good window size
var h = src.h, w = src.w;
// tile size
var tilesize = [64, 64];
// number of bins
var num_bins = 256;
// number of tiles in x and y direction
var xtiles = Math.ceil(w / tilesize[0]);
var ytiles = Math.ceil(h / tilesize[1]);
var cdfs = new Array(ytiles);
for(var i=0;i<ytiles;i++)
cdfs[i] = new Array(xtiles);
var inv_tile_size = [1.0 / tilesize[0], 1.0 / tilesize[1]];
var binWidth = 256 / num_bins;
var gimg = filters.grayscale(src);
// create histograms
for(var i=0;i<ytiles;i++)
{
var y0 = i * tilesize[1];
var y1 = Math.min(y0+tilesize[1], h);
for(var j=0;j<xtiles;j++)
{
var x0 = j * tilesize[0];
var x1 = Math.min(x0+tilesize[0], w);
var hist = histogram(gimg, x0, y0, x1, y1, num_bins);
var cdf = buildcdf( hist );
cdf = normalizecdf(cdf, 255);
cdfs[i][j] = cdf;
}
}
var dst = new RGBAImage(w, h);
var srcdata = src.data;
for(var y=0, idx=0;y<h;++y)
{
for(var x=0;x<w;++x, idx+=4)
{
// intensity of current pixel
var I = gimg.getPixel(x, y).r;
// bin index
var bin = Math.floor(I / binWidth);
// current tile
var tx = x * inv_tile_size[0] - 0.5;
var ty = y * inv_tile_size[1] - 0.5;
var xl = Math.max(Math.floor(tx), 0);
var xr = Math.min(xl+1, xtiles-1);
var yt = Math.max(Math.floor(ty), 0);
var yd = Math.min(yt+1, ytiles-1);
var fx = tx - xl;
var fy = ty - yt;
var cdf11 = cdfs[yt][xl][bin];
var cdf12 = cdfs[yd][xl][bin];
var cdf21 = cdfs[yt][xr][bin];
var cdf22 = cdfs[yd][xr][bin];
// bilinear interpolation
var Iout = (1 - fx) * (1 - fy) * cdf11
+ (1 - fx) * fy * cdf12
+ fx * (1 - fy) * cdf21
+ fx * fy * cdf22;
var ratio = Iout / I;
var c = new Color(srcdata[idx] * ratio, srcdata[idx+1] * ratio, srcdata[idx+2] * ratio, srcdata[idx+3]);
dst.setPixel(x, y, c.clamp());
}
}
return dst;
},
// lut is the look up table defined by the input curve
curve : function(src, lut, channel) {
switch( channel )
{
case 'red':
{
return src.map(function(data, idx) {
data[idx] = lut[data[idx]];
});
}
case 'green':
{
return src.map(function(data, idx) {
data[idx+1] = lut[data[idx+1]];
});
}
case 'blue':
{
return src.map(function(data, idx) {
data[idx+2] = lut[data[idx+2]];
});
}
case 'brightness':
default:
{
var round = Math.round;
var clp = clamp;
return src.map(function(data, idx) {
var lev = round(data[idx] * 0.299 + data[idx+1] * 0.587 + data[idx+2] * 0.114);
var bias = 1e-6; // prevent divide by zero
var ratio = lut[lev]/(lev + bias);
data[idx] = clp(round(data[idx] * ratio), 0, 255); ++idx;
data[idx] = clp(round(data[idx] * ratio), 0, 255); ++idx;
data[idx] = clp(round(data[idx] * ratio), 0, 255);
});
}
}
},
reduction : function(src, method, colors) {
switch(method) {
case 'uniform': {
var levs = Math.ceil(Math.pow(colors, 1.0/3.0));
var round = Math.round;
return src.map(function(data, idx) {
data[idx] = round(round((data[idx] / 255.0) * levs) / levs * 255.0); ++idx;
data[idx] = round(round((data[idx] / 255.0) * levs) / levs * 255.0); ++idx;
data[idx] = round(round((data[idx] / 255.0) * levs) / levs * 255.0);
});
}
case 'population': {
var hist = colorHistogram(src, 0, 0, src.w, src.h);
var rcdf = normalizecdf( buildcdf(hist[0]) );
var gcdf = normalizecdf( buildcdf(hist[1]) );
var bcdf = normalizecdf( buildcdf(hist[2]) );
var levels = Math.ceil(Math.pow(colors, 1.0/3.0));
// get sample points using CDF
var genSamples = function(cdf) {
var pts = [];
var step = (1.0 - cdf[0]) / levels;
for(var j=0;j<=levels;j++) {
var p = step * j + cdf[0];
for(var i=1;i<256;i++) {
if( cdf[i-1] <= p && cdf[i] >= p ) {
pts.push(i);
break;
}
}
}
return pts;
};
// sample points in each channel
var rPoints = genSamples(rcdf),
gPoints = genSamples(gcdf),
bPoints = genSamples(bcdf);
// assemble the samples to a color table
return src.map(function(data, idx) {
// find closet r sample point
data[idx] = findClosest(data[idx], rPoints); ++idx;
// find closet g sample point
data[idx] = findClosest(data[idx], gPoints); ++idx;
// find closet b sample point
data[idx] = findClosest(data[idx], bPoints);
});
}
case 'mediancut': {
var colormap = algorithms.mediancut(src, colors);
return src.map(function(data, idx) {
var nc = findClosestColor(new Color(data[idx], data[idx+1], data[idx+2], data[idx+3]), colormap);
data[idx] = nc.r; ++idx;
data[idx] = nc.g; ++idx;
data[idx] = nc.b;
});
}
case 'knn': {
var colormap = algorithms.kmeans(src, colors);
return src.map(function(data, idx) {
var nc = findClosestColor(new Color(data[idx], data[idx+1], data[idx+2], data[idx+3]), colormap);
data[idx] = nc.r; ++idx;
data[idx] = nc.g; ++idx;
data[idx] = nc.b;
});
}
case 'ann': {
var colormap = algorithms.neuralnetwork(src, colors);
return src.map(function(data, idx) {
var nc = findClosestColor(new Color(data[idx], data[idx+1], data[idx+2], data[idx+3]), colormap);
data[idx] = nc.r; ++idx;
data[idx] = nc.g; ++idx;
data[idx] = nc.b;
});
}
}
},
spatialfilter : function( src, f ) {
console.log( 'applying spatial filter ...' ) ;
// source image size
var w = src.w, h = src.h;
// filter size
var wf = Math.floor((f.width - 1) / 2);
var hf = Math.floor((f.height - 1) / 2);
// filter weights
var weights = f.weights;
var bias = f.bias;
// inverse of the scaling factor( sum of weights )
var invfactor = 1.0 / f.factor;
// slow implementation
/*
var round = Math.round;
return src.map( function(r, g, b, a, x, y, w, h) {
r = 0, g = 0, b = 0;
for(var i=-hf, fi= 0, fidx = 0;i<=hf;i++, fi++) {
var py = clamp(i+y, 0, h-1);
for(var j=-wf, fj=0;j<=wf;j++, fj++, fidx++) {
var px = clamp(j+x, 0, w-1);
var wij = weights[fidx];
var cij = src.getPixel(px, py);
r += cij.r * wij;
g += cij.g * wij;
b += cij.b * wij;
}
}
r = round(clamp(r * invfactor + bias, 0, 255));
g = round(clamp(g * invfactor + bias, 0, 255));
b = round(clamp(b * invfactor + bias, 0, 255));
return new Color(r, g, b, a);
} );
*/
// fast implementation
var dst = new RGBAImage(w, h);
var srcdata = src.data;
var dstdata = dst.data;
var round = Math.round;
var clp = clamp;
for(var y = 0,idx=0;y<h;++y) {
for(var x=0;x<w;++x) {
var r = 0, g = 0, b = 0;
for(var i=-hf, fi= 0, fidx = 0;i<=hf;++i, ++fi) {
var py = clp(i+y, 0, h-1);
for(var j=-wf, fj=0;j<=wf;++j, ++fj, ++fidx) {
var px = clp(j+x, 0, w-1);
var pidx = (py * w + px) * 4;
var wij = weights[fidx];
r += srcdata[pidx] * wij;
g += srcdata[++pidx] * wij;
b += srcdata[++pidx] * wij;
}
}
r = round(clp(r * invfactor + bias, 0, 255));
g = round(clp(g * invfactor + bias, 0, 255));
b = round(clp(b * invfactor + bias, 0, 255));
dstdata[idx] = r; ++idx;
dstdata[idx] = g; ++idx;
dstdata[idx] = b; ++idx;
dstdata[idx] = 255; ++idx;
}
}
return dst;
}
};