-
Notifications
You must be signed in to change notification settings - Fork 2
/
svmfilter_webgl.js
executable file
·1350 lines (1179 loc) · 48.2 KB
/
svmfilter_webgl.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
var webglFilter = function() {
/*
* Textures:
* 0 : raw filter
* 1 : patches
* 2 : finished response
* 3 : grad/lbp treated patches
* 4 : sobel filter
* 5 : lbp filter
*
* Routing:
* ( ) 0/4/5 --\
* ( ) _\|
* 1 ----> ( ---------->3 ) ----------> 2
* lbpResponse/ patchResponse
* gradientResponse
*/
var gl, canvas;
var filterWidth, filterHeight, patchWidth, patchHeight, numPatches, canvasWidth, canvasHeight;
var patchResponseProgram, patchDrawProgram;
var fbo, numBlocks, patchTex;
var drawRectBuffer, drawLayerBuffer, drawImageBuffer, rttTexture;
var texCoordBuffer, texCoordLocation, apositionBuffer;
var newCanvasWidth, newCanvasBlockHeight, newCanvasHeight;
var drawOutRectangles, drawOutImages, drawOutLayer;
var patchCells, textureWidth, textureHeight, patchSize, patchArray;
var biases;
var lbpResponseProgram;
var lbo, lbpTexCoordLocation, lbpTexCoordBuffer, lbpPositionLocation, lbpAPositionBuffer;
var gradientResponseProgram;
var gbo, gradTexCoordLocation, gradTexCoordBuffer, gradPositionLocation, gradAPositionBuffer;
var lbpInit = false;
var sobelInit = false;
var rawInit = false;
var lbpResponseVS = [
"attribute vec2 a_texCoord;",
"attribute vec2 a_position;",
"",
"varying vec2 v_texCoord;",
"",
"void main() {",
" // transform coordinates to regular coordinates",
" gl_Position = vec4(a_position,0.0,1.0);",
" ",
" // pass the texCoord to the fragment shader",
" v_texCoord = a_texCoord;",
"}"
].join('\n');
var lbpResponseFS;
var gradientResponseVS = [
"attribute vec2 a_texCoord;",
"attribute vec2 a_position;",
"",
"varying vec2 v_texCoord;",
"",
"void main() {",
" // transform coordinates to regular coordinates",
" gl_Position = vec4(a_position,0.0,1.0);",
" ",
" // pass the texCoord to the fragment shader",
" v_texCoord = a_texCoord;",
"}"
].join('\n');
var gradientResponseFS;
var patchResponseVS;
var patchResponseFS;
var drawResponsesVS = [
"attribute vec2 a_texCoord_draw;",
"attribute vec2 a_position_draw;",
"attribute float a_patchChoice_draw;",
"",
"uniform vec2 u_resolutiondraw;",
"",
"varying vec2 v_texCoord;",
"varying float v_select;",
"",
"void main() {",
" // convert the rectangle from pixels to 0.0 to 1.0",
" vec2 zeroToOne = a_position_draw / u_resolutiondraw;",
"",
" // convert from 0->1 to 0->2",
" vec2 zeroToTwo = zeroToOne * 2.0;",
"",
" // convert from 0->2 to -1->+1 (clipspace)",
" vec2 clipSpace = zeroToTwo - 1.0;",
" ",
" // transform coordinates to regular coordinates",
" gl_Position = vec4(clipSpace * vec2(1.0, 1.0), 0, 1);",
"",
" // pass the texCoord to the fragment shader",
" v_texCoord = a_texCoord_draw;",
" ",
" v_select = a_patchChoice_draw;",
"}"
].join('\n');
var drawResponsesFS = [
"precision mediump float;",
"",
"// our responses",
"uniform sampler2D u_responses;",
"",
"// the texCoords passed in from the vertex shader.",
"varying vec2 v_texCoord;",
"varying float v_select;",
"",
"const vec4 bit_shift = vec4(256.0*256.0*256.0, 256.0*256.0, 256.0, 1.0);",
"const vec4 bit_mask = vec4(0.0, 1.0/256.0, 1.0/256.0, 1.0/256.0);",
"",
"// packing code from here http://stackoverflow.com/questions/9882716/packing-float-into-vec4-how-does-this-code-work",
"void main() {",
" vec4 colorSum = texture2D(u_responses, v_texCoord);",
" float value = 0.0;",
" if (v_select < 0.1) {",
" value = colorSum[0];",
" } else if (v_select > 0.9 && v_select < 1.1) {",
" value = colorSum[1];",
" } else if (v_select > 1.9 && v_select < 2.1) {",
" value = colorSum[2];",
" } else if (v_select > 2.9 && v_select < 3.1) {",
" value = colorSum[3];",
" } else {",
" value = 1.0;",
" }",
" ",
" vec4 res = fract(value * bit_shift);",
" res -= res.xxyz * bit_mask;",
" ",
" //gl_FragColor = vec4(value, value, value, value);",
" //gl_FragColor = vec4(1.0, value, 1.0, 1.0);",
" gl_FragColor = res;",
"}"
].join('\n');
this.init = function(filters, bias, nP, pW, pH, fW, fH) {
// we assume filterVector goes from left to right, rowwise, i.e. row-major order
if (fW != fH) {
alert("filter width and height must be same size!");
return;
}
// if filter width is not odd, alert
if (fW % 2 == 0 || fH % 2 == 0) {
alert("filters used in svm must be of odd dimensions!");
return;
}
// setup variables
biases = bias;
filterWidth = fW;
filterHeight = fH;
patchWidth = pW;
patchHeight = pH;
numPatches = nP;
numBlocks = Math.floor(numPatches / 4) + Math.ceil((numPatches % 4)/4);
canvasWidth = patchWidth;
canvasHeight = patchHeight*numBlocks;
newCanvasWidth = patchWidth-filterWidth+1;
newCanvasBlockHeight = patchHeight-filterWidth+1;
newCanvasHeight = newCanvasBlockHeight*numPatches;
patchCells = (Math.floor(numPatches / 4) + Math.ceil((numPatches % 4)/4));
textureWidth = patchWidth;
textureHeight = patchHeight*patchCells;
patchSize = patchWidth*patchHeight;
patchArray = new Float32Array(patchSize*patchCells*4);
var opp = [1/patchWidth, 1/(patchHeight*numBlocks)];
// write out shaders
patchResponseFS = [
"precision mediump float;",
"",
"const vec2 u_onePixelPatches = vec2("+(1/patchWidth).toFixed(10)+","+(1/(patchHeight*numBlocks)).toFixed(10)+");",
"const vec2 u_onePixelFilters = vec2("+(1/filterWidth).toFixed(10)+","+(1/(filterHeight*numBlocks)).toFixed(10)+");",
"const float u_halffilterwidth = "+((filterWidth-1.0)/2).toFixed(1)+";",
"const float u_halffilterheight = "+((filterHeight-1.0)/2).toFixed(1)+";",
"",
"// our patches",
"uniform sampler2D u_patches;",
"// our filters",
"uniform sampler2D u_filters;",
"",
"// the texCoords passed in from the vertex shader.",
"varying vec2 v_texCoord;",
"varying vec2 v_texCoordFilters; // this should give us correct filter",
"",
"void main() {",
" vec4 colorSum = vec4(0.0, 0.0, 0.0, 0.0);",
" vec4 maxn = vec4(0.0, 0.0, 0.0, 0.0);",
" vec4 minn = vec4(256.0, 256.0, 256.0, 256.0);",
" vec4 scale = vec4(0.0, 0.0, 0.0, 0.0);",
" vec4 patchValue = vec4(0.0, 0.0, 0.0, 0.0);",
" vec4 filterValue = vec4(0.0, 0.0, 0.0, 0.0);",
" vec4 filterTemp = vec4(0.0, 0.0, 0.0, 0.0);",
" for (int w = 0;w < "+filterWidth+";w++) {",
" for (int h = 0;h < "+filterHeight+";h++) {",
" patchValue = texture2D(u_patches, v_texCoord + u_onePixelPatches * vec2(float(w)-u_halffilterwidth, float(h)-u_halffilterheight));",
" filterValue = texture2D(u_filters, v_texCoordFilters + u_onePixelFilters * vec2(float(w)-u_halffilterwidth, float(h)-u_halffilterheight));",
" maxn = max(patchValue, maxn);",
" minn = min(patchValue, minn);",
" colorSum += patchValue*filterValue;",
" filterTemp += filterValue;",
" } ",
" }",
" scale = maxn-minn;",
" colorSum = (colorSum-(minn*filterTemp))/scale;",
" // logistic transformation",
" colorSum = 1.0/(1.0 + exp(- (colorSum) ));",
" gl_FragColor = colorSum;",
"}"
].join('\n');
patchResponseVS = [
"attribute vec2 a_texCoord;",
"attribute vec2 a_position;",
"",
"const vec2 u_resolution = vec2("+canvasWidth.toFixed(1)+","+canvasHeight.toFixed(1)+");",
"const float u_patchHeight = "+(1/numBlocks).toFixed(10)+";",
"const float u_filterHeight = "+(1/numBlocks).toFixed(10)+";",
"const vec2 u_midpoint = vec2(0.5 ,"+(1/(numBlocks*2)).toFixed(10)+");",
"",
"varying vec2 v_texCoord;",
"varying vec2 v_texCoordFilters;",
"",
"void main() {",
" // convert the rectangle from pixels to 0.0 to 1.0",
" vec2 zeroToOne = a_position / u_resolution;",
"",
" // convert from 0->1 to 0->2",
" vec2 zeroToTwo = zeroToOne * 2.0;",
"",
" // convert from 0->2 to -1->+1 (clipspace)",
" vec2 clipSpace = zeroToTwo - 1.0;",
" ",
" // transform coordinates to regular coordinates",
" gl_Position = vec4(clipSpace * vec2(1.0, 1.0), 0, 1);",
" ",
" // pass the texCoord to the fragment shader",
" v_texCoord = a_texCoord;",
" ",
" // set the filtertexture coordinate based on number filter to use",
" v_texCoordFilters = u_midpoint + vec2(0.0, u_filterHeight * floor(a_texCoord[1]/u_patchHeight));",
"}"
].join('\n');
if ('lbp' in filters) {
// lbpResponseFragment
lbpResponseFS = [
"precision mediump float;",
"",
"uniform vec2 u_onePixelPatches;",
"",
"// our patches",
"uniform sampler2D u_patches;",
"",
"// the texCoords passed in from the vertex shader.",
"varying vec2 v_texCoord;",
"",
"void main() {",
" vec4 topLeft = texture2D(u_patches, v_texCoord + vec2(-"+opp[0].toFixed(5)+", -"+opp[1].toFixed(5)+"));",
" vec4 topMid = texture2D(u_patches, v_texCoord + vec2(0.0, -"+opp[1].toFixed(5)+"));",
" vec4 topRight = texture2D(u_patches, v_texCoord + vec2("+opp[0].toFixed(5)+", -"+opp[1].toFixed(5)+"));",
" vec4 midLeft = texture2D(u_patches, v_texCoord + vec2(-"+opp[0].toFixed(5)+", 0.0));",
" vec4 midMid = texture2D(u_patches, v_texCoord);",
" vec4 midRight = texture2D(u_patches, v_texCoord + vec2("+opp[0].toFixed(5)+", 0.0));",
" vec4 bottomLeft = texture2D(u_patches, v_texCoord + vec2(-"+opp[0].toFixed(5)+", "+opp[1].toFixed(5)+"));",
" vec4 bottomMid = texture2D(u_patches, v_texCoord + vec2(0.0, "+opp[1].toFixed(5)+"));",
" vec4 bottomRight = texture2D(u_patches, v_texCoord + vec2("+opp[0].toFixed(5)+", "+opp[1].toFixed(5)+"));",
" vec4 lbp = step(midMid, midRight)*1.0 + step(midMid, topRight)*2.0 + step(midMid, topMid)*4.0;",
" lbp = lbp + step(midMid, topLeft)*8.0 + step(midMid, midLeft)*16.0 + step(midMid, bottomLeft)*32.0;",
" lbp = lbp + step(midMid, bottomMid)*64.0 + step(midMid, bottomRight)*128.0;",
" gl_FragColor = lbp;",
"}"
].join('\n');
}
if ('sobel' in filters) {
// gradResponseFragment
gradientResponseFS = [
"precision mediump float;",
"",
"uniform vec2 u_onePixelPatches;",
"",
"// our patches",
"uniform sampler2D u_patches;",
"",
"// the texCoords passed in from the vertex shader.",
"varying vec2 v_texCoord;",
"",
"void main() {",
" vec4 bottomLeft = texture2D(u_patches, v_texCoord + vec2(-"+opp[0].toFixed(5)+", "+opp[1].toFixed(5)+"));",
" vec4 bottomRight = texture2D(u_patches, v_texCoord + vec2("+opp[0].toFixed(5)+", "+opp[1].toFixed(5)+"));",
" vec4 topLeft = texture2D(u_patches, v_texCoord + vec2(-"+opp[0].toFixed(5)+", -"+opp[1].toFixed(5)+"));",
" vec4 topRight = texture2D(u_patches, v_texCoord + vec2("+opp[0].toFixed(5)+", -"+opp[1].toFixed(5)+"));",
" vec4 dx = (",
" bottomLeft +",
" (texture2D(u_patches, v_texCoord + vec2(-"+opp[0].toFixed(5)+", 0.0))*vec4(2.0,2.0,2.0,2.0)) +",
" topLeft -",
" bottomRight -",
" (texture2D(u_patches, v_texCoord + vec2("+opp[0].toFixed(5)+", 0.0))*vec4(2.0,2.0,2.0,2.0)) -",
" topRight)/4.0;",
" vec4 dy = (",
" bottomLeft +",
" (texture2D(u_patches, v_texCoord + vec2(0.0, "+opp[1].toFixed(5)+"))*vec4(2.0,2.0,2.0,2.0)) +",
" bottomRight -",
" topLeft -",
" (texture2D(u_patches, v_texCoord + vec2(0.0, -"+opp[1].toFixed(5)+"))*vec4(2.0,2.0,2.0,2.0)) -",
" topRight)/4.0;",
" vec4 gradient = sqrt((dx*dx) + (dy*dy));",
" gl_FragColor = gradient;",
"}"
].join('\n');
}
//create webglcanvas
canvas = document.createElement('canvas')
canvas.setAttribute('width', (patchWidth-filterWidth+1)+"px");
canvas.setAttribute('height', ((patchHeight-filterHeight+1)*numPatches)+"px");
canvas.setAttribute('id', 'renderCanvas');
canvas.setAttribute('style', 'display:none;');
document.body.appendChild(canvas);
// TODO : isolate this library from webgl-util.js
gl = setupWebGL(canvas, {premultipliedAlpha: false, preserveDrawingBuffer : true, antialias : false});
// check for float textures support and fail if not
if (!gl.getExtension("OES_texture_float")) {
alert("Your graphics card does not support floating point textures! :(");
return;
}
/** insert filters into textures **/
if ('raw' in filters) {
insertFilter(filters['raw'], gl.TEXTURE0)
rawInit = true;
}
if ('sobel' in filters) {
insertFilter(filters['sobel'], gl.TEXTURE4)
sobelInit = true;
}
if ('lbp' in filters) {
insertFilter(filters['lbp'], gl.TEXTURE5)
lbpInit = true;
}
/** calculate vertices for calculating responses **/
// vertex rectangles to draw out
var rectangles = [];
var halfFilter = (filterWidth-1)/2;
var yOffset;
for (var i = 0;i < numBlocks;i++) {
yOffset = i*patchHeight;
//first triangle
rectangles = rectangles.concat(
[halfFilter, yOffset+halfFilter,
patchWidth-halfFilter, yOffset+halfFilter,
halfFilter, yOffset+patchHeight-halfFilter]
);
//second triangle
rectangles = rectangles.concat(
[halfFilter, yOffset+patchHeight-halfFilter,
patchWidth-halfFilter, yOffset+halfFilter,
patchWidth-halfFilter, yOffset+patchHeight-halfFilter]
);
}
rectangles = new Float32Array(rectangles);
// image rectangles to draw out
var irectangles = [];
for (var i = 0;i < rectangles.length;i++) {
if (i % 2 == 0) {
irectangles[i] = rectangles[i]/canvasWidth;
} else {
irectangles[i] = rectangles[i]/canvasHeight;
}
}
irectangles = new Float32Array(irectangles);
if ('lbp' in filters || 'sobel' in filters) {
var topCoord = 1.0 - 2/(patchHeight*numBlocks);
var bottomCoord = 1.0 - 2/numBlocks + 2/(patchHeight*numBlocks);
var yOffset;
// calculate position of vertex rectangles for gradient/lbp program
var gradRectangles = [];
for (var i = 0;i < numBlocks;i++) {
yOffset = i * (2/numBlocks);
//first triangle
gradRectangles = gradRectangles.concat(
[-1.0, topCoord - yOffset,
1.0, topCoord - yOffset,
-1.0, bottomCoord - yOffset]
);
//second triangle
gradRectangles = gradRectangles.concat(
[-1.0, bottomCoord - yOffset,
1.0, topCoord - yOffset,
1.0, bottomCoord - yOffset]
);
}
gradRectangles = new Float32Array(gradRectangles);
topCoord = 1.0 - 1/(patchHeight*numBlocks);
bottomCoord = 1.0 - 1/numBlocks + 1/(patchHeight*numBlocks);
// calculate position of image rectangles to draw out
var gradIRectangles = [];
for (var i = 0;i < numBlocks;i++) {
yOffset = i * (1/numBlocks);
//first triangle
gradIRectangles = gradIRectangles.concat(
[0.0, topCoord - yOffset,
1.0, topCoord - yOffset,
0.0, bottomCoord - yOffset]
);
//second triangle
gradIRectangles = gradIRectangles.concat(
[0.0, bottomCoord - yOffset,
1.0, topCoord - yOffset,
1.0, bottomCoord - yOffset]
);
}
gradIRectangles = new Float32Array(gradIRectangles);
}
// vertices for drawing out responses
// drawOutRectangles
drawOutRectangles = new Float32Array(12*numPatches);
var yOffset, indexOffset;
for (var i = 0;i < numPatches;i++) {
yOffset = i*newCanvasBlockHeight;
indexOffset = i*12;
//first triangle
drawOutRectangles[indexOffset] = 0.0;
drawOutRectangles[indexOffset+1] = yOffset;
drawOutRectangles[indexOffset+2] = newCanvasWidth;
drawOutRectangles[indexOffset+3] = yOffset;
drawOutRectangles[indexOffset+4] = 0.0;
drawOutRectangles[indexOffset+5] = yOffset+newCanvasBlockHeight;
//second triangle
drawOutRectangles[indexOffset+6] = 0.0;
drawOutRectangles[indexOffset+7] = yOffset+newCanvasBlockHeight;
drawOutRectangles[indexOffset+8] = newCanvasWidth;
drawOutRectangles[indexOffset+9] = yOffset;
drawOutRectangles[indexOffset+10] = newCanvasWidth;
drawOutRectangles[indexOffset+11] = yOffset+newCanvasBlockHeight;
}
// images
drawOutImages = new Float32Array(numPatches*12);
var halfFilterWidth = ((filterWidth-1)/2)/patchWidth;
var halfFilterHeight = ((filterWidth-1)/2)/(patchHeight*patchCells);
var patchHeightT = patchHeight / (patchHeight*patchCells);
for (var i = 0;i < numPatches;i++) {
yOffset = Math.floor(i / 4)*patchHeightT;
indexOffset = i*12;
//first triangle
drawOutImages[indexOffset] = halfFilterWidth;
drawOutImages[indexOffset+1] = yOffset+halfFilterHeight;
drawOutImages[indexOffset+2] = 1.0-halfFilterWidth;
drawOutImages[indexOffset+3] = yOffset+halfFilterHeight;
drawOutImages[indexOffset+4] = halfFilterWidth;
drawOutImages[indexOffset+5] = yOffset+patchHeightT-halfFilterHeight;
//second triangle
drawOutImages[indexOffset+6] = halfFilterWidth;
drawOutImages[indexOffset+7] = yOffset+patchHeightT-halfFilterHeight;
drawOutImages[indexOffset+8] = 1.0-halfFilterWidth;
drawOutImages[indexOffset+9] = yOffset+halfFilterHeight;
drawOutImages[indexOffset+10] = 1.0-halfFilterWidth;
drawOutImages[indexOffset+11] = yOffset+patchHeightT-halfFilterHeight;
}
// layer
drawOutLayer = new Float32Array(numPatches*6);
var layernum;
for (var i = 0;i < numPatches;i++) {
layernum = i % 4;
indexOffset = i*6;
drawOutLayer[indexOffset] = layernum;
drawOutLayer[indexOffset+1] = layernum;
drawOutLayer[indexOffset+2] = layernum;
drawOutLayer[indexOffset+3] = layernum;
drawOutLayer[indexOffset+4] = layernum;
drawOutLayer[indexOffset+5] = layernum;
}
/** set up programs and load attributes etc **/
if ('sobel' in filters) {
var grVertexShader = loadShader(gl, gradientResponseVS, gl.VERTEX_SHADER);
var grFragmentShader = loadShader(gl, gradientResponseFS, gl.FRAGMENT_SHADER);
gradientResponseProgram = createProgram(gl, [grVertexShader, grFragmentShader]);
gl.useProgram(gradientResponseProgram);
// set up vertices with rectangles
gradPositionLocation = gl.getAttribLocation(gradientResponseProgram, "a_position");
gradAPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, gradAPositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, gradRectangles, gl.STATIC_DRAW);
gl.enableVertexAttribArray(gradPositionLocation);
gl.vertexAttribPointer(gradPositionLocation, 2, gl.FLOAT, false, 0, 0);
// set up texture positions
gradTexCoordLocation = gl.getAttribLocation(gradientResponseProgram, "a_texCoord");
gradTexCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, gradTexCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, gradIRectangles, gl.STATIC_DRAW);
gl.enableVertexAttribArray(gradTexCoordLocation);
gl.vertexAttribPointer(gradTexCoordLocation, 2, gl.FLOAT, false, 0, 0);
// set up patches texture in gradientResponseProgram
gl.uniform1i(gl.getUniformLocation(gradientResponseProgram, "u_patches"), 1);
}
if ('lbp' in filters) {
var lbpVertexShader = loadShader(gl, lbpResponseVS, gl.VERTEX_SHADER);
var lbpFragmentShader = loadShader(gl, lbpResponseFS, gl.FRAGMENT_SHADER);
lbpResponseProgram = createProgram(gl, [lbpVertexShader, lbpFragmentShader]);
gl.useProgram(lbpResponseProgram);
// set up vertices with rectangles
lbpPositionLocation = gl.getAttribLocation(lbpResponseProgram, "a_position");
lbpAPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, lbpAPositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, gradRectangles, gl.STATIC_DRAW);
gl.enableVertexAttribArray(lbpPositionLocation);
gl.vertexAttribPointer(lbpPositionLocation, 2, gl.FLOAT, false, 0, 0);
// set up texture positions
gradTexCoordLocation = gl.getAttribLocation(lbpResponseProgram, "a_texCoord");
lbpTexCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, lbpTexCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, gradIRectangles, gl.STATIC_DRAW);
gl.enableVertexAttribArray(lbpTexCoordLocation);
gl.vertexAttribPointer(lbpTexCoordLocation, 2, gl.FLOAT, false, 0, 0);
// set up patches texture in lbpResponseProgram
gl.uniform1i(gl.getUniformLocation(lbpResponseProgram, "u_patches"), 1);
}
// setup patchdraw program
var drVertexShader = loadShader(gl, drawResponsesVS, gl.VERTEX_SHADER);
var drFragmentShader = loadShader(gl, drawResponsesFS, gl.FRAGMENT_SHADER);
patchDrawProgram = createProgram(gl, [drVertexShader, drFragmentShader]);
gl.useProgram(patchDrawProgram);
// set the resolution/dimension of the canvas
var resolutionLocation = gl.getUniformLocation(patchDrawProgram, "u_resolutiondraw");
gl.uniform2f(resolutionLocation, newCanvasWidth, newCanvasHeight);
// set u_responses
var responsesLocation = gl.getUniformLocation(patchDrawProgram, "u_responses");
gl.uniform1i(responsesLocation, 2);
// setup patchresponse program
var prVertexShader = loadShader(gl, patchResponseVS, gl.VERTEX_SHADER);
var prFragmentShader = loadShader(gl, patchResponseFS, gl.FRAGMENT_SHADER);
patchResponseProgram = createProgram(gl, [prVertexShader, prFragmentShader]);
gl.useProgram(patchResponseProgram);
// set up vertices with rectangles
var positionLocation = gl.getAttribLocation(patchResponseProgram, "a_position");
apositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, apositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, rectangles, gl.STATIC_DRAW);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
// set up texture positions
texCoordLocation = gl.getAttribLocation(patchResponseProgram, "a_texCoord");
texCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, irectangles, gl.STATIC_DRAW);
gl.enableVertexAttribArray(texCoordLocation);
gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 0, 0);
if ('lbp' in filters || 'sobel' in filters) {
// set up gradient/lbp buffer (also used for lbp)
gl.activeTexture(gl.TEXTURE3);
var gradients = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, gradients);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, patchWidth, patchHeight*numBlocks, 0, gl.RGBA, gl.FLOAT, null);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
// set up gradient/lbp framebuffer
gbo = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, gbo);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, gradients, 0);
}
// set up buffer to draw to
gl.activeTexture(gl.TEXTURE2);
rttTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, rttTexture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, patchWidth, patchHeight*numBlocks, 0, gl.RGBA, gl.FLOAT, null);
// set up response framebuffer
fbo = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, rttTexture, 0);
gl.viewport(0, 0, patchWidth, patchHeight*numBlocks);
/* initialize some textures and buffers used later on */
patchTex = gl.createTexture();
drawRectBuffer = gl.createBuffer();
drawImageBuffer = gl.createBuffer();
drawLayerBuffer = gl.createBuffer();
}
this.getRawResponses = function(patches) {
// TODO: check patches correct length/dimension
insertPatches(patches);
// switch to correct program
gl.useProgram(patchResponseProgram);
// set u_patches to point to texture 1
gl.uniform1i(gl.getUniformLocation(patchResponseProgram, "u_patches"), 1);
// set u_filters to point to correct filter
gl.uniform1i(gl.getUniformLocation(patchResponseProgram, "u_filters"), 0);
// set up vertices with rectangles
var positionLocation = gl.getAttribLocation(patchResponseProgram, "a_position");
gl.bindBuffer(gl.ARRAY_BUFFER, apositionBuffer);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
// set up texture positions
var texCoordLocation = gl.getAttribLocation(patchResponseProgram, "a_texCoord");
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
gl.enableVertexAttribArray(texCoordLocation);
gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 0, 0);
// set framebuffer to the original one if not already using it
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
gl.viewport(0, 0, patchWidth, patchHeight*numBlocks);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER)
// draw to framebuffer
gl.drawArrays(gl.TRIANGLES, 0, patchCells*6);
//gl.finish();
var responses = drawOut('raw');
return responses;
}
this.getSobelResponses = function(patches) {
// check that it is initialized
if (!sobelInit) return;
insertPatches(patches);
/* do sobel filter on patches */
// switch to correct program
gl.useProgram(gradientResponseProgram);
// set up vertices with rectangles
var gradPositionLocation = gl.getAttribLocation(gradientResponseProgram, "a_position");
gl.bindBuffer(gl.ARRAY_BUFFER, gradAPositionBuffer);
gl.enableVertexAttribArray(gradPositionLocation);
gl.vertexAttribPointer(gradPositionLocation, 2, gl.FLOAT, false, 0, 0);
// set up texture positions
var gradTexCoordLocation = gl.getAttribLocation(gradientResponseProgram, "a_texCoord");
gl.bindBuffer(gl.ARRAY_BUFFER, gradTexCoordBuffer);
gl.enableVertexAttribArray(gradTexCoordLocation);
gl.vertexAttribPointer(gradTexCoordLocation, 2, gl.FLOAT, false, 0, 0);
// set framebuffer to the original one if not already using it
gl.bindFramebuffer(gl.FRAMEBUFFER, gbo);
gl.viewport(0, 0, patchWidth, patchHeight*numBlocks);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER)
// draw to framebuffer
gl.drawArrays(gl.TRIANGLES, 0, patchCells*6);
/* calculate responses */
gl.useProgram(patchResponseProgram);
// set patches and filters to point to correct textures
gl.uniform1i(gl.getUniformLocation(patchResponseProgram, "u_filters"), 4);
gl.uniform1i(gl.getUniformLocation(patchResponseProgram, "u_patches"), 3);
var positionLocation = gl.getAttribLocation(patchResponseProgram, "a_position");
gl.bindBuffer(gl.ARRAY_BUFFER, apositionBuffer);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
// set up texture positions
var texCoordLocation = gl.getAttribLocation(patchResponseProgram, "a_texCoord");
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
gl.enableVertexAttribArray(texCoordLocation);
gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 0, 0);
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
gl.viewport(0, 0, patchWidth, patchHeight*numBlocks);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER)
// draw to framebuffer
gl.drawArrays(gl.TRIANGLES, 0, patchCells*6);
/* get the responses */
var responses = drawOut('sobel');
return responses;
}
this.getLBPResponses = function(patches) {
// check that it is initialized
if (!lbpInit) return;
insertPatches(patches);
/* do sobel filter on patches */
// switch to correct program
gl.useProgram(lbpResponseProgram);
// set up vertices with rectangles
var lbpPositionLocation = gl.getAttribLocation(lbpResponseProgram, "a_position");
gl.bindBuffer(gl.ARRAY_BUFFER, lbpAPositionBuffer);
gl.enableVertexAttribArray(lbpPositionLocation);
gl.vertexAttribPointer(lbpPositionLocation, 2, gl.FLOAT, false, 0, 0);
// set up texture positions
var lbpTexCoordLocation = gl.getAttribLocation(lbpResponseProgram, "a_texCoord");
gl.bindBuffer(gl.ARRAY_BUFFER, lbpTexCoordBuffer);
gl.enableVertexAttribArray(lbpTexCoordLocation);
gl.vertexAttribPointer(lbpTexCoordLocation, 2, gl.FLOAT, false, 0, 0);
// set framebuffer to the original one if not already using it
gl.bindFramebuffer(gl.FRAMEBUFFER, gbo);
gl.viewport(0, 0, patchWidth, patchHeight*numBlocks);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER)
// draw to framebuffer
gl.drawArrays(gl.TRIANGLES, 0, patchCells*6);
/* calculate responses */
gl.useProgram(patchResponseProgram);
gl.uniform1i(gl.getUniformLocation(patchResponseProgram, "u_filters"), 5);
gl.uniform1i(gl.getUniformLocation(patchResponseProgram, "u_patches"), 3);
var positionLocation = gl.getAttribLocation(patchResponseProgram, "a_position");
gl.bindBuffer(gl.ARRAY_BUFFER, apositionBuffer);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
// set up texture positions
var texCoordLocation = gl.getAttribLocation(patchResponseProgram, "a_texCoord");
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
gl.enableVertexAttribArray(texCoordLocation);
gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 0, 0);
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
gl.viewport(0, 0, patchWidth, patchHeight*numBlocks);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER)
// draw to framebuffer
gl.drawArrays(gl.TRIANGLES, 0, patchCells*6);
/* get the responses */
var responses = drawOut('lbp');
return responses;
}
var insertPatches = function(patches) {
// pass patches into texture, each patch in either r, g, b or a
var patchArrayIndex = 0;
var patchesIndex1 = 0;
var patchesIndex2 = 0;
for (var i = 0;i < patchCells;i++) {
for (var j = 0;j < patchHeight;j++) {
for (var k = 0;k < patchWidth;k++) {
patchesIndex1 = i*4;
patchesIndex2 = (j*patchWidth) + k;
patchArrayIndex = ((patchSize*i) + patchesIndex2)*4;
//set r with first patch
if (patchesIndex1 < numPatches) {
patchArray[patchArrayIndex] = patches[patchesIndex1][patchesIndex2];
} else {
patchArray[patchArrayIndex] = 0;
}
//set g with 2nd patch
if (patchesIndex1+1 < numPatches) {
patchArray[patchArrayIndex + 1] = patches[patchesIndex1+1][patchesIndex2];
} else {
patchArray[patchArrayIndex + 1] = 0;
}
//set b with 3rd patch
if (patchesIndex1+2 < numPatches) {
patchArray[patchArrayIndex + 2] = patches[patchesIndex1+2][patchesIndex2];
} else {
patchArray[patchArrayIndex + 2] = 0;
}
//set a with 4th patch
if (patchesIndex1+3 < numPatches) {
patchArray[patchArrayIndex + 3] = patches[patchesIndex1+3][patchesIndex2];
} else {
patchArray[patchArrayIndex + 3] = 0;
}
}
}
}
// pass texture into an uniform
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, patchTex);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, textureWidth, textureHeight, 0, gl.RGBA, gl.FLOAT, patchArray);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
}
var insertFilter = function(filter, textureNum) {
var filterSize = filterWidth*filterHeight;
var filterArray = new Float32Array(filterSize*(numBlocks)*4);
for (var i = 0;i < numBlocks;i++) {
for (var j = 0;j < filterHeight;j++) {
for (var k = 0;k < filterWidth;k++) {
//set r with first filter
if (i*4 < filter.length) {
filterArray[((filterSize*i) + (j*filterWidth) + k)*4] = filter[i*4][(j*filterWidth) + k];
} else {
filterArray[((filterSize*i) + (j*filterWidth) + k)*4] = 0;
}
//set g with 2nd filter
if ((i*4 + 1) < filter.length) {
filterArray[((filterSize*i) + (j*filterWidth) + k)*4 + 1] = filter[(i*4)+1][(j*filterWidth) + k];
} else {
filterArray[((filterSize*i) + (j*filterWidth) + k)*4 + 1] = 0;
}
//set b with 3rd filter
if ((i*4 + 2) < filter.length) {
filterArray[((filterSize*i) + (j*filterWidth) + k)*4 + 2] = filter[(i*4)+2][(j*filterWidth) + k];
} else {
filterArray[((filterSize*i) + (j*filterWidth) + k)*4 + 2] = 0;
}
//set a with 4th filter
if ((i*4 + 3) < filter.length) {
filterArray[((filterSize*i) + (j*filterWidth) + k)*4 + 3] = filter[(i*4)+3][(j*filterWidth) + k];
} else {
filterArray[((filterSize*i) + (j*filterWidth) + k)*4 + 3] = 0;
}
}
}
}
gl.activeTexture(textureNum);
var filterTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, filterTexture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, filterWidth, filterHeight*numBlocks, 0, gl.RGBA, gl.FLOAT, filterArray);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
}
var drawOut = function(type) {
// switch programs
gl.useProgram(patchDrawProgram);
// bind canvas buffer
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.viewport(0, 0, newCanvasWidth, newCanvasHeight);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER)
gl.bindBuffer(gl.ARRAY_BUFFER, drawRectBuffer);
gl.bufferData(
gl.ARRAY_BUFFER,
drawOutRectangles,
gl.STATIC_DRAW);
var positionLocation = gl.getAttribLocation(patchDrawProgram, "a_position_draw");
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, drawImageBuffer);
gl.bufferData(
gl.ARRAY_BUFFER,
drawOutImages,
gl.STATIC_DRAW);
var textureLocation = gl.getAttribLocation(patchDrawProgram, "a_texCoord_draw");
gl.enableVertexAttribArray(textureLocation);
gl.vertexAttribPointer(textureLocation, 2, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, drawLayerBuffer);
gl.bufferData(
gl.ARRAY_BUFFER,
drawOutLayer,
gl.STATIC_DRAW);
var layerLocation = gl.getAttribLocation(patchDrawProgram, "a_patchChoice_draw");
gl.enableVertexAttribArray(layerLocation);
gl.vertexAttribPointer(layerLocation, 1, gl.FLOAT, false, 0, 0);
// draw out
gl.drawArrays(gl.TRIANGLES, 0, numPatches*6);
var responses = getOutput();
responses = unpackToFloat(responses);
// split
responses = splitArray(responses, numPatches);
// add bias
responses = addBias(responses, biases[type]);
// normalize responses to lie within [0,1]
var rl = responses.length;
for (var i = 0;i < rl;i++) {
responses[i] = normalizeFilterMatrix(responses[i]);
}
return responses;
}
var addBias = function(responses, bias) {
// do a little trick to add bias in the logit function
var biasMult;
for (var i = 0;i < responses.length;i++) {
biasMult = Math.exp(bias[i]);
for (var j = 0;j < responses[i].length;j++) {
responses[i][j] = 1/(1+((1-responses[i][j])/(responses[i][j]*biasMult)));
}
}
return responses;
}
var splitArray = function(array, parts) {
var sp = [];
var al = array.length;
var splitlength = al/parts;
var ta = [];
for (var i = 0;i < al;i++) {
if (i % splitlength == 0) {
if (i != 0) {
sp.push(ta);
}
ta = [];
}
ta.push(array[i]);
}
sp.push(ta);
return sp;
}
var getOutput = function() {
// get data