Skip to content

Commit 4d76408

Browse files
committed
FIJI macro for preprocessing and segmentation of wood anatomy
1 parent 92f25b6 commit 4d76408

1 file changed

Lines changed: 69 additions & 81 deletions

File tree

notebooks/FIJI_preprocessing.ijm renamed to notebooks/Wood_Anatomy-Image_Preprocessing_and_Segmentation.ijm

Lines changed: 69 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function processFile(dir, file) {
8585
// Save good wood mask-dark
8686
selectImage("good_wood_mask");
8787
run("Duplicate...", "title=good_wood_mask_copy");
88-
saveAs("Tiff", fulloutdir + File.separator + filename + File.separator + "nodark_wood_mask.tif");
88+
saveAs("Tiff", fulloutdir + File.separator + filename + File.separator + "healthy_wood_mask.tif");
8989
close();
9090
close("good_wood_mask_copy");
9191

@@ -161,11 +161,11 @@ function processFile(dir, file) {
161161
run("Auto Local Threshold", "method=Sauvola radius=15 parameter_1=0 parameter_2=0 white");
162162
run("Invert");
163163

164-
selectImage("wood_mask");
164+
selectImage("good_wood_mask"); // or [good_wood_mask_copy] or commented out this to have whole segmentation
165165
run("Duplicate...", "title=wood_mask_copy");
166166
run("8-bit");
167167

168-
imageCalculator("AND create", "preprocessed_image_copy", "wood_mask_copy"); // or [good_wood_mask_copy] or commented out this to have whole segmentation
168+
imageCalculator("AND create", "preprocessed_image_copy", "wood_mask_copy");
169169
rename("masked_preprocessed_image"); // This is still needed here because otherwise I loose in FIll holes step part of the segmentation so at least the largest component needs to stay
170170

171171
run("Fill Holes");
@@ -178,42 +178,29 @@ function processFile(dir, file) {
178178
rename("thresholded_image"); // [thresholded_image]
179179
close("preprocessed_image_copy");
180180
close("wood_mask_copy");
181-
182181

183182

184-
selectImage("thresholded_image");
185-
run("Duplicate...", "title=local_threshold_image_no_small_regions");
186-
187-
selectImage("thresholded_image");
188-
run("Duplicate...", "title=long_cell_segmentation");
189-
190-
selectImage("thresholded_image");
191-
run("Duplicate...", "title=local_threshold_image_to_overlay");
192-
193-
selectImage("thresholded_image");
194-
run("Duplicate...", "title=local_threshold_image_to_save");
195-
// waitForUser("Postprocess the lumen segmentation - remove small objects 2");
196-
197183
// Save Lumen segmentation
198-
selectImage("local_threshold_image_to_save");
184+
selectImage("thresholded_image");
185+
run("Duplicate...", "title=thresholded_image_copy");
199186
run("8-bit");
200187
run("Invert");
201188
saveAs("Tiff", fulloutdir + File.separator + filename + File.separator + "segmentation.tif");
202-
//waitForUser("Postprocess the lumen segmentation - remove small objects 2");
189+
close();
190+
close("thresholded_image_copy");
191+
203192

204193
//Long cell segmentation
205-
selectImage("long_cell_segmentation");
194+
selectImage("thresholded_image");
195+
run("Duplicate...", "title=long_cell_segmentation");
206196
run("Invert");
207-
208-
// 1) Measure all particles including AR
209-
run("Set Measurements...", "area fit shape redirect=None decimal=3");
197+
run("Set Measurements...", "area fit shape redirect=None decimal=3"); // Measure all particles including AR
210198
run("Analyze Particles...", "size=5-Infinity circularity=0.00-0.5 show=Nothing display clear add");
211199

212-
// 2) Filter ROIs by AR threshold and build a mask
213-
minAR = 2.0; // adjust threshold for "elongated"
200+
minAR = 2.0; // adjust threshold for "elongated" // Filter ROIs by AR threshold and build a mask
214201
n = nResults;
215202
run("Select None");
216-
newImage("elongated", "8-bit black", getWidth(), getHeight(), 1);
203+
newImage("long_cells", "8-bit black", getWidth(), getHeight(), 1);
217204

218205
for (i = 0; i < n; i++) {
219206
ar = getResult("AR", i);
@@ -222,33 +209,31 @@ function processFile(dir, file) {
222209
run("Fill");
223210
}
224211
}
225-
selectWindow("elongated");
226-
227-
// waitForUser("Postprocess the lumen segmentation - remove small objects 2");
228-
//run("Analyze Particles...", "size=100-Infinity circularity=0.00-0.30 show=Masks");
229-
//run("Invert LUT");
230-
//run("Invert");
212+
selectWindow("long_cells");
231213
run("8-bit");
232-
rename("long_cells");
233214

234215
selectImage("good_wood_mask");
235216
run("Duplicate...", "title=good_wood_mask_copy");
217+
236218
imageCalculator("AND create", "long_cells", "good_wood_mask_copy"); // commented out this to have whole segmentation
237219
saveAs("Tiff", fulloutdir + File.separator + filename + File.separator + "long_cells2.tif");
238220
close("good_wood_mask_copy");
221+
close("long_cell_segmentation");
239222

240223
//Save lumen segmentation on original image
241-
selectImage("local_threshold_image_to_overlay");
224+
selectImage("thresholded_image");
225+
run("Duplicate...", "title=thresholded_image_copy");
242226
run("Analyze Particles...", "display clear overlay add composite record");
243227

244228
selectImage("preprocessed_image");
245229
run("Duplicate...", "title=preprocessed_image_copy");
246-
selectImage("preprocessed_image_copy");
230+
247231
roiManager("Show All without labels");
248232
run("Flatten");
249233
saveAs("Tiff", fulloutdir + File.separator + filename + File.separator + "segmentation_on_input_transparent.tif");
250234
close();
251235
close("preprocessed_image_copy");
236+
close("thresholded_image_copy");
252237
//-------------------------------------------
253238

254239
//// Create and save late wood segmentation
@@ -281,93 +266,96 @@ green_flag = 1;
281266
if (green_flag==1) {
282267
//-------------------------------------------
283268
// --- Watershed - excluding small regions - seeds only on large cells
269+
// selectImage("thresholded_image");
270+
// run("Duplicate...", "title=thresholded_image_copy");
271+
// run("Invert");
272+
// run("8-bit");
273+
// run("Analyze Particles...", "size=350-infinity pixel circularity=0.00-1.00 show=Masks"); //0-2000, 0.5-1.0
274+
// run("Invert LUT");
275+
// run("Invert");
276+
//
277+
// run("Distance Map");
278+
// run("Enhance Contrast", "saturated=0.35");
279+
// saveAs("Tiff", fulloutdir + File.separator + filename + File.separator + "DT_on_removed_small_regions.tif");
280+
// rename("distance_map"); // [distance_map]
281+
// close("thresholded_image_copy");
282+
283+
// --- Watershed - normal - seeds on cell regions
284284
selectImage("thresholded_image");
285285
run("Duplicate...", "title=thresholded_image_copy");
286286
run("Invert");
287287
run("8-bit");
288-
run("Analyze Particles...", "size=350-infinity pixel circularity=0.00-1.00 show=Masks"); //0-2000, 0.5-1.0
288+
run("Analyze Particles...", "size=0-2000 pixel circularity=0.5-1.00 show=Masks"); //0-2000, 0.5-1.0
289289
run("Invert LUT");
290290
run("Invert");
291291

292292
run("Distance Map");
293293
run("Enhance Contrast", "saturated=0.35");
294-
saveAs("Tiff", fulloutdir + File.separator + filename + File.separator + "DT_on_removed_small_regions.tif");
294+
saveAs("Tiff", fulloutdir + File.separator + filename + File.separator + "DT_on_cell_regions.tif");
295295
rename("distance_map");
296296
close("thresholded_image_copy");
297297

298-
// --- Watershed - normal - seeds on cell regions
299-
// selectImage("thresholded_image");
300-
// run("Duplicate...", "title=thresholded_image_copy");
301-
// run("Invert");
302-
// run("8-bit");
303-
// run("Analyze Particles...", "size=0-2000 pixel circularity=0.5-1.00 show=Masks"); //0-2000, 0.5-1.0
304-
// run("Invert LUT");
305-
// run("Invert");
306-
//
307-
// run("Distance Map");
308-
// run("Enhance Contrast", "saturated=0.35");
309-
// saveAs("Tiff", fulloutdir + File.separator + filename + File.separator + "DT_on_cell_regions.tif");
310-
// rename("distance_map");
311-
// close("thresholded_image_copy");
312-
313298

314299
run("Classic Watershed", "input=[distance_map] mask=None use min=0 max=255");
315-
rename("watershed_image");
316-
300+
rename("watershed_image"); // [watershed_image]
301+
saveAs("Tiff", fulloutdir + File.separator + filename + File.separator + "watershed_image.tif");
302+
rename("watershed_image");
317303

318304
// Save watershed as a binary image
319305
selectImage("watershed_image");
320-
run("Duplicate...", "title=watershed_image_to_save");
306+
run("Duplicate...", "title=watershed_image_copy");
321307
run("8-bit");
322308
setAutoThreshold("Default dark no-reset");
323309
setThreshold(1, 255);
324310
run("Threshold...");
325311
run("Convert to Mask");
326-
run("Duplicate...", "title=watershed_image_to_save1");
312+
run("Duplicate...", "title=cell_segmentation"); // [cell_segmentation]
313+
314+
selectImage("watershed_image_copy");
327315
saveAs("Tiff", fulloutdir + File.separator + filename + File.separator + "cell_segmentation.tif");
316+
rename("watershed_image_copy");
317+
// close("watershed_image_copy");
328318

329319
// Save watershed as a binary image - remove large cells and elongated cells
330-
selectImage("watershed_image_to_save");
320+
selectImage("watershed_image_copy");
331321
run("Analyze Particles...", "size=0-5000 pixel circularity=0.00-1.00 show=Masks display clear overlay add composite record"); //0-2000, 0.5-1.0
332322
run("Invert LUT");
323+
rename("watershed_image_cleaned");
333324

334-
run("Duplicate...", "title=watershed_image_to_overlay");
335-
selectImage("Mask of watershed_image_to_save");
325+
run("Duplicate...", "title=watershed_image_cleaned_copy");
326+
selectImage("watershed_image_cleaned_copy");
336327
saveAs("Tiff", fulloutdir + File.separator + filename + File.separator + "good_cell_mask.tif");
337-
// waitForUser("Clean the segmentation and continue processing2");
338-
328+
close();
329+
close("watershed_image_cleaned_copy");
339330

340331

341332
//-------------------------------------------
342333
// Create wall segmentation by overlaying lumen and cell segmentation
343-
344-
345-
// selectImage("preprocessed_image");
346-
// run("Duplicate...", "title=preprocessed_image_copy");
347-
// selectImage("preprocessed_image_copy");
348334
selectImage("thresholded_image");
349335
run("Duplicate...", "title=thresholded_image_copy");
336+
selectImage("watershed_image");
337+
run("Duplicate...", "title=watershed_image_copy");
350338

351-
imageCalculator("AND create 32-bit", "thresholded_image_copy", "watershed_image");
339+
imageCalculator("AND create 32-bit", "thresholded_image_copy", "watershed_image_copy");
352340
run("6 shades");
353341
run("8-bit");
354-
rename("wall_segmentation");
342+
rename("image_combination");
355343
close("thresholded_image_copy");
344+
close("watershed_image_copy");
356345

357346
// Overlay wall segmentation on the original image
358-
selectImage("original_image");
359-
run("Duplicate...", "title=original_image_copy");
360-
361-
selectImage("wall_segmentation"); // * wall_segmentation
347+
selectImage("image_combination");
362348
run("8-bit");
363349
setAutoThreshold("Default dark no-reset");
364350
setThreshold(1, 255);
365351
run("Threshold...");
366352
run("Convert to Mask");
367-
368-
selectImage("wall_segmentation");
353+
run("Duplicate...", "title=wall_segmentation"); // [wall_segmentation]
369354
run("Duplicate...", "title=wall_segmentation_copy");
370-
selectImage("wall_segmentation_copy");
355+
356+
selectImage("original_image");
357+
run("Duplicate...", "title=original_image_copy");
358+
371359
imageCalculator("AND create", "wall_segmentation_copy", "original_image_copy");
372360
rename("wall_segmentation_on_original_image");
373361
run("8-bit");
@@ -380,27 +368,27 @@ if (green_flag==1) {
380368
// Saving the wall segmentation
381369
selectImage("wall_segmentation");
382370
run("Duplicate...", "title=wall_segmentation_copy");
383-
selectImage("wall_segmentation_copy");
384371
saveAs("Tiff", fulloutdir + File.separator + filename + File.separator + "wall_segmentation.tif");
385372
close();
386373
close("wall_segmentation_copy");
387374

388375

389-
390-
selectImage("watershed_image_to_overlay");
376+
// Saving the cell segmentation on input image
377+
selectImage("cell_segmentation");
378+
run("Duplicate...", "title=cell_segmentation_copy");
391379
run("Analyze Particles...", "display clear overlay add composite record");
392380

393381
selectImage("original_image");
394382
run("Duplicate...", "title=original_image_copy");
395-
selectImage("original_image_copy");
383+
396384
roiManager("Show All without labels");
397385
run("Flatten");
398386
saveAs("Tiff", fulloutdir + File.separator + filename + File.separator + "cell_on_input_transparent.tif");
399387
close();
400-
close("watershed_image_to_overlay");
388+
close("cell_segmentation_copy");
401389
close("original_image_copy");
402390

403-
// Saving the wall segmentation on
391+
// Saving the wall segmentation on input image
404392
selectImage("wall_segmentation");
405393
run("Duplicate...", "title=wall_segmentation_copy");
406394
selectImage("wall_segmentation_copy");

0 commit comments

Comments
 (0)