-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathADAblock_20160212.ijm
5612 lines (4926 loc) · 207 KB
/
ADAblock_20160212.ijm
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
// ImageJ BCP Structure Analysis Framework //{{{ collapse all...
// ABOUT THE CODE
// ADAblock: Automated Defect Analysis of Block Copolymers
// ImageJ Macro Code Implementation of Defect Analysis Algorithm
// Version: 0.50i Date: 2015.01.30 Author: Jeffrey N. Murphy
// Updated versions available at:
// The algorithm is described in the following paper:
// ..................................................
// DOI:
// SETTINGS /{{{
program_name = "ADAblock";
program_version = "v1.00"; prog_version = 1.00;
modification_date = "2015.06.02";
d_mode = 1; //(diagnostc mode)
requires("1.49o"); // Requires Latest Version of ImageJ
// http://fiji.sc/wiki/index.php/Auto_Threshold
savestages = 0; /** Saves more images if set to 1 **/
// END OF SETTINGS //}}}
// MODIFIABLE DEFINED VARIABLES //{{{
// Constants whose values can be modified.
image_log_name = "Output_Log";
period_limits_nm = newArray(10,150); // minimum & maximum period in nanometres
period_default_nm = 50;
auto_smoothing_factor = 0.15;
period_range_min_nm = 4; //nm // For FFT period auto-detection
period_range_max_nm = 100; //nm // For FFT period auto-detection
binary_grooming = true; // Include a binary grooming step to reduce extra defects.
// END OF MODIFIABLE DEFINED VARIABLES //}}}
// DEPENDENCIES //{{{
// Exit if necessary plugins are not installed.
// "Auto Local Threshold" Plugin
List.setCommands;
if (List.get("Auto Local Threshold")!="") {
// plugin is installed
} else {
print("Install: AUTO LOCAL THRESHOLD");
print("URL: http://bit.ly/plugin_ALT");
exit("AUTO LOCAL THRESHOLD\nnot installed.");
}
// END OF DEPENDENCIES //}}}
// FUNCTIONS //{{{
// 000 Output Tags & Data //{{{
var output_tags = newArray(0);
var output_data= newArray(0);
var output_labels = newArray(0);
function outputTD(tag,data){
output_tags = Array.concat(output_tags,tag); output_data = Array.concat(output_data,data);
}
function outputL(label){
output_labels = Array.concat(output_labels,label);
}
//}}}
// 001 AppendToArray //{{{
// Appends a value to the array
// Returns the new array
// Format: array = AppendToArray(array,value);
function AppendToArray(array,value) {
temp_array = newArray(lengthOf(array) + 1);
for(i = 0; i < lengthOf(array); i++){
temp_array[i] = array[i];
}
temp_array[lengthOf(temp_array) - 1] = value;
array = temp_array;
return array;
} //}}}
// 002 CheckInArray //{{{
// Checks if a value is already in the array
// Returns true or false
function CheckInArray(array,value) {
i = 0; check = false;
while(i < lengthOf(array)){
if(array[i]==value){ i = lengthOf(array); check = true; }
else{i++;}
}
return check;
} //}}}
// 003 Indexer //{{{
// Indexer: Converts (x,y) coordinates to values
// Only works for images up to 9999 x 9999
// Allows for creating a single list of values
function xyIndexer(x,y) {
value = 100000000 + 10000*x + y;
return value;
} //}}}
// 004 DeIndexer //{{{
// DeIndexer: Converts values to (x,y) coordinates
// Returns Array (x,y)
function xyDeIndexer(value) {
x = floor((value - 100000000)/10000);
y = value - 100000000 - x*10000;
xy = newArray(x,y);
return xy;
} //}}}
// 005 Circuit Value //{{{
// Circuit Value (Count the Jumps)
// Returns Number of Jumps Required (value)
// Note: outside of image, pixel value is 0
function circuitValue(x,y,value) {
count = 0;
if(getPixel(x+1,y+1)-getPixel(x,y+1)>=value){count++;}
if(getPixel(x+1,y)-getPixel(x+1,y+1)>=value){count++;}
if(getPixel(x+1,y-1)-getPixel(x+1,y)>=value){count++;}
if(getPixel(x,y-1)-getPixel(x+1,y-1)>=value){count++;}
if(getPixel(x-1,y-1)-getPixel(x,y-1)>=value){count++;}
if(getPixel(x-1,y)-getPixel(x-1,y-1)>=value){count++;}
if(getPixel(x-1,y+1)-getPixel(x-1,y)>=value){count++;}
if(getPixel(x,y+1)-getPixel(x-1,y+1)>=value){count++;}
return count;
} //}}}
// 006 Four-Connected Circuit Value //{{{
// Circuit Value (Count the Jumps), excluding diaconals (+)
// Returns Number of Jumps Required (only values: 0,1,2)
// Note: outside of image, pixel value is 0
function circuitValueFour(x,y,value) {
count = 0;
if(getPixel(x+1,y)-getPixel(x,y-1)>=value){count++;}
if(getPixel(x,y-1)-getPixel(x-1,y)>=value){count++;}
if(getPixel(x-1,y)-getPixel(x,y+1)>=value){count++;}
if(getPixel(x,y+1)-getPixel(x+1,y)>=value){count++;}
return count;
}
// 006b Four-Connected X
function circuitValueX(x,y,value) {
count = 0;
if(getPixel(x+1,y+1)-getPixel(x+1,y-1)>=value){count++;}
if(getPixel(x+1,y-1)-getPixel(x-1,y-1)>=value){count++;}
if(getPixel(x-1,y-1)-getPixel(x-1,y+1)>=value){count++;}
if(getPixel(x-1,y+1)-getPixel(x+1,y+1)>=value){count++;}
return count;
} //}}}
// 007 Neighbour Value //{{{
// Neighbour Value (or Point Value)
// Returns the number of 8-connected pixels (>= threshold value)
// Note: outside of image, pixel value is 0
function neighbourValue(x,y,value) {
count = 0;
if(getPixel(x,y+1)>=value){count++;}
if(getPixel(x+1,y+1)>=value){count++;}
if(getPixel(x+1,y)>=value){count++;}
if(getPixel(x+1,y-1)>=value){count++;}
if(getPixel(x,y-1)>=value){count++;}
if(getPixel(x-1,y-1)>=value){count++;}
if(getPixel(x-1,y)>=value){count++;}
if(getPixel(x-1,y+1)>=value){count++;}
return count;
} //}}}
// 008 Neighbour Value Exact //{{{
// Neighbour Value (or Point Value)
// Returns the number of 8-connected pixels (>= threshold value)
// Note: outside of image, pixel value is 0
function neighbourValueExact(x,y,value) {
count = 0;
if(getPixel(x,y+1)==value){count++;}
if(getPixel(x+1,y+1)==value){count++;}
if(getPixel(x+1,y)==value){count++;}
if(getPixel(x+1,y-1)==value){count++;}
if(getPixel(x,y-1)==value){count++;}
if(getPixel(x-1,y-1)==value){count++;}
if(getPixel(x-1,y)==value){count++;}
if(getPixel(x-1,y+1)==value){count++;}
return count;
}
function neighbourValueExactFour(x,y,value) {
count = 0;
if(getPixel(x,y+1)==value){count++;}
if(getPixel(x+1,y)==value){count++;}
if(getPixel(x,y-1)==value){count++;}
if(getPixel(x-1,y)==value){count++;}
return count;
} //}}}
// 009 FollowTwo //{{{
// Follows a series of points in a skeletonized image
// Start at x,y Terminal Point; ends at Junction Point
// Requires ~4755 msec for "snake" of entire 1280x896 image
// Returns {x, y, distance travelled, pixels}
function followTwo(x,y,value) {
condition = true; pixels = 0; u = x; v = y; a = 0; b = 0;
while(condition){
// Find which direction to go:
count = 0; i = 0; j = 0; ip = u-x ; jp = v-y ;
// count = neighbours; i,j = direction to be moved; u,v = previous position; a = t-movement; b = x-movemnt;
if(getPixel(x,y+1)>=value){count++; if(ip == 0 && jp == 1){ }else{ i = 0; j = 1; }}
if(getPixel(x+1,y+1)>=value){count++; if(ip == 1 && jp == 1){ }else{ i = 1; j = 1; }}
if(getPixel(x+1,y)>=value){count++; if(ip == 1 && jp == 0){ }else{ i = 1; j = 0; }}
if(getPixel(x+1,y-1)>=value){count++; if(ip == 1 && jp == -1){ }else{ i = 1; j = -1; }}
if(getPixel(x,y-1)>=value){count++; if(ip == 0 && jp == -1){ }else{ i = 0; j = -1; }}
if(getPixel(x-1,y-1)>=value){count++; if(ip == -1 && jp == -1){ }else{ i = -1; j = -1; }}
if(getPixel(x-1,y)>=value){count++; if(ip == -1 && jp == 0){ }else{ i = -1; j = 0; }}
if(getPixel(x-1,y+1)>=value){count++; if(ip == -1 && jp == 1){ }else{ i = -1; j = 1; }}
// setPixel(x,y,value); // print(steps + " " + x + " " + y);
// Check to see if we should continue:
if(pixels > 0 && count == 2){ u = x; v = y; x += i; y+= j; pixels++;}
else if(pixels==0 && count<=2){ u = x; v = y; x += i; y+= j; pixels++;}
else {condition = false; pixels++; i = 0; j = 0;}
if(abs(i)+abs(j) == 1){a++;} else if(abs(i)+abs(j) == 2){b++;}
}
distance = a + sqrt(2)*b; result = newArray(x,y,distance,pixels);
return result;
} //}}}
// 010 FollowErase //{{{
// Follows a series of points in a skeletonized image
// Start at x,y Terminal Point; deletes last unnecessary Junction Point
// Requires ~4708 msec for "snake" of entire 1280x896 image
// Returns {x, y, pixels}
function followErase(x,y,value) {
condition = true; pixels = 0; erase = 10;
while(condition){
// Find which direction to go:
ocount = 0; dcount = 0; i = 0; j = 0;
// count = neighbours; i,j = direction to be moved; u,v = previous position; a = t-movement; b = x-movemnt;
if(getPixel(x,y+1)>=value){ocount++; i = 0; j = 1;}
if(getPixel(x+1,y+1)>=value){dcount++; i = 1; j = 1;}
if(getPixel(x+1,y)>=value){ocount++; i = 1; j = 0;}
if(getPixel(x+1,y-1)>=value){dcount++; i = 1; j = -1;}
if(getPixel(x,y-1)>=value){ocount++; i = 0; j = -1;}
if(getPixel(x-1,y-1)>=value){dcount++; i = -1; j = -1;}
if(getPixel(x-1,y)>=value){ocount++; i = -1; j = 0;}
if(getPixel(x-1,y+1)>=value){dcount++; i = -1; j = 1;}
count = ocount + dcount;
if(count==1){setPixel(x,y,erase); pixels++; x += i; y+= j;}
else { condition = false;
// Measure Circuit Value
cvcount = 0; // Eight-connected
if(getPixel(x+1,y+1)-getPixel(x,y+1)>=value){cvcount++;}
if(getPixel(x+1,y)-getPixel(x+1,y+1)>=value){cvcount++;}
if(getPixel(x+1,y-1)-getPixel(x+1,y)>=value){cvcount++;}
if(getPixel(x,y-1)-getPixel(x+1,y-1)>=value){cvcount++;}
if(getPixel(x-1,y-1)-getPixel(x,y-1)>=value){cvcount++;}
if(getPixel(x-1,y)-getPixel(x-1,y-1)>=value){cvcount++;}
if(getPixel(x-1,y+1)-getPixel(x-1,y)>=value){cvcount++;}
if(getPixel(x,y+1)-getPixel(x-1,y+1)>=value){cvcount++;}
// Check Conditions
if(cvcount<2){setPixel(x,y,erase); pixels++;}
else if(cvcount==2 && dcount==2){
scount = 0; // Up,Left,Down,Right;
if(getPixel(x,y+1)>=value){scount++; i = 0; j = 1;}
if(getPixel(x+1,y)>=value){scount++; i = 1; j = 0;}
if(getPixel(x,y-1)>=value){scount++; i = 0; j = -1;}
if(getPixel(x-1,y)>=value){scount++; i = -1; j = 0;}
setPixel(x,y,erase); pixels++;
}
}
}
result = newArray(x,y,pixels);
return result;
} //}}}
// 011 Set Foreground Index //{{{
// Sets the Foreground Colour using an Index
// Value corresponds to Color when using FloodFill
function setForegroundIndex(value) {
getLut(reds,greens,blues);
setForegroundColor(reds[value],greens[value],blues[value]);
} //}}}
// 012 LUT Functions //{{{
// LUTS One
function LUTs_001(true_or_false){
if(true_or_false){
reds = newArray(256); greens = newArray(256); blues = newArray(256);
for(n=0; n<256; n++){ reds[n] = 255-n; greens[n] = 255-n; blues[n] = 255-n;}
// Array.fill(reds, 0); Array.fill(greens, 0); Array.fill(blues, 0)
value = 1; reds[value] = 255; greens[value] = 200; blues[value] = 200;
value = 2; reds[value] = 120; greens[value] = 255; blues[value] = 120;
value = 254; reds[value] = 0; greens[value] = 0; blues[value] = 170;
value = 253; reds[value] = 0; greens[value] = 165; blues[value] = 120;
setLut(reds, greens, blues);
}
}
// LUT Index Modification
function LUT_index_mod(index,r,g,b){
getLut(reds, greens, blues);
reds[index] = r; greens[index] = g; blues[index] = b;
setLut(reds, greens, blues);
}
//}}}
// 013 Weighted Least Squares Algorithm //{{{
// For estimation of line widths
// Array Summing Function
function arraySumP3(ArrayA,ArrayB,ArrayC){
sum = 0;
if(ArrayB==1){ for(n=0; n<ArrayA.length; n++){ sum += ArrayA[n];} }
else if(ArrayC==1){ for(n=0; n<ArrayA.length; n++){ sum += ArrayA[n]*ArrayB[n];} }
else { for(n=0; n<ArrayA.length; n++){ sum += ArrayA[n]*ArrayB[n]*ArrayC[n];} }
return sum;
}
// Simple Linear Regression (Weighted Least Squares)
// for measuring Average Width, using a series of Perimeters and Areas
// Formula: A = wP-C; A = area; P = perimeter; w = half-width; C = a constant
// Xi = Perimeters. Yi = Areas. (both are arrays)
// options: unweighted, inv_sqrt_area, median_distance
function particleWLSQ(Xi,Yi,Weighting_Method,iterations){
N = Xi.length;
// Weighting array
Wi_not_norm = newArray(N); Wi = newArray(N);
if(Weighting_Method=="unweighted"){ Array.fill(Wi_not_norm,1); }
else if(Weighting_Method=="inv_sqrt_area"){ for(n=0; n<N; n++){ Wi_not_norm[n] = 1/(sqrt(Yi[n])); } }
else if(Weighting_Method=="median_distance" && N >= 3){
if(N%2==1){
n_med = (N-1)/2+1; if(n_med>=Xi.length-1){n_med = Xi.length-2;}
Xi_med = Xi[n_med+1]/3 + Xi[n_med]/3 + Xi[n_med-1]/3;
}
else{
Xi_med = 0.5*Xi[floor(N/2)]+0.5*Xi[floor(N/2+1)];
}
for(n=0; n<N; n++){
Wi_not_norm[n] = 1/pow(1+abs(Xi[n]-Xi_med),1.5);
}
}
else { Array.fill(Wi_not_norm,1);}
Wi_Normalization = arraySumP3(Wi_not_norm,1,1);
for(n=0; n<N; n++){ Wi[n] = Wi_not_norm[n] / Wi_Normalization; }
Wi_method = Wi_not_norm;
// Calculate All Sums
S_Yi = arraySumP3(Yi,1,1);
S_Xi = arraySumP3(Xi,1,1);
S_Wi = arraySumP3(Wi,1,1);
S_XiXi = arraySumP3(Xi,Xi,1);
S_YiXi = arraySumP3(Yi,Xi,1);
S_WiWi = arraySumP3(Wi,Wi,1);
S_WiXi = arraySumP3(Wi,Xi,1);
S_WiYi = arraySumP3(Wi,Yi,1);
S_WiXiXi = arraySumP3(Wi,Xi,Xi);
S_WiXiYi = arraySumP3(Wi,Xi,Yi);
// Form of Y = aX+b
Alpha = ( S_YiXi - (S_Yi*S_Xi/N)) / (S_XiXi - (S_Xi*S_Xi/N));
Beta = (S_Yi/N) - Alpha*(S_Xi/N);
AlphaW = (S_Wi*S_WiXiYi - S_WiXi*S_WiYi)/(S_Wi*S_WiXiXi - S_WiXi*S_WiXi);
BetaW = (S_WiXiXi*S_WiYi - (S_WiXi*S_WiXiYi))/(S_Wi*S_WiXiXi - S_WiXi*S_WiXi);
// Iterative Re-Weighting: Normalization
BetaWW = BetaW; AlphaWW = AlphaW;
for(m=0; m<iterations; m++){
Ei = newArray(N); Y_fit = newArray(N);
for(n=0; n<N; n++){
Y_fit[n] = AlphaWW*Xi[n]+BetaWW;
Ei[n] = abs(Y_fit[n] - Yi[n]);
Wi_not_norm[n] = 1/Ei[n]*Wi_method[n];
}
Wi_Normalization = arraySumP3(Wi_not_norm,1,1);
for(n=0; n<N; n++){ Wi[n] = Wi_not_norm[n] / Wi_Normalization; }
// Re-Calculate Wi-Sums
S_Wi = arraySumP3(Wi,1,1);
S_WiWi = arraySumP3(Wi,Wi,1);
S_WiXi = arraySumP3(Wi,Xi,1);
S_WiYi = arraySumP3(Wi,Yi,1);
S_WiXiXi = arraySumP3(Wi,Xi,Xi);
S_WiXiYi = arraySumP3(Wi,Xi,Yi);
// Form of Y = aX+b
BetaWW = (S_WiXiXi*S_WiYi - (S_WiXi*S_WiXiYi))/(S_Wi*S_WiXiXi - S_WiXi*S_WiXi);
AlphaWW = (S_Wi*S_WiXiYi - S_WiXi*S_WiYi)/(S_Wi*S_WiXiXi - S_WiXi*S_WiXi);
}
W_Width_Calc = 2*AlphaWW;
//Results: Alpha, Beta, AlphaW, BetaW, AlphaWW, BetaWW, W_Width_Calc
results = newArray(W_Width_Calc, Alpha, Beta, AlphaW, BetaW, AlphaWW, BetaWW, iterations);
return results;
}
// Array Maximum (not presently used)
// returns the maximum value of the array and the position in the array
function arrayMax(array){
max_value = array[0]; i_max = 0;
for(i=0; i<array.length; i++){
if(array[i] > max_value){max_value = array[i]; i_max = i;}
}
result = newArray(i_max,max_value);
return result;
}
// Array Maxima
// returns the maximum values of the array and the positions in the array
function arrayMaxima(Value_array,n_array,Value,n){
for(i=0; i<Value_array.length; i++){
if(Value_array[i]<Value){
for(j=Value_array.length-1; j>i; j--){
Value_array[j] = Value_array[j-1];
n_array[j] = n_array[j-1];
}
Value_array[i] = Value;
n_array[i] = n;
result = Array.concat(Value_array,n_array);
return result;
}
}
result = Array.concat(Value_array,n_array);
return result;
}
// Simple Linear Regression (Weighted Least Squares) **FULL**
// As above, except that Perimeters and Areas gathered from results table directly
// includes steps required for *exclusion* of small-area particles (min_area) ## by area
// and exclusion via dropping the largest area values in the series (large_drop) ## by count
function particleWLSQfull(results_i,results_n,Weighting_Method,iterations,min_area,large_drops){
count = -1*large_drops; drops = newArray(large_drops); Array.fill(drops, 0);
n_drops = newArray(large_drops); Array.fill(n_drops, -1);
if(large_drops>0){
for(n=results_i; n<results_n; n++){
area_value = getResult("Area",n);
if(area_value >= min_area){
count +=1;
if(area_value > drops[large_drops-1]){
max_values = arrayMaxima(drops,n_drops,area_value,n);
drops = Array.trim(max_values, large_drops);
n_drops = Array.slice(max_values,large_drops,max_values.length);
}
}
}
if(count<2){ results = newArray(-1, -1, -1, -1, -1, -1, -1, -1); return results; }
m = 0;
Xi = newArray(count); Yi = newArray(count);
for(n=results_i; n<results_n; n++){
drops_onoff = true;
for(nn=0; nn<drops.length; nn++){
if(n_drops[nn]==n){drops_onoff = false;}
}
if((getResult("Area",n) >= min_area) & drops_onoff){
Yi[m] = getResult("Area",n);
Xi[m] = getResult("Perim.",n);
m ++;
}
}
} else {
for(n=results_i; n<results_n; n++){
if(getResult("Area",n) >= min_area){
count+= 1;
}
}
if(count<3){ results = newArray(-1, -1, -1, -1, -1, -1, -1, -1); return results; }
m = 0;
Xi = newArray(count); Yi = newArray(count);
for(n=results_i; n<results_n; n++){
if(getResult("Area",n) >= min_area){
Yi[m] = getResult("Area",n);
Xi[m] = getResult("Perim.",n);
m ++;
}
}
}
results = particleWLSQ(Xi,Yi,Weighting_Method,iterations);
return results;
} //}}}
// 014 Edge Walk Pixels //{{{
// follows the edge of the image
// Checks to see if how many times the edge is touched by an object
// returns number of times touching and number of pixels touching
function edgeWalkPixels(colour_object,zero_x,zero_y,width,height) {
jump = 1; j_count = 0; p_count = 0;
px = getPixel(zero_x,zero_y); if(px==colour_object){old=1;}else{old=0;}
top_count = 0;
y = zero_y;
for(x=zero_x+1; x<width; x++){ px = getPixel(x,y); if(px==colour_object){new=1; p_count++;}else{new=0;} if(new-old==jump){j_count++;} old=new; }
top_count = p_count;
x = width-1;
for(y=zero_y+1; y<height; y++){ px = getPixel(x,y); if(px==colour_object){new=1; p_count++;}else{new=0;} if(new-old==jump){j_count++;} old=new; }
right_count = p_count - top_count;
y = height-1;
for(x=width-2; x>=zero_x; x--){ px = getPixel(x,y); if(px==colour_object){new=1; p_count++;}else{new=0;} if(new-old==jump){j_count++;} old=new; }
bottom_count = p_count - (right_count + top_count);
x = zero_x;
for(y=height-2; y>=zero_y; y--){ px = getPixel(x,y); if(px==colour_object){new=1; p_count++;}else{new=0;} if(new-old==jump){j_count++;} old=new; }
left_count = p_count - (right_count + top_count + bottom_count);
if(top_count>0){tc = 1000;}else{tc = 0;}
if(right_count>0){rc = 100;}else{rc = 0;}
if(bottom_count>0){bc = 10;}else{bc = 0;}
if(left_count>0){lc = 1;}else{lc = 0;}
sum = tc + rc + bc + lc; //!@#$ could used a more nuanced approach, but only an issue with small patterns.
results = newArray(j_count,p_count,sum);
return results;
}
// Edge Pixel Count: implements Edge Walk Pixels
// Requires results table.
// No knowledge of phases is necessary
// except that colour_object cannot be the
function edgePixelCount(colour_object,zero_x,zero_y,w,h){
for(n=0; n<nResults; n++){
xo = getResult("XStart",n); yo = getResult("YStart",n); xy_value = getPixel(xo,yo);
bx = getResult("BX",n); by = getResult("BY",n);
Lx = getResult("Width",n); Ly = getResult("Height",n);
if(bx==0 || by==0 || bx+Lx==w || by+Ly==h){
setResult("OnEdge",n,1);
setForegroundIndex(colour_object);
floodFill(xo,yo);
result = edgeWalkPixels(colour_object,zero_x,zero_y,w,h);
setResult("EdgeTouch",n,result[0]);
setResult("EdgePixels",n,result[1]);
setResult("Sides",n,result[2]);
//if(n<nPositive){setForegroundIndex(255);}else{setForegroundIndex(0);}
setForegroundIndex(xy_value); // Replacement line
floodFill(xo,yo);
}
else{ setResult("OnEdge",n,0); }
}
updateResults(); return 1;
} //}}}
// 015 CHECK INSIDE //{{{
// Discover whether objects are enclosed inside of another. Enclosure indicates exterior lines or a defect
// contains particles: "Contains" = 1
// enclosed by particles: "Enclosed"
function checkInside(nPositive,nTotal,cvalue){
if(isNaN(getResult("Contains",0))){for(n=0; n<nTotal;n++){setResult("Contains",n,0);}}
if(isNaN(getResult("Enclosed",0))){for(n=0; n<nTotal;n++){setResult("Enclosed",n,0);}}
if(isNaN(getResult("Enclosed.By",0))){for(n=0; n<nTotal;n++){setResult("Enclosed.By",n,-1);}}
for(i=0; i<2; i++){
if(i==0){start_A = 0; end_B = nPositive; start_C = nPositive; end_D = nTotal;}
else{start_A = nPositive; end_B = nTotal; start_C = 0; end_D = nPositive;}
for(n=start_A; n<end_B; n++){
m_inside_check = newArray(0);
for(m=start_C; m<end_D; m++){
nBx = getResult("BX",n); nBy = getResult("BY",n);
nLx = getResult("Width",n); nLy = getResult("Height",n);
mBx = getResult("BX",m); mBy = getResult("BY",m);
mLx = getResult("Width",m); mLy = getResult("Height",m);
// if "m" entry is inside of "n" entry
if(mBx>nBx && mBy>nBy && (mBx+mLx)<(nBx+nLx) && (mBy+mLy)<(nBy+nLy)){
// Checks to see if neighbouring values changed after flood fill. Indicates enclosure.
// Enclosed ones, via ">" (not ">=" definition have first pixel inside regardless.
m_inside_check = Array.concat(m_inside_check,m);
}
//else{if(getResult("Lines",n)!=1){setResult("Lines",n,0);}}
}
// Flood Fill part is moved outside for speed...
if(m_inside_check.length>0){
nx = getResult("XStart",n); ny = getResult("YStart",n); nxy_value = getPixel(nx,ny);
setForegroundIndex(cvalue); floodFill(nx,ny,"8-connected");
for(m=0; m<m_inside_check.length; m++){
mx = getResult("XStart",m_inside_check[m]); my = getResult("YStart",m_inside_check[m]);
mn_connection = neighbourValueExact(mx,my,cvalue);
if(mn_connection>0){
count = getResult("Contains",n); setResult("Contains",n,count+1);
setResult("Enclosed",m_inside_check[m],1); setResult("Enclosed.By",m_inside_check[m],n);
}
}
setForegroundIndex(nxy_value); floodFill(nx,ny,"8-connected"); // return to original value
}
}
}
updateResults(); return 1;
}
function checkInsideEDGE(nPositive,nTotal,cvalue,w,h){
if(isNaN(getResult("Contains",0))){for(n=0; n<nTotal;n++){setResult("Contains",n,0);}}
if(isNaN(getResult("Enclosed",0))){for(n=0; n<nTotal;n++){setResult("Enclosed",n,0);}}
if(isNaN(getResult("Enclosed.By",0))){for(n=0; n<nTotal;n++){setResult("Enclosed.By",n,-1);}}
for(i=0; i<2; i++){
if(i==0){start_A = 0; end_B = nPositive; start_C = nPositive; end_D = nTotal;}
else{start_A = nPositive; end_B = nTotal; start_C = 0; end_D = nPositive;}
for(n=start_A; n<end_B; n++){
m_inside_check = newArray(0);
for(m=start_C; m<end_D; m++){
nBx = getResult("BX",n); nBy = getResult("BY",n);
nLx = getResult("Width",n); nLy = getResult("Height",n);
mBx = getResult("BX",m); mBy = getResult("BY",m);
mLx = getResult("Width",m); mLy = getResult("Height",m);
// if "m" entry is inside of "n" entry
if(mBx>=nBx && mBy>=nBy && (mBx+mLx)<=(nBx+nLx) && (mBy+mLy)<=(nBy+nLy)){
// Checks to see if neighbouring values changed after flood fill. Indicates enclosure.
// Enclosed ones, via ">" (not ">=" definition have first pixel inside regardless.
m_inside_check = Array.concat(m_inside_check,m);
}
//else{if(getResult("Lines",n)!=1){setResult("Lines",n,0);}}
}
// Flood Fill part is moved outside for speed...
if(m_inside_check.length>0){
nx = getResult("XStart",n); ny = getResult("YStart",n); nxy_value = getPixel(nx,ny);
setForegroundIndex(cvalue); floodFill(nx,ny,"8-connected");
if( (nx==0 || nx==w-1) || (ny==0 || ny==h-1) ){ nxny = furthestNonEdgePixel(nx,ny,w,h,cvalue); nx = nxny[0]; ny = nxny[1];}
for(m=0; m<m_inside_check.length; m++){
mx = getResult("XStart",m_inside_check[m]); my = getResult("YStart",m_inside_check[m]);
mn_connection = neighbourValueExact(mx,my,cvalue);
if(mn_connection>0){
count = getResult("Contains",n); setResult("Contains",n,count+1);
setResult("Enclosed",m_inside_check[m],1); setResult("Enclosed.By",m_inside_check[m],n);
}
}
setForegroundIndex(nxy_value); floodFill(nx,ny,"8-connected"); // return to original value
}
}
}
updateResults(); return 1;
}//}}}
// 016 Conditions True //{{{
// If x conditions are *true*, then
// e.g.
// array_labels = newArray("Area","Circ.");
// array_conditions = newArray(">=",">");
// array_values = newArray(50,0.80);
// column is string to name COLUMN in Results Table
// n_start = 0; n_end = nResults
function conditionsTrue(array_labels,array_conditions,array_values,n_start,n_end,column,x){
// array_conditions: ==,!=,>,<,>=,<=
product_sum = 0; sum_sum = 0;
cond_eval = newArray(array_values.length);
for(n=n_start; n<n_end; n++){
for(m=0; m<array_values.length; m++){
label = array_labels[m];
cond = array_conditions[m];
value = array_values[m];
L_value = getResult(label,n);
macro_expression = "result="+L_value+cond+value+"; return toString(result);";
cond_eval[m] = eval(macro_expression);
}
product = 1; sum = 0;
for(m=0; m<cond_eval.length; m++){
product = product * cond_eval[m];
sum += cond_eval[m];
}
if(sum >= x){result = 1; sum_sum += 1;}else{result = 0;}
setResult(column,n,result);
product_sum += product;
}
updateResults(); return sum_sum;
}
// Version with weighs attached
// prod_true = array of values if true
// prod_false = array of values if true
// binary = 1 or 0. if true, results will be 1 or 0; if false, they will be
function conditionsWeighted(array_labels,array_conditions,array_values,prod_true,prod_false,binary,threshold,n_start,n_end,column){
// array_conditions: ==,!=,>,<,>=,<=
product_sum = 0; sum_sum = 0;
cond_eval = newArray(array_values.length);
for(n=n_start; n<n_end; n++){
for(m=0; m<array_values.length; m++){
label = array_labels[m];
cond = array_conditions[m];
value = array_values[m];
L_value = getResult(label,n);
macro_expression = "result="+L_value+cond+value+"; return toString(result);";
if(eval(macro_expression)){cond_eval[m] = prod_true[m]}else{cond_eval[m] = prod_false[m];}
}
product = 1; sum = 0;
for(m=0; m<cond_eval.length; m++){
product = product * cond_eval[m];
sum += cond_eval[m];
}
if(binary){
if(sum >= threshold){result = 1; sum_sum += 1;}else{result = 0;}
} else {
if(sum >= threshold){sum_sum += 1;}
result = sum;
}
setResult(column,n,result);
product_sum += product;
}
updateResults(); return sum_sum;
}
example = 0;
if(example){
start = getTime();
array_labels = newArray("Phase","Area","Circ.");
array_conditions = newArray("==",">=",">");
array_values = newArray(1,50,0.72);
prod_true = newArray(1,1,1);
prod_false = newArray(0,0,0);
n_start = 0;
n_end = nResults();
x = 3;
threshold = 3;
binary = 0;
columnA = "CT";
columnB = "CW";
a = conditionsTrue(array_labels,array_conditions,array_values,n_start,n_end,columnA,x);
print("A: " + a);
b = conditionsWeighted(array_labels,array_conditions,array_values,prod_true,prod_false,binary,threshold,n_start,n_end,columnB);
print("B: " + b);
end = getTime();
print("Time: " + end-start);
} //}}}
// 017 Colour Particles //{{{
// Two functions for colouring particles according to a SINGLE condition.
// latter function includes PHASE SELECTION
// e.g.
//cP = colourParticles("Phase","==",1,100,0,nResults);
//cP = colourParticlesPhase(1,"Area","<",100,100,0,nResults);
function colourParticles(column,condition,value,index,n_start,n_end){
count = 0; setForegroundIndex(index);
for(n=n_start; n<n_end; n++){
col_value = getResult(column,n);
macro_expression = "result="+col_value+condition+value+"; return toString(result);";
if(eval(macro_expression)){
x = getResult("XStart",n);
y = getResult("YStart",n);
floodFill(x,y,"8-connected");
count++;
}
}
return count;
}
function colourParticlesPhase(phase,column,condition,value,index,n_start,n_end){
count = 0; setForegroundIndex(index);
for(n=n_start; n<n_end; n++){
if(getResult("Phase",n)==phase){
col_value = getResult(column,n);
macro_expression = "result="+col_value+condition+value+"; return toString(result);";
if(eval(macro_expression)){
x = getResult("XStart",n);
y = getResult("YStart",n);
floodFill(x,y,"8-connected");
count++;
}
}
}
return count;
}
/* INCOMPLETE
function colourByParameter(column,value,index,n_start,n_end){
count = 0; setForegroundIndex(index);
for(n=n_start; n<n_end; n++){
col_value = getResult(column,n);
macro_expression = "result="+col_value+condition+value+"; return toString(result);";
if(eval(macro_expression)){
x = getResult("XStart",n);
y = getResult("YStart",n);
floodFill(x,y,"8-connected");
count++;
}
}
return count;
}
*/
//}}}
// 018 XY CODER (Encoder-Decoder) //{{{
/** type = "enc" will encode x & y to return value (just leave value = 0)
type = "dec" will decode value to return x & y (just leave x & y = 0) **/
function xy_coder(x,y,value,type){
scale = 10000;
if(type=="enc"){
enc = scale*x+y;
return enc;
}
else if(type=="dec"){
x = floor(value/scale);
y = value-x*scale;
dec = newArray(x,y);
return dec;
}
}
//}}}
// 019 Centre Pixel //{{{
/** Takes an array of pixels & returns centre-most pixel
enc = 1 will encode; enc = 0 will give array **/
function centrePixel(xpoints,ypoints,enc) {
xsum = 0; ysum = 0;
for(i=0; i<xpoints.length; i++){
xsum += xpoints[i];
ysum += ypoints[i];
}
xavg = ysum / xpoints.length; yavg = ysum / ypoints.length;
xo = xpoints[0]; yo = ypoints[0];
min_dist = sqrt(pow(xavg-xo,2)+pow(yavg-yo,2));
for(i=1; i<xpoints.length; i++){
dist = sqrt(pow(xavg-xpoints[i],2)+pow(yavg-ypoints[i],2));
if(dist<min_dist){
xo = xpoints[i];
yo = ypoints[i];
min_dist = dist;
}
}
if(enc == 1){ xy = xy_coder(xo,yo,0,"enc"); return xy;}
else{ xy = newArray(xo,yo); return xy; }
}
//}}}
// 020 IsPointInPath //{{{
/** Adapted from Python code for the "EVEN-ODD RULE"
source: http://en.wikipedia.org/wiki/Even-odd_rule **/
function isPointInPath(x, y, xpoly, ypoly){
num = xpoly.length;
i = 0;
j = num - 1;
c = false;
for(i=0; i<num; i++){
if( ((ypoly[i] > y) != (ypoly[j] > y)) & (x < (xpoly[j] - xpoly[i]) * (y - ypoly[i]) / (ypoly[j] - ypoly[i]) + xpoly[i]) ){
if(c){ c = false; } else { c = true; } // c = not c
}
j = i;
}
return c;
}
// Example
// Make selection of an object prior to running this
// Object should fill with set value.
/**
getSelectionCoordinates(xpoints,ypoints);
getSelectionBounds(xo, yo, wS, hS); // wS & hS are width & height of selection
for(y = yo; y < yo+hS; y++){
for(x = xo; x < xo+wS; x++){
a = isPointInPath(x,y,xpoints,ypoints);
if(a){setPixel(x,y,150);}
}
}
**/
/** Original PYTHON CODE:
def isPointInPath(x, y, poly):
num = len(poly)
i = 0
j = num - 1
c = False
for i in range(num):
if ((poly[i][1] > y) != (poly[j][1] > y)) and (x < (poly[j][0] - poly[i][0]) * (y - poly[i][1]) / (poly[j][1] - poly[i][1]) + poly[i][0]):
c = not c
j = i
return c
**/
//}}}
// 021 Get Selection Pixels //{{{
// getSelectionPixels
/** xORy = 0 gives xpoints
xORy = 1 gives ypoints
xORy = 2 gives encoded points (xy_coder)
xORy = 3 gives centre pixel, encoded
xORy = 4 gives centre pixel, array **/
function getSelectionPixels(xORy){
getSelectionCoordinates(xpoly,ypoly);
getSelectionBounds(xo, yo, wS, hS);
i = 0;
for(y = yo; y < yo+hS; y++){
for(x = xo; x < xo+wS; x++){
if(isPointInPath(x,y,xpoly,ypoly)){i++;}
}
}
xpoints = newArray(i); ypoints = newArray(i); i = 0;
for(y = yo; y < yo+hS; y++){
for(x = xo; x < xo+wS; x++){
if(isPointInPath(x,y,xpoly,ypoly)){xpoints[i] = x; ypoints[i] = y; i++;}
}
}
if(xORy==0){ return xpoints; }
if(xORy==1){ return ypoints; }
if(xORy==2){
xy_points = newArray(xpoints.length);
for(i=0; i<xpoints.length; i++){
xy_points[i] = xy_coder(xpoints[i],ypoints[i],0,"enc");
}
return xy_points;
}
if(xORy==3){
xy = centrePixel(xpoints,ypoints,1);
return xy;
}
if(xORy==4){
xy = centrePixel(xpoints,ypoints,0);
return xy;
}
}
//}}}
// 022 Defect Encoder / Decoder //{{{
/** type = "enc" : encode. Input = phase, connectivity, x, y.
type = "dec" : decode. Input = values. **/
function defect_coder(values, phase, connectivity, x, y,type){
// encode values: 1PCCXXXXXYYYYY
// Phase (1,0); Connectivity (0...9); X-coordinates; Y-coordinates
expts = newArray(12,10,5,0);
if(type=="enc"){
values = newArray(phase, connectivity, x, y);
enc = 1 * pow(10,expts[0]+1);
for(i=0; i<4; i++){ enc += values[i] * pow(10,expts[i]); }
return enc;
}
else if(type=="dec"){
dec = newArray(4);
v = values;
vs = 1 * pow(10,expts[0]+1);
for(i=0; i<4; i++){
v = v - vs;
dec[i] = floor(v / pow(10,expts[i]));
vs = dec[i] * pow(10,expts[i]);
}
return dec;
}
} //}}}
// 023 Integer String //{{{
/** converts an integer to a string for printing **/
function integerString(n){
m = floor(log(n)/log(10));
str = "";
rem = n;
for(i=m; i>=0; i--){
ni = floor(rem/pow(10,i));
rem = rem - ni * pow(10,i);
str += toString(ni);
}
return str;
}
//}}}
// 024 Shift Values //{{{
/** how == 0 use xpts & ypts pixel arrays
how = 1 obtain xpts & ypts from selection
shift = how much the values will be shifted by
e.g. 100: values will be shifted by +100
-50: values will be shifted by -50 **/
function shiftValues(xpoints,ypoints,shift,how){
if(how==1){
getSelectionCoordinates(xpoly,ypoly);
getSelectionBounds(xo, yo, wS, hS);
i = 0;
for(y = yo; y < yo+hS; y++){
for(x = xo; x < xo+wS; x++){
if(isPointInPath(x,y,xpoly,ypoly)){i++;}
}
}
xpoints = newArray(i); ypoints = newArray(i); i = 0;
for(y = yo; y < yo+hS; y++){
for(x = xo; x < xo+wS; x++){
if(isPointInPath(x,y,xpoly,ypoly)){xpoints[i] = x; ypoints[i] = y; i++;}
}
}
}
for(i=0; i<xpoints.length; i++){
x = xpoints[i]; y = ypoints[i];
value = getPixel(x,y);
setPixel(x,y,value+shift);
}
return xpoints.length;
} //}}}
// 025 Distance from Edge //{{{
function edgeDistance(w,h,x,y){
dist = minOf(minOf(x,w-x-1),minOf(y,h-y-1));
return dist;
}//}}}