-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeica_LAS_Apply.Scaling.ijm
151 lines (138 loc) · 4.08 KB
/
Leica_LAS_Apply.Scaling.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
/*
* Script written by Brenton Cavanagh 2019 [email protected]
* Purpose: To apply scaling to tiff files captured in leica LAS
* from ANX, XML & EAX or TXT calibration files.
* + the option to also output flattened image in TIF or PNG
*/
//Script updated by Brenton Cavanagh 20221021
#@File (label="Input Directory",style="directory") dirIN
#@File (label="Save location",style="directory") dirOUT
//setup
setBatchMode(true);
list = getFileList(dirIN);
count = 0;
skipped = newArray();
reason = newArray();
//Start of process
for (i=0; i<list.length; i++) {
filename = dirIN+File.separator+list[i];
file = list[i];
if (endsWith(filename, ".tif")){
open(filename);
//run("Bio-Formats Importer", "open=["+filename+"] color_mode=Default view=Hyperstack");
shortname = File.nameWithoutExtension;
savename = dirOUT+File.separator+shortname;
fileType = newArray(".anx",".eax",".cal.xml",".txt");
if (File.exists(dirIN+File.separator+".Metadata"+File.separator) == true){
caldir = dirIN+File.separator+".Metadata"+File.separator;
}
else{
caldir = dirIN+File.separator;
}
//check for calibration file
for (e = 0; e < fileType.length; e++) {
calname = caldir+file+fileType[e];
ext = fileType[e];
if (File.exists(calname) == true){
print("Found "+ file + ext);
//extract scaling info
s = File.openAsString(calname);
lines = split(s, "\n");
for (j=0; j<lines.length; j++) {
line = lines[j];
if (ext == ".anx" || ext == ".eax"){
if (indexOf(line,"<MetresPerPixel>")!=-1) {
idx1 = indexOf(line, "<MetresPerPixel>");
idx2 = indexOf(line, "</MetresPerPixel>");
value = substring(line, idx1+16, idx2);
if (lengthOf(value) > 1){
convert(); //convert from exp meters/pixel to micron/pixe;
}
else{
skipped = Array.concat(skipped, file);
reason = Array.concat(reason, "Invalid Scale");
print("Invalid Scale");
print("");
close();
}
e=10;
}
}
else if(ext == ".cal.xml"){
if (indexOf(line,"<XMetresPerPixel>")!=-1) {
idx1 = indexOf(line, "<XMetresPerPixel>");
idx2 = indexOf(line, "</XMetresPerPixel>");
value = substring(line, idx1+17, idx2);
if (lengthOf(value) > 1){
convert(); //convert from exp meters/pixel to micron/pixel
}
else{
skipped = Array.concat(skipped, file);
reason = Array.concat(reason, "Invalid Scale");
print("Invalid Scale");
print("");
close();
}
e=10;
}
}
}
}
}
//no calibration file found
if (e == 3){
skipped = Array.concat(skipped, file);
reason = Array.concat(reason, "Calibration missing");
print("Resaving "+ file);
print("Calibration missing");
print("");
close();
}
}
}
saveerrors();
//Notify user that script is finished
print("Finished resaving "+count+" Images");
function convert() {
idx3 = indexOf(value, "E");
number = substring(value, 0, idx3);
power = substring(value, idx3+2, idx3+4);
expon = pow(10, (power));
sign = substring(value, idx3+1, idx3+2);
if (sign == "-"){
scale = ((1/expon)*number)*1000000;
print("Applying scale: "+scale);
print("");
//Apply scaling to image and save
apply();
}
else{
scale = expon*number;
print("WARNING! "+scale+" um/pixel, are you sure the scale is correct?");
}
}
function apply() {
//Apply scaling
selectWindow(file);
run("Set Scale...", "distance=1 known="+scale+" pixel=1 unit=micron");
//optinal flatten
run("Flatten");
saveAs("tiff", savename);
close();
count++;
}
function saveerrors(){
//Print skipped files
if (skipped.length != 0){
print("The following "+skipped.length+" files were skipped, please see errorlog.txt");
Array.print(skipped);
print("");
text = File.open(dir2+"errorlog.txt");
for (i=0; i<skipped.length; i++){
print(text, skipped[i]+"\t"+reason[i]);
}
}
else{
print("No errors found");
}
}