-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNd2toTiff_1Color.ijm
191 lines (156 loc) · 5.3 KB
/
Nd2toTiff_1Color.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
// --- Main procedures begin ---
start_frame = 1;
substract_end_frames = 0;
ext = "nd2";
inDir = getDirectory("--> INPUT: Choose Directory Containing " + ext + "Files <--");
outDir=inDir;
//outDir = getDirectory("--> OUTPUT: Choose Directory for TIFF Output <--");
inList = getFileList(inDir);
list = inList;
//list = getFromFileList(ext, inList);
//list = getFromFileList("405", inList);
// Checkpoint: get file list of *.nd2 files
print("Below is a list of files to be converted:");
printArray(list); // Implemented below
setBatchMode(true);
for (i=0; i<list.length; i++)
{
inFullname = inDir + list[i];
//outFullname = outDir + list[i] + ".tif";
print("Converting", i+1, "of", list.length, list[i]); // Checkpoint: Indicating progress
//convertBioFormatToTif(inFullname, outFullname); // Implemented below
splitBioFormatToTif(inFullname, outDir);
print("...done."); //Checkpoint: Done one.
}
print("--- All Done ---");
// --- Main procedures end ---
function convertBioFormatToTif(inFullname, outFullname)
{
run("Bio-Formats Importer", "open='" + inFullname + "' autoscale color_mode=Default view=[Standard ImageJ] stack_order=Default virtual");
saveAs("Tiff", outFullname);
close();
}
function convertBioFormatTo8BitTif(inFullname, outFullname)
{
run("Bio-Formats Importer", "open='" + inFullname + "' autoscale color_mode=Default view=[Standard ImageJ] stack_order=Default virtual");
run("8-bit");
saveAs("Tiff", outFullname);
close();
}
function splitBioFormatToTif(inFullname, outDir)
{
run("Bio-Formats Macro Extensions");
Ext.setId(inFullname);
Ext.getSizeT(numT);
Ext.getSizeC(numC);
Ext.getSizeZ(numZ);
if (numZ > numT) {
num_frames = numZ;
}
else {
num_frames = numT;
}
for (c = 1; c <= numC; c++)
{
coptions = newArray(c, c, 1);
toptions = newArray(1, numT, 1);
zoptions = newArray(1, numZ, 1);
outName = "" + slugify(barename(inFullname)) + "_C_" + toString(c);
if (numC <= 1)
{
outName = "" + slugify(barename(inFullname));
}
outFullname = "" + trimDirTail(outDir) + "\\" + outName + ".tif";
id = bfImport(inFullname, coptions, zoptions, toptions);
//I added this in order to make a substack to remove the bleaching frames **P.Aurelio
if (c == 1)
{
run("Make Substack...", " slices="+toString(start_frame)+"-"+toString(num_frames-substract_end_frames));
}
if (c == 2)
{
run("Make Substack...", " slices="+toString(start_frame)+"-"+toString(num_frames-substract_end_frames));
}
//selectImage(id);
saveAs("Tiff", outFullname);
close();
}
}
function bfImport(path,channels,zs,times)
{
// Import image from "path", in specified ranges. Return the image ID.
// From dvSplitTimePoints.txt
// Originally written by Sebastien Huart. Modified by Bangyu Zhou.
// Example:
// coptions=newArray(2,2,1); // the second channel, (start from 0)
// toptions=newArray(5,5,1); // the 5th time slice, (start from 1)
// zoptions=newArray(1,20,1); // from 1st to 20th z plane, every plane (step is 1)
// srcId=bfImport(path,coptions,zoptions,toptions);
run("Bio-Formats Macro Extensions");
Ext.setId(path);
// bfDimOrders=newArray("XYZCT","XYZTC","XYCZT","XYCTZ","XYTZC","XYTCZ");
dimOrder = "";
Ext.getDimensionOrder(dimOrder);
Ext.getSizeT(numT);
Ext.getSizeZ(numZ);
Ext.getSizeC(numC);
// print("Image " + path + " has: ");
// print(numC + " channel(s), " +
// numZ + " z plane(s), " +
// numT + " time point(s)." );
options = "open=[" + path + "] view=[Standard ImageJ] stack_order=" + dimOrder + " virtual specify_range ";
cOpts = "c_begin=" + channels[0] + " c_end=" + channels[1] + " c_step=" + channels[2];
zOpts = "z_begin=" + zs[0] + " z_end=" + zs[1] + " z_step=" + zs[2];
tOpts = "t_begin=" + times[0] + " t_end=" + times[1] + " t_step=" + times[2];
options = options + cOpts + " " + zOpts + " " + tOpts;
run("Bio-Formats Importer", options);
id = getImageID();
return id;
}
function getFromFileList(ext, fileList)
{
// Select from fileList array the filenames with specified extension
// and return a new array containing only the selected ones.
// Depends on:
// getExtension(filename)
// By ZBY
// Last update at 2010 Aug 25
selectedFileList = newArray(fileList.length);
ext = toLowerCase(ext);
j = 0;
for (i=0; i<fileList.length; i++)
{
extHere = toLowerCase(getExtension(fileList[i]));
if (extHere == ext)
{
selectedFileList[j] = fileList[i];
j++;
}
}
selectedFileList = Array.trim(selectedFileList, j);
return selectedFileList;
}
function printArray(array)
{
// Print array elements recursively
for (i=0; i<array.length; i++)
print(array[i]);
}
function getExtension(filename)
{
ext = substring( filename, lastIndexOf(filename, ".") + 1 );
return ext;
}
function barename(filename)
{// Strip directory path and extension(from the first period) in filename
fn = File.getName(filename);
return substring(fn, 0, indexOf(fn, "."));
}
function slugify(string)
{// Replace none-word character into underscore
return replace(string, "\\W+", "_");
}
function trimDirTail(dir)
{// Trim any tailing backslash
return replace(dir, "\\\\+$", "");
}