-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTrimming.txt
124 lines (99 loc) · 3.45 KB
/
Trimming.txt
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
function get_borders(filepath) {
open(filepath);
getDimensions(width, height, channels, slices, frames);
margin = width/4;
y_border = newArray(0,0,0,0,0,0); // {image1_top, image1_bottom, image2_top, image2_bottom, image3_top, image3_bottom}
i = 0; // index of the border array
flag = 0; // flag == 1 when the line is on a brain image
y = 0;
makeLine(margin, 0, width - margin, 0);
getStatistics(area, mean, min, max, std, histogram);
while(std > threshold) { // igonore noise around edge
y += interval;
makeLine(margin, y, width - margin, y);
getStatistics(area, mean, min, max, std, histogram);
}
while(y < height) {
makeLine(margin, y, width - margin, y);
getStatistics(area, mean, min, max, std, histogram);
if(std > threshold && flag == 0) { // beginning of the brain image
y_border[i] = y;
i += 1;
flag = 1;
}
if(std < threshold && flag == 1) { // end of the brain image
y_border[i] = y;
i += 1;
flag = 0;
}
if(i == 6) break;
y += interval;
}
x_border = newArray(0,0,0,0,0,0); // {image1_left, image1_right, image2_left, image2_right, image3_left, image3_right}
x = width/2 - margin;
i=0; // index of the border array
while(x > 0) {
makeLine(x, y_border[i*2], x, y_border[i*2+1]);
getStatistics(area, mean, min, max, std, histogram);
if(std < threshold) { // beginning of the brain image
x_border[i*2] = x;
i += 1;
}
if(i == 3) break;
x -= interval;
}
x = width/2 + margin;
i=0; // index of the border array
while(x < width) {
makeLine(x, y_border[i*2], x, y_border[i*2+1]);
getStatistics(area, mean, min, max, std, histogram);
if(std < threshold) { // beginning of the brain image
x_border[i*2+1] = x;
i += 1;
}
if(i == 3) break;
x += interval;
}
close();
return Array.concat(x_border, y_border, width, height);
}
function trim_images(low_borders, filename){
filepath = input_directory + filename + ".high.tif";
open(filepath);
borders = newArray(0,0,0,0,0,0,0,0,0,0,0,0,0,0);
for(i=0;i<6;i++){
borders[i] = low_borders[i] * getWidth / low_borders[12];
}
for(i=6;i<12;i++){
borders[i] = low_borders[i] * getHeight / low_borders[13];
}
borders[12] = getWidth;
borders[13] = getHeight;
x_margin = borders[12] * 0.08;
y_margin = borders[13] * 0.03;
for(j=0;j<3;j++){
left = borders[j*2] - x_margin;
top = borders[j*2+6] - y_margin;
width = borders[j*2+1] - borders[j*2] + 2 * x_margin;
height = borders[j*2+7] - borders[j*2+6] + 2 * y_margin;
makeRectangle(left, top, width, height);
run("Duplicate...", "duplicate");
run("Rotate 90 Degrees Right");
run("Rotate 90 Degrees Right");
run("Save", "save=" + input_directory + filename + "-" + j + ".tif");
close();
}
close();
}
threshold = 20;
interval = 20;
input_directory = getArgument;
if (input_directory=="") exit ("No argument!");
filelist = getFileList(input_directory);
for (i=0; i<filelist.length; i++) {
split_filename = split(filelist[i], ".");
if (split_filename[1] == "low") {
borders = get_borders(input_directory + filelist[i]);
trim_images(borders, split_filename[0]);
}
}