-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassbalanceMacro_v1-3.ijm
67 lines (62 loc) · 1.94 KB
/
ClassbalanceMacro_v1-3.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
// For use with the Nocodeseg pipeline (Pettersen et al, 2022).
// This macro identifies tiles with > 5% of a particular class and
// duplicates them to resolve class imbalance; it first duplicates the tiles and then the
// corresponding TIF images
//part 1: duplicate PNG labels and create a list of corresponding TIFs
Dialog.create("Select Options");
Dialog.addDirectory("Labels folder", "");
Dialog.show();
dir1 = Dialog.getString();
list_dir1 = getFileList(dir1);
setBatchMode(true);
for (i=0; i<list_dir1.length; i++) {
showProgress(i+1, list_dir1.length);
open(dir1+list_dir1[i]);
setThreshold(2, 2); //select the class requiring balancing
threshold = 1.5; // threshold = (class minus 1) + 0.5
w = getWidth();
h = getHeight();
count = 0;
for (x = 0; x < w; x++) {
for(y = 0; y < h; y++) {
v = getPixel(x, y);
if (v > threshold)
count++;
}
}
if (count/(w*h) > 0.05) { //i.e. if image has >5% of the class
labels_dir = getInfo("image.directory");
path_list = getInfo("image.directory") + getInfo("image.filename");
path = getInfo("image.directory") + getInfo("image.filename") + "copy";
run("Duplicate...", " ");
saveAs("png", path);
close("*");
print(File.getNameWithoutExtension(path_list) + ".tif");
} else {
close();
}
}
//part 2: duplicate corresponding TIFs
Dialog.create("Select Options");
Dialog.addDirectory("Images folder", "");
Dialog.show();
img_dir = Dialog.getString();
img_list = img_dir + "list.txt";
selectWindow("Log");
saveAs("Text", img_list);
selectWindow("Log");
contents = getInfo();
close("Log");
if (indexOf(contents, "Title: ")>=0)
exit("Log file should list atleast one TIF file");
list = split(contents, "\n");
for (i=0; i<list.length; i++) {
open(list[i]);
pathtif = img_dir + getInfo("image.filename") + "copy";
run("Duplicate...", " ");
saveAs("tif", pathtif);
close("*");
}
Dialog.create("");
Dialog.addMessage("Script has completed running")
Dialog.show();