forked from harthur/hog-descriptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessing.js
190 lines (154 loc) · 5.43 KB
/
processing.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
var processing = {
intensities: function(imagedata) {
if (!imagedata.data) {
// it's a canvas, extract the imagedata
var canvas = imagedata;
var context = canvas.getContext("2d");
imagedata = context.getImageData(0, 0, canvas.width, canvas.height);
}
var lumas = new Array(imagedata.height);
for (var y = 0; y < imagedata.height; y++) {
lumas[y] = new Array(imagedata.width);
for (var x = 0; x < imagedata.height; x++) {
var i = x * 4 + y * 4 * imagedata.width;
var r = imagedata.data[i],
g = imagedata.data[i + 1],
b = imagedata.data[i + 2],
a = imagedata.data[i + 3];
var luma = a == 0 ? 1 : (r * 299/1000 + g * 587/1000
+ b * 114/1000) / 255;
lumas[y][x] = luma;
}
}
return lumas;
},
gradients: function(canvas) {
var intensities = this.intensities(canvas);
return this._gradients(intensities);
},
_gradients: function(intensities) {
var height = intensities.length;
var width = intensities[0].length;
var gradX = new Array(height);
var gradY = new Array(height);
for (var y = 0; y < height; y++) {
gradX[y] = new Array(width);
gradY[y] = new Array(width);
var row = intensities[y];
for (var x = 0; x < width; x++) {
var prevX = x == 0 ? 0 : intensities[y][x - 1];
var nextX = x == width - 1 ? 0 : intensities[y][x + 1];
var prevY = y == 0 ? 0 : intensities[y - 1][x];
var nextY = y == height - 1 ? 0 : intensities[y + 1][x];
// kernel [-1, 0, 1]
gradX[y][x] = -prevX + nextX;
gradY[y][x] = -prevY + nextY;
}
}
return {
x: gradX,
y: gradY
};
},
gradientVectors: function(canvas) {
var intensities = this.intensities(canvas);
return this._gradientVectors(intensities);
},
_gradientVectors: function(intensities) {
var height = intensities.length;
var width = intensities[0].length;
var vectors = new Array(height);
for (var y = 0; y < height; y++) {
vectors[y] = new Array(width);
for (var x = 0; x < width; x++) {
var prevX = x == 0 ? 0 : intensities[y][x - 1];
var nextX = x == width - 1 ? 0 : intensities[y][x + 1];
var prevY = y == 0 ? 0 : intensities[y - 1][x];
var nextY = y == height - 1 ? 0 : intensities[y + 1][x];
// kernel [-1, 0, 1]
var gradX = -prevX + nextX;
var gradY = -prevY + nextY;
vectors[y][x] = {
mag: Math.sqrt(Math.pow(gradX, 2) + Math.pow(gradY, 2)),
orient: Math.atan2(gradY, gradX)
}
}
}
return vectors;
},
drawGreyscale: function(canvas) {
var ctx = canvas.getContext('2d');
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var intensities = this.intensities(canvas);
for (var y = 0; y < imageData.height; y++) {
for (var x = 0; x < imageData.width; x++) {
var i = (y * 4) * imageData.width + x * 4;
var luma = intensities[y][x] * 255;
imageData.data[i] = luma;
imageData.data[i + 1] = luma;
imageData.data[i + 2] = luma;
imageData.data[i + 3] = 255;
}
}
ctx.putImageData(imageData, 0, 0, 0, 0, imageData.width, imageData.height);
return canvas;
},
drawGradient: function(canvas, dir) {
var ctx = canvas.getContext("2d");
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var gradients = this.gradients(canvas);
var grads = gradients[dir || "x"];
for (var y = 0; y < imageData.height; y++) {
for (var x = 0; x < imageData.width; x++) {
var i = (y * 4) * imageData.width + x * 4;
var grad = Math.abs(grads[y][x]) * 255;
imageData.data[i] = grad;
imageData.data[i + 1] = grad;
imageData.data[i + 2] = grad;
imageData.data[i + 3] = 255;
}
}
ctx.putImageData(imageData, 0, 0, 0, 0, imageData.width, imageData.height);
return canvas;
},
drawMagnitude: function(canvas) {
var ctx = canvas.getContext("2d");
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var vectors = processing.gradientVectors(canvas);
for (var y = 0; y < imageData.height; y++) {
for (var x = 0; x < imageData.width; x++) {
var i = (y * 4) * imageData.width + x * 4;
var mag = Math.abs(vectors[y][x].mag) * 255;
imageData.data[i] = mag;
imageData.data[i + 1] = mag;
imageData.data[i + 2] = mag;
imageData.data[i + 3] = 255;
}
}
ctx.putImageData(imageData, 0, 0, 0, 0, imageData.width, imageData.height);
return canvas;
},
drawOrients: function(canvas) {
var ctx = canvas.getContext("2d");
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
var vectors = processing.gradientVectors(canvas);
for (var y = 0; y < imageData.height; y++) {
for (var x = 0; x < imageData.width; x++) {
var i = (y * 4) * imageData.width + x * 4;
var orient = Math.abs(vectors[y][x].orient);
orient *= (180 / Math.PI);
if (orient < 0) {
orient += 180;
}
orient *= 255 / 180;
imageData.data[i] = orient;
imageData.data[i + 1] = orient;
imageData.data[i + 2] = orient;
imageData.data[i + 3] = 255;
}
}
ctx.putImageData(imageData, 0, 0, 0, 0, imageData.width, imageData.height);
return canvas;
}
}
module.exports = processing;