-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNikon_NIS.Elements_Resave.fluorescence.ijm
135 lines (122 loc) · 4.77 KB
/
Nikon_NIS.Elements_Resave.fluorescence.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
/*
* Script written by Brenton Cavanagh 2018 [email protected]
* Purpose: To open and resave ND2 files from the Nikon Eclipse 90i
* Define the correct channel colours and save as multipage TIF with
* the option to also output flattened image in PNG
*/
//Generate user input
#@File (label="Input Directory",style="directory") dirIN
#@File (label="Save location",style="directory") dirOUT
#@String(label="Channel one", description="Set colour for channel one", choices={"None","BrightField","Red","Green","Blue","Cyan","Magenta","Yellow","Grays"}, value="None") chanOne
#@String(label="Channel two", description="Set colour for channel two", choices={"None","BrightField","Red","Green","Blue","Cyan","Magenta","Yellow","Grays"}, value="None") chanTwo
#@String(label="Channel three", description="Set colour for channel three", choices={"None","BrightField","Red","Green","Blue","Cyan","Magenta","Yellow","Grays"}, value="None") chanThree
#@String(label="Channel four", description="Set colour for channel four", choices={"None","BrightField","Red","Green","Blue","Cyan","Magenta","Yellow","Grays"}, value="None") chanFour
//#@String(label="Channel five", description="Set colour for channel five", choices={"None","BrightField","Red","Green","Blue","Cyan","Magenta","Yellow","Grays"}, value="None") chanFive
//#@String(label="Channel six", description="Set colour for channel six", choices={"None","BrightField","Red","Green","Blue","Cyan","Magenta","Yellow","Grays"}, value="None") chanSix
//#@String(label="Preview PNG?", description="Save a PNG copy of the image", choices={"Yes","No",},value="No", style="radioButtonHorizontal") Flatten
setBatchMode(true);
count = 0;
//Remove channels that are "none"
//strings = newArray(chanOne, chanTwo, chanThree, chanFour, chanFive, chanSix);
strings = newArray(chanOne, chanTwo, chanThree, chanFour);
colours = newArray();
c = 0;
while (c<strings.length) {
if (strings[c] == "None") {
//print("found: "+strings[i]);
} else {
colours = Array.concat(colours, strings[c]);
}
c++;
}
//Recurse images in specified folders
list = getFileList(dirIN);
errors = newArray();
for (i=0; i<list.length; i++) {
filename = dirIN+File.separator+list[i];
savename = dirOUT+File.separator+list[i];
if (endsWith(filename, ".nd2")){
run("Bio-Formats Importer", "open=["+filename+"] autoscale color_mode=Default view=Hyperstack");
print("Resaving "+list[i]);
getDimensions(width, height, channels, slices, frames);
if (channels >= colours.length) {
// Set colours
for (j=0; j<colours.length; j++){
if (colours[j] !="BrightField" && channels == 1){
run(colours[j]);
}
else if (colours[j] !="BrightField"){
Stack.setChannel(j+1)
run(colours[j]);
Stack.setDisplayMode("composite");
}
//Create RGB Brightfield image and grayscale
if (colours[j] =="BrightField"){
run("Duplicate...", "duplicate channels=1-"+j);
rename("FL");
selectWindow(list[i]);
run("Duplicate...", "duplicate channels="+(j+1)+"-"+(j+3));
rename("BF");
Stack.setChannel(1);
run("Blue");
Stack.setChannel(2);
run("Green");
Stack.setChannel(3);
run("Red");
Stack.setDisplayMode("composite");
run("Flatten");
saveAs("tiff", savename+"_BF");
rename("zBF");
run("16-bit");
close(list[i]);
close("BF");
selectWindow("FL");
rename(list[i]);
//Merge open images into single image
selectWindow(list[i]);
getDimensions(width, height, channels, slices, frames);
if (channels > 1){
run("Split Channels");
selectWindow("C1-"+list[i]);
}
//generate and sort list of open images
openArray = newArray(nImages);
for (k=0; k<nImages; k++) {
selectImage(k+1);
openArray[k] = getTitle();
}
Array.sort(openArray);
str = "";
for (l=0; l<openArray.length-1; l++){
str = str +"c"+(l+1)+"=["+openArray[l]+"] ";
}
str = str +"c"+(l+1)+"=["+openArray[openArray.length-1]+"]";
run("Merge Channels...", ""+str+" create keep");
Stack.setDisplayMode("composite");
}
}
saveAs("tiff", savename);
//cleanup
run("Close All");
count++;
}
else{
error = "ERROR: "+ channels + " channels found, " + colours.length + " colours assigned -- skipping";
print(error);
errors = Array.concat(errors, list[i]);
run("Close All");
}
}
else{
print("--> Skipping "+ list[i] + ", file type incorrect.");
}
}
if (errors.length != 0){
print(" ");
print("The following "+errors.length+" files were skipped, please see reasons in the log above.");
Array.print(errors);
}
//Notify user that script is finished
print(" ");
print("Finished resaving "+count+" Images");
//Script updated by Brenton Cavanagh 20230216