-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorph.js
389 lines (312 loc) · 11.1 KB
/
morph.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
/******* note that we always use var i = y * this.width + x to traverse arrays, and iterate through y within x ********/
/************************************ structuring element definitions / methods ***************************************/
var StructuringElement = function(d,data)
{
if(d){
this.dim = d;
}
else{
this.dim = 3;
}
if(data){
this.data = data;
}
else{
this.data = [];
// this.integerRepresentation = new UintArray(32);
for(var ind = 0; ind < this.dim * this.dim; ind++){
this.data.push(1);
}
}
}
var Contours = function(){
this.contours = [[]];
}
Contours.prototype.totalPoints = function(){
var len = 0;
this.contours.forEach(function(contour){
len += contour.length;
})
return len;
}
var MORPH_3x3_CROSS_ELEMENT = new StructuringElement(3,[0, 1, 0,
1, 1, 1,
0, 1, 0])
var MORPH_3x3_RECT_ELEMENT = new StructuringElement()
var MORPH_3x3_TOP_RIGHT_CORNER_ELEMENT = new StructuringElement(3,[-1, 1,-1,
0, 1, 1,
0, 0,-1])
var MORPH_3x3_BOTTOM_LEFT_CORNER_ELEMENT = new StructuringElement(3,[-1, 0, 0,
1, 1, 0,
-1, 1,-1])
var MORPH_3x3_TOP_LEFT_CORNER_ELEMENT = new StructuringElement(3,[ -1, 1,-1,
1, 1, 0,
-1, 0, 0])
var MORPH_3x3_BOTTOM_RIGHT_CORNER_ELEMENT = new StructuringElement(3,[ 0, 0,-1,
0, 1, 1,
-1, 1,-1])
StructuringElement.prototype.dilateOp = function(el){
SECheck(el);
for(var j = 0; j < 9; j++){
if(el.data[j] == -1)continue;
if(el.data[j] == 1 && this.data[j] == 1){
return 1;
}
}
return 0;
}
StructuringElement.prototype.erodeOp = function(el){
SECheck(el);
for(var i = 0; i < 9; i++){
if(el.data[i] == -1)continue;
if(el.data[i] != this.data[i] && el.data[i] != 1){
return 0;
}
}
return 1;
}
StructuringElement.prototype.equivalenceOp = function(el){
SECheck(el);
for(var i = 0; i < 9; i++){
if(el.data[i] == -1)continue;
if(el.data[i] != this.data[i]){
return 0;
}
}
return 1;
}
/********************************* morphological operations constructor / methods *************************************/
var Morph = function(height, width, bits){
this.height = height;
this.width = width;
if(bits){
this.data = bits;
console.log(this.height * this.width);
console.log(this.data.length);
if(this.height * this.width != this.data.length)throw 'MORPH_DIMENSION_ERROR: incorrect dimensions';
}
else{
this.data = Array.apply(null, new Array(this.height * this.width)).map(Number.prototype.valueOf,0);
}
}
Morph.prototype.constructMatrixForIndex = function(ind,d){
if(!d)d = 3;
var mat = new StructuringElement(d,0);
var halfDim = Math.floor(d / 2);
var upperLeft = ((ind - (this.height * halfDim))) - 1;
var count = 0;
for(var x = 0; x < d * d; x++){
var j = upperLeft + (x % d) + this.height * Math.floor(x / d);
if(j < this.data.length && j >= 0){
mat.data[count] = this.data[j];
}
count++;
}
return mat;
}
Morph.prototype.subtract = function(mo){
MorphCheck(mo);
for(var ind = 0; ind < this.data.length && ind < mo.data.length; ind++){
if(this.data[ind] == 1 && mo.data[ind] == 0){
continue;
}
this.data[ind] = 0;
}
return this;
}
Morph.prototype.add = function(mo){
MorphCheck(mo);
for(var ind = 0; ind < this.data.length && ind < mo.data.length; ind++){
if(mo.data[ind] == 1){
this.data[ind] = 1;
}
}
}
Morph.prototype.erodeWithElement = function(el){
alert("erode");
if(el){
SECheck(el);
}
else{
el = new StructuringElement();
}
var result = [];
var i = this.height * this.width
while(i > 0){
result.push(0)
i--;
}
for(var x = 1; x < this.width - 1; x++){
for(var y = 1; y < this.height - 1; y++){
var i = y * this.width + x;
var mat = this.constructMatrixForIndex(i, el.dim);
result[i] = el.erodeOp(mat);
}
}
this.data = result;
return this;
}
Morph.prototype.dilateWithElement = function(el){
if(el){
SECheck(el);
}
else{
el = new StructuringElement();
}
var result = Array.apply(null, new Array(this.height * this.width)).map(Number.prototype.valueOf,0);
for(var x = 1; x < this.width - 1; x++){
for(var y = 1; y < this.height - 1; y++){
var ind = x * this.height + y;
var mat = this.constructMatrixForIndex(ind, el.dim);
result[ind] = mat.dilateOp(el);
}
}
this.data = result;
return this;
}
Morph.prototype.openingWithElement = function(el){
this.dilateWithElement(el);
this.erodeWithElement(el);
}
Morph.prototype.closingWithElement = function(el){
this.erodeWithElement(el);
this.dilateWithElement(el);
}
Morph.prototype.getSubImageInRect = function(top,left,height,width){
if(left + width > this.width || top + height > this.height){
throw "MORPH_SUBIMAGE_BOUND_ERROR: check subimage bounds)"
}
var startIndex = top * this.width + left;
var endIndex = (top + height) * this.width + (left + width)
var subImage = []
var i = startIndex
while(i < endIndex){
subImage.append(this.data.splice(i, i + width))
i += this.width
}
return subImage;
}
Morph.prototype.hitMissTransform = function(){
var result = Array.apply(null, new Array(this.height * this.width)).map(Number.prototype.valueOf,0);
for(var x = 1; x < this.width - 1; x++){
for(var y = 1; y < this.height - 1; y++){
var i = y * this.width + x;
var mat = this.constructMatrixForIndex(i, 3);
result[i] = mat.equivalenceOp(MORPH_3x3_BOTTOM_LEFT_CORNER_ELEMENT) || mat.equivalenceOp(MORPH_3x3_BOTTOM_RIGHT_CORNER_ELEMENT) || mat.equivalenceOp(MORPH_3x3_TOP_LEFT_CORNER_ELEMENT) || mat.equivalenceOp(MORPH_3x3_TOP_RIGHT_CORNER_ELEMENT);
}
}
return result;
}
Morph.prototype.labelConnectedComponents = function(){
var copy = new Morph(this.height, this.width, this.data);
var labelIndex = 2;
var equivalenceClasses = new Array();
for(var y = 1; y < this.height - 1; y++){
for(var x = 1; x < this.width - 1; x++){
var i = y * this.width + x;
if(copy.data[i] > 0){
var mat = copy.constructMatrixForIndex(i, 3);
var connectedNeighborCount = 0;
var assignEquivalenceClass = function(label,i){
if(connectedNeighborCount > 0){
equivalenceClasses[String(label)] = Math.min(equivalenceClasses[String(label)],Math.min(label,copy.data[i]));
equivalenceClasses[String(copy.data[i])] = Math.min(equivalenceClasses[String(label)],Math.min(label,copy.data[i]));
copy.data[i] =Math.min(equivalenceClasses[String(label)],Math.min(label,copy.data[i]));
}
else{
equivalenceClasses[String(label)] = label;
copy.data[i] = label;
}
}
var neighborCopy0;
if(mat.data[0] > 2){
assignEquivalenceClass(mat.data[0],i);
connectedNeighborCount++;
}
if(mat.data[1] > 2){
assignEquivalenceClass(mat.data[1],i);
connectedNeighborCount++;
}
if(mat.data[2] > 2){
assignEquivalenceClass(mat.data[2],i);
connectedNeighborCount++;
}
if(mat.data[3] > 2){
assignEquivalenceClass(mat.data[3],i);
connectedNeighborCount++;
}
if( connectedNeighborCount == 0){
labelIndex++;
assignEquivalenceClass(labelIndex,i);
}
}
}
}
/*** second pass . consolidates labels ***/
var connectedSegments = new Contours();;
for(var y = 1; y < this.height - 1; y++){
for(var x = 1; x < this.width - 1; x++){
var i = x * this.height + y;
if(copy.data[i] > 0){
copy.data[i] = equivalenceClasses[String(copy.data[i])]
if(!(connectedSegments.contours[String(equivalenceClasses[String(copy.data[i])])] instanceof Array)){
connectedSegments.contours[String(equivalenceClasses[String(copy.data[i])])] = [[(2.0*x)/this.width,(2.0*y)/this.height]]
}
else{
connectedSegments.contours[String(equivalenceClasses[String(copy.data[i])])].push([(2.0*x)/this.width,(2.0*y)/this.height])
}
}
}
}
return {labeledMorph:copy,maxLabel:labelIndex,contours:connectedSegments.contours};
}
Morph.prototype.printDataAsMatrix = function(){
for(var y = 0; y < this.height ; y++){
var str = ""
for(var x = 0; x < this.width; x++){
var i = y * this.width + x;
str += this.data[i] + ' '
}
console.log(str)
}
}
Morph.prototype.iterativeThinning = function(iterations){
var hitmiss = new Morph(this.height,this.width,this.data);
var copy = new Morph(this.height,this.width,this.data);
hitmiss.data = hitmiss.hitMissTransform();
var c = 0;
while(c < iterations){
copy.subtract(hitmiss);
hitmiss.data = copy.hitMissTransform();
c++;
}
this.data = copy.data;
}
morphFromContext = function(context){
var data = this.createImageData(context.width,context.height);
return new Morph(context.height, context.width, context.createImageData(this.width,this.height))
}
SECheck = function(el){
if(!(el instanceof StructuringElement)){
throw 'MORPH_TYPE_ERROR: input must be a "StructuringElement"';
return;
}
}
MorphCheck = function(mo){
if(!(mo instanceof Morph)){
throw 'MORPH_TYPE_ERROR: input must be a "Morph" object';
return;
}
}
Array.prototype.compare = function (array) {
// if the other array is a false value, return
if(this.length == array.length){
for (var i = 0; i < Math.max(this.length, array.length); i++) {
if(this[i] != array[i])return false;
}
}
else{
return false;
}
return true;
}