-
Notifications
You must be signed in to change notification settings - Fork 24
/
imagej_fiji_macros.ijm
123 lines (102 loc) · 4.7 KB
/
imagej_fiji_macros.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
// ------------------------------------------------------------------------------------
// ImageJ/FIJI Binary Lesion Segmentation Proofreading Macros
//
// (c) Philipp Tschandl 2021
//
// You may re-use this code under the CC BY-NC 4.0 license.
// This code was used to revisit, check and correct automated lesion segmentations for:
// Tschandl, P. et al. Human–computer collaboration for skin cancer recognition.
// Nat Med 26, 1229–1234 (2020). https://doi.org/10.1038/s41591-020-0942-0
//
// Images should be placed as:
// "/PATH/TO/YOUR/DATASET/IMAGES/ImageName1.jpg" ...
// Automated segmentations should be placed as:
// "/PATH/TO/YOUR/DATASET/SEGMENTATIONS_AUTOMATED/ImageName1_segmentation.png" ...
// Corrected segmentations will be stored as:
// "/PATH/TO/YOUR/DATASET/SEGMENTATIONS_CORRECTED/ImageName1_segmentation.png" ...
//
// ------------------------------------------------------------------------------------
// If you don't want to select the folders after every restart you can hard-code them here
var lesionImagesPath = "/PATH/TO/YOUR/DATASET/IMAGES/";
var lesionAutoSegmentationsPath = "/PATH/TO/YOUR/DATASET/SEGMENTATIONS_AUTOMATED/";
var lesionCorrectedSegmentationsPath = "/PATH/TO/YOUR/DATASET/SEGMENTATIONS_CORRECTED/";
// Set the source and target folders
macro "setSegmentationPaths" {
showMessage("Set paths for [1] Images, [2] Automated Segmentations, [3] Corrected Segmentations.");
lesionImagesPath=getDirectory("Directory of raw images (*ID*.img)");
lesionAutoSegmentationsPath=getDirectory("Directory of automated segmentations (*ID*_segmentation.png)");
lesionCorrectedSegmentationsPath=getDirectory("Directory of corrected segmentations (*ID*_segmentation.png)");
showMessage("[1] Images: "+lesionImagesPath + " -- \n[2] Automated Segmentations: " + lesionAutoSegmentationsPath + " -- \n[3] Corrected Segmentations: " + lesionCorrectedSegmentationsPath);
}
// Loads the corresponding mask to the opened image and converts it into a selection
macro "loadBinaryMaskAnnotation... [l]" {
if ((lesionImagesPath == "") | (lesionAutoSegmentationsPath == "") | (lesionCorrectedSegmentationsPath == ""))
exit("You need to run 'setSegmentationsPaths' before using this command!");
imageid = getTitle;
lesionid = getTitle;
dot = indexOf(lesionid, ".");
if (dot >= 0) lesionid = substring(lesionid, 0, dot);
target = lesionCorrectedSegmentationsPath+lesionid+"_segmentation.png";
autoseg = lesionAutoSegmentationsPath+lesionid+"_segmentation.png";
if (File.exists(target))
open(target);
else
open(autoseg);
if (File.exists(target))
showStatus("MANUAL segmentation loaded");
else
showStatus("AUTOMATED segmentation loaded");
selectWindow(lesionid+"_segmentation.png");
run("Invert");
run("Create Selection");
selectWindow(imageid);
run("Restore Selection");
selectWindow(lesionid+"_segmentation.png");
close();
selectWindow(imageid);
// Optional, depending on the automated segmentations:
// run("Enlarge...", "enlarge=2");
}
// Stores the current selection as a binary mask
macro "Store Seg as Mask... [e]" {
if ((lesionImagesPath == "") | (lesionAutoSegmentationsPath == "") | (lesionCorrectedSegmentationsPath == ""))
exit("You need to run 'setSegmentationsPaths' before using this command!");
imageid=getTitle;
lesionid=getTitle;
dot = indexOf(lesionid, ".");
if (dot >= 0) lesionid = substring(lesionid, 0, dot);
run("Create Mask");
path = lesionCorrectedSegmentationsPath+lesionid+"_segmentation.png";
saveAs("PNG", path);
close();
selectWindow(imageid);
run("Open Next");
run("Select None");
run("loadBinaryMaskAnnotation... [l]");
}
// Checks images and target folder, returns how many are finished,
// ...and opens the first image without a corrected segmentation mask
macro "Get first unsegmented image... [u]" {
if ((lesionImagesPath == "") | (lesionAutoSegmentationsPath == "") | (lesionCorrectedSegmentationsPath == ""))
exit("You need to run 'setSegmentationsPaths' before using this command!");
showMessage("Getting files from:\n" + lesionImagesPath);
list = getFileList(lesionImagesPath);
for (i = 0; i < list.length; i++) {
imageid = list[i];
lesionid = list[i];
dot = indexOf(lesionid, ".");
if (dot >= 0) lesionid = substring(lesionid, 0, dot);
target = lesionCorrectedSegmentationsPath+lesionid+"_segmentation.png";
if (File.exists(target)) {
showStatus(lesionid+" already segmented.");
} else {
showMessage("Continue labeling at: "+lesionid);
open(lesionImagesPath + list[i]);
i = 99999999;
}
}
seglist = getFileList(lesionCorrectedSegmentationsPath);
showMessage("Already segmented "+seglist.length+" of "+list.length+" images");
setTool("freehand");
run("loadBinaryMaskAnnotation... [l]");
}