-
Notifications
You must be signed in to change notification settings - Fork 0
/
BKB_IJ.ijm
257 lines (245 loc) · 7.2 KB
/
BKB_IJ.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// "Filter Grain images"
//
// This macro batch processes all the files in a folder and any
// subfolders in that folder. It will run the color threshold on any pictures
// in the folders and output to the specified directory.
// Swap out filterImage() in the processFile() method if you need other functionality.
// The input directory should contain only picture files such as 1.jpg, 2.jpg, 3.jpeg or
// other image files.
requires("1.33s");
// ****************************
// If you want to set a static directory, you need double slashes '\\'. A '\' is an escape character.
// Similarly if you need an apostrophe ', you'd need to do \'
// ****************************
//This prompts the user to pick an input/output directory
dir = getDirectory("Choose an input directory ");
destDir = getDirectory("Choose an image output directory");
File.makeDirectory(destDir+"ProcessedImages\\");
imageDir = destDir+"ProcessedImages\\";
setBatchMode(true);
count = 0;
countFiles(dir);
n = 0;
processFiles(dir, destDir);
selectWindow("Summary");
saveAs("Measurements",destDir+"BlackSummary.xls");
function countFiles(dir) {
list = getFileList(dir);
for (picNum=0; picNum<list.length; picNum++) {
if (endsWith(list[picNum], "/"))
countFiles(dir+list[picNum]);
else
count++;
}
}
function makeMontage() {
setBatchMode(true);
id=getImageID;
n= nSlices;
columns=floor(sqrt(n));
if (columns*columns<n) columns++;
rows = columns;
if (n<=columns*(rows-1)) rows--;
sw=getWidth; sh=getHeight;
type = ""+bitDepth;
if (type=="24") type = "RGB";
newImage("test", type+" black", columns*getWidth, rows*getHeight, 1);
iw=getWidth/columns; ih=getHeight/rows;
montage=getImageID;
selectImage(id);
for (i=0; i<n; i++) {
selectImage(id);
setSlice(i+1);
run("Select All");
run("Copy");
selectImage(montage);
makeRectangle((i%columns)*iw, floor(i/columns)*ih, sw, sh);
run("Paste");
}
resetMinAndMax();
}
function processFiles(dir, destDir) {
list = getFileList(dir);
for (fileNum=0; fileNum<list.length; fileNum++) {
if (endsWith(list[fileNum], "/"))
processFiles(dir+list[fileNum],destDir+list[fileNum]);
else {
showProgress(n++, count);
path = dir+list[fileNum];
File.makeDirectory(destDir+"temp\\");
tempDir = destDir+"temp\\";
imagePath = imageDir+list[fileNum];
editPath = tempDir+"A_"+list[fileNum];
blackPath = tempDir+"B_"+list[fileNum];
redPath = tempDir+"C_"+list[fileNum];
drupePath = tempDir+"D_"+list[fileNum];
picName = list[fileNum];
processFile(path, editPath, blackPath, redPath, drupePath, imagePath, picName);
File.delete(editPath);
File.delete(blackPath);
File.delete(redPath);
File.delete(drupePath);
File.delete(tempDir);
}
}
}
function processFile(path, editPath, blackPath, redPath, drupePath, imagePath, picName) {
open(path);
run("Size...", "width=1000 height=666");
makeLine(947,350,947,586);
run("Set Scale...", "distance=1 known=1 unit=pixel");
run("Chart White Balance", "chart=[Small MacBeth ColorChecker Chart] include");
makeRectangle(760,280,1000,666);
setForegroundColor(50, 168, 82);
run("Fill", "slice");
//run("Crop");
saveAs("jpeg", editPath);
close();
open(editPath);
filterImage1();
saveAs("jpeg", blackPath);
close();
open(editPath);
filterImage2();
saveAs("jpeg", redPath);
close();
open(editPath);
filterImage3();
saveAs("jpeg", drupePath);
close(); close(); close(); close();
open(editPath);
open(drupePath);
open(blackPath);
open(redPath);
run("Images to Stack", "name=stack title=[] use");
makeMontage();
saveAs("jpeg", imagePath);
close();
}
//This is the filter for whole berries
function filterImage1(){
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
call("ij.plugin.frame.ColorThresholder.RGBtoLab");
run("RGB Stack");
run("Convert Stack to Images");
selectWindow("Red");
rename("0");
selectWindow("Green");
rename("1");
selectWindow("Blue");
rename("2");
min[0]=0;
max[0]=255;
filter[0]="pass";
min[1]=110;
max[1]=255;
filter[1]="pass";
min[2]=0;
max[2]=255;
filter[2]="pass";
for (i=0;i<3;i++){
selectWindow(""+i);
setThreshold(min[i], max[i]);
run("Convert to Mask");
if (filter[i]=="stop") run("Invert");
}
imageCalculator("AND create", "0","1");
imageCalculator("AND create", "Result of 0","2");
for (i=0;i<3;i++){
selectWindow(""+i);
close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);
run("Analyze Particles...", "size=50-Infinity circularity=0.00-1.00 show=Masks include summarize");
}
//This is the filter for red regions
function filterImage2(){
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
call("ij.plugin.frame.ColorThresholder.RGBtoLab");
run("RGB Stack");
run("Convert Stack to Images");
selectWindow("Red");
rename("0");
selectWindow("Green");
rename("1");
selectWindow("Blue");
rename("2");
min[0]=0;
max[0]=100;
filter[0]="pass";
min[1]=138;
max[1]=255;
filter[1]="pass";
min[2]=0;
max[2]=255;
filter[2]="pass";
for (i=0;i<3;i++){
selectWindow(""+i);
setThreshold(min[i], max[i]);
run("Convert to Mask");
if (filter[i]=="stop") run("Invert");
}
imageCalculator("AND create", "0","1");
imageCalculator("AND create", "Result of 0","2");
for (i=0;i<3;i++){
selectWindow(""+i);
close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);
run("Analyze Particles...", "size=5-Infinity circularity=0.00-1.00 show=Masks include summarize");
}
//This is the filter for counting drupelets
function filterImage3(){
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
call("ij.plugin.frame.ColorThresholder.RGBtoLab");
run("RGB Stack");
run("Convert Stack to Images");
selectWindow("Red");
rename("0");
selectWindow("Green");
rename("1");
selectWindow("Blue");
rename("2");
min[0]=25;
max[0]=255;
filter[0]="pass";
min[1]=110;
max[1]=255;
filter[1]="pass";
min[2]=0;
max[2]=255;
filter[2]="pass";
for (i=0;i<3;i++){
selectWindow(""+i);
setThreshold(min[i], max[i]);
run("Convert to Mask");
if (filter[i]=="stop") run("Invert");
}
imageCalculator("AND create", "0","1");
imageCalculator("AND create", "Result of 0","2");
for (i=0;i<3;i++){
selectWindow(""+i);
close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);
run("Watershed");
run("Analyze Particles...", "size=5-Infinity show=Outlines include summarize");
}