-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSplit_Channels_Batch.ijm
executable file
·172 lines (132 loc) · 4.58 KB
/
Split_Channels_Batch.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
// Split_Channels macro by Christophe Leterrier
macro "Split_Channels_Batch" {
//*************** Initialization ***************
// Default values for the Options Panel
ENH_DEF = false;
ROLLING_DIAM = 50;
UNSHARP_MASK = 0.35;
SAT = 0.01;
SAVE_DEF="In a folder next to the source folder";
// Get the folder name
INPUT_DIR=getDirectory("Select the input stacks directory");
print("\n\n\n*** Split Channels Log ***");
print("");
print("INPUT_DIR :"+INPUT_DIR);
// Initialize choices variables
SAVE_ARRAY = newArray("In the source folder", "In a subfolder of the source folder", "In a folder next to the source folder", "In a subfolder with custom location");
//*************** Dialog ***************
// Creation of the dialog box
Dialog.create("Split Channels Options");
Dialog.addCheckbox("Enhance", ENH_DEF);
Dialog.addChoice("Save Images", SAVE_ARRAY, SAVE_DEF);
Dialog.show();
// Feeding variables from dialog choices
ENH = Dialog.getCheckbox();
SAVE_TYPE = Dialog.getChoice();
// Get all file names
ALL_NAMES=getFileList(INPUT_DIR);
Array.sort(ALL_NAMES);
ALL_EXT=newArray(ALL_NAMES.length);
// Create extensions array
for (i = 0; i < ALL_NAMES.length; i++) {
// print(ALL_NAMES[i]);
ALL_NAMES_PARTS = getFileExtension(ALL_NAMES[i]);
ALL_EXT[i] = ALL_NAMES_PARTS[1];
}
//*************** Prepare processing ***************
setBatchMode(true);
// Create the output folder
OUTPUT_DIR="Void";
if (SAVE_TYPE == "In the source folder") {
OUTPUT_DIR = INPUT_DIR;
}
if (SAVE_TYPE == "In a subfolder of the source folder") {
OUTPUT_DIR = INPUT_DIR + "split" + File.separator;
if (File.isDirectory(OUTPUT_DIR) == false) {
File.makeDirectory(OUTPUT_DIR);
}
}
if (SAVE_TYPE == "In a folder next to the source folder") {
OUTPUT_DIR = File.getParent(INPUT_DIR);
OUTPUT_NAME = File.getName(INPUT_DIR);
OUTPUT_SHORTA = split(OUTPUT_NAME, " ");
OUTPUT_SHORT = OUTPUT_SHORTA[0];
OUTPUT_DIR = OUTPUT_DIR + File.separator + OUTPUT_SHORT + " split" + File.separator;
if (File.isDirectory(OUTPUT_DIR) == false) {
File.makeDirectory(OUTPUT_DIR);
}
}
if (SAVE_TYPE == "In a folder with custom location") {
OUTPUT_DIR = getDirectory("Choose the save folder");
INPUT_NAME = File.getName(INPUT_DIR);
OUTPUT_DIR = OUTPUT_DIR + File.separator + INPUT_NAME + " split" + File.separator;
if (File.isDirectory(OUTPUT_DIR) == false) {
File.makeDirectory(OUTPUT_DIR);
}
}
OUTPUT_PARENT_DIR=File.getParent(OUTPUT_DIR);
print("OUTPUT_DIR: " + OUTPUT_DIR);
print("OUTPUT_PARENT_DIR: " + OUTPUT_PARENT_DIR);
//*************** Processing ***************
// Loop on all .tif extensions
for (n=0; n<ALL_EXT.length; n++) {
if (ALL_EXT[n]==".tif") {
// Get the file path
FILE_PATH=INPUT_DIR+ALL_NAMES[n];
// Store components of the file name
FILE_NAME=File.getName(FILE_PATH);
FILE_DIR = File.getParent(FILE_PATH);
FILE_SEP = getFileExtension(FILE_NAME);
FILE_SHORTNAME = FILE_SEP[0];
FILE_EXT = FILE_SEP[1];
print("");
print("INPUT_PATH:", FILE_PATH);
// print("FILE_NAME:", FILE_NAME);
// print("FILE_DIR:", FILE_DIR);
// print("FILE_EXT:", FILE_EXT);
// print("FILE_SHORTNAME:", FILE_SHORTNAME);
open(FILE_PATH);
STACK_ID = getImageID();
getDimensions(w, h, ch, sl, fr);
run("Split Channels");
for(j = 0; j < ch; j++) {
// Construct window name (from the names created by the "Split Channels" command)
TEMP_CHANNEL = d2s(j+1,0);
SOURCE_WINDOW_NAME = "C" + TEMP_CHANNEL + "-" + FILE_NAME;
//Select source image
selectWindow(SOURCE_WINDOW_NAME);
// Optional enhancement
if (ENH == true) {
run("Subtract Background...", "rolling=" + ROLLING_DIAM + " stack");
// run("Unsharp Mask...", "radius=1 mask=" + UNSHARP_MASK + " stack");
run("Enhance Contrast...", "saturated=" + SAT);
}
else {
resetMinAndMax;
}
// Create output file path and save the output image
OUTPUT_PATH = OUTPUT_DIR + FILE_SHORTNAME + "-C=" + j + ".tif";
save(OUTPUT_PATH);
print("OUTPUT_PATH: " + OUTPUT_PATH);
close();
}
}// end of IF loop on tif extensions
}// end of FOR loop on all files
setBatchMode("exit and display");
print("");
print("*** Split Channels end ***");
showStatus("Split Channels finished");
}
//*************** Functions ***************
function getFileExtension(Name) {
nameparts = split(Name, ".");
shortname = nameparts[0];
if (nameparts.length > 2) {
for (k = 1; k < nameparts.length - 1; k++) {
shortname += "." + nameparts[k];
}
}
extname = "." + nameparts[nameparts.length - 1];
namearray = newArray(shortname, extname);
return namearray;
}