-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpollen_classifier.js
More file actions
342 lines (276 loc) · 8.97 KB
/
Copy pathpollen_classifier.js
File metadata and controls
342 lines (276 loc) · 8.97 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* pollenjs: A pollen classifier plugin for ImageJ
* Copyright (C) 2017 Jean-Christophe Taveau.
*
* This file is part of pollenjs
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with pollenjs. If not, see <http://www.gnu.org/licenses/>.
*
*
* Authors:
* Rudy Anne
* Jean-Christophe Taveau
*/
var tools = {};
/**
* Dialog Window for various settings
*
*/
tools.GUI = function () {
function basicGUI() {
var gd=new GenericDialog("Pollen Params");
gd.addChoice("Predefined", ["Advanced","Colza","Peach","Strawberry","Tomato"],"Advanced");
gd.addCheckbox("Thumbnails", true);
gd.showDialog();
if (gd.wasCanceled()) {
return undefined;
}
else {
settings.type = gd.getNextChoice();
settings.thumb = gd.getNextBoolean();
}
return settings;
}
function advancedGUI() {
var size = 100.0;
var sig1 = 6.0;
var sig2 = 8.0;
var thrshld = 100;
var gd=new GenericDialog("Advanced Pollen Params");
gd.addCheckbox("Dark Pollen", true);
gd.addCheckbox("Thumbnails", true);
gd.addNumericField("Particle Diameter (px):", size, 1);
gd.addNumericField("Sigma1:", sig1, 2);
gd.addNumericField("Sigma2:", sig2, 3);
gd.addNumericField("Threshold", thrshld, 4);
gd.showDialog();
if (gd.wasCanceled()) {
return undefined;
}
else {
settings.dark = gd.getNextBoolean();
settings.thumb = gd.getNextBoolean();
settings.diameter = gd.getNextNumber();
settings.size = settings.diameter * 3;
settings.sig1 = gd.getNextNumber();
settings.sig2 = gd.getNextNumber();
settings.threshold = gd.getNextNumber();
}
return settings;
}
var predefined = {
"Colza" : {
dark :true,
diameter :40.0,
sig1 : 16.0,
sig2 : 18.0,
threshold :100
},
"Peach" : {
},
"Strawberry": {
},
"Tomato" : {
dark :false,
diameter : 30.0,
sig1 : 6.0,
sig2 : 9.0,
threshold :80
}
};
var settings = {scale: 4,size: -1};
// Select file window
var od =new OpenDialog("Choose a file", null);
var folder = od.getDirectory();
settings.extension = od.getFileName().split('.').pop();
settings.path = folder;
var dir = new java.io.File(folder);
var files = dir.listFiles();
settings.filenames = [];
for (var i = 0; i < files.length; i++) {
settings.filenames.push(files[i].toString().substring(folder.length));
}
settings.filenames.sort();
settings.path = folder;
settings.folder = folder.substring(0,folder.length - 1).split('/').pop();
settings = basicGUI();
if (settings.type === 'Advanced') {
settings = advancedGUI();
}
else {
settings.dark = predefined[settings.type].dark;
settings.diameter = predefined[settings.type].diameter;
settings.size = settings.diameter * 3;
settings.sig1 = predefined[settings.type].sig1;
settings.sig2 = predefined[settings.type].sig2;
settings.threshold = predefined[settings.type].threshold;
}
return settings;
}
/**
* DoG: Difference of Gaussian
*/
tools.DoG = function(imp,sig1,sig2) {
var copy = imp.duplicate();
if (settings.dark) {
copy.getProcessor().invert();
}
var imp1 = copy.duplicate(); imp1.setTitle("sigma 1");
var imp2 = copy.duplicate(); imp2.setTitle("sigma 2");
IJ.run(imp1, "Gaussian Blur...", "sigma=" + sig1);
IJ.run(imp2, "Gaussian Blur...", "sigma=" + sig2);
var ic = new ImageCalculator();
var dog = ic.run("Subtract create", imp1, imp2);
IJ.run(dog, "Enhance Contrast...", "saturated=0.0 normalize");
dog.setTitle("DoG");
// Clean up imageplus
imp1.close();
imp2.close();
// return
if (DEBUG) {
dog.show();
}
return dog;
}
/**
* Find Particles from DoG filtered image
*
*/
tools.findParticles = function(imageplus,threshold) {
return polygon = new MaximumFinder().getMaxima(imageplus.getProcessor(), threshold, false);
}
/**
* Pick and Extract Particles
*
*/
tools.pickParticles = function(org,coords,src,out) {
var results = ResultsTable.getResultsTable();
var x = coords.xpoints;
var y = coords.ypoints;
//var d = results.getColumn(results.getColumnIndex('Diameter'));
var half = settings.size/2.0;
for(var i = 0;i < x.length; i++){
// Remove outliers
if (x[i] - half >= 0.0 && x[i] + half < org.width && y[i] - half >= 0.0 && y[i] + half < org.height) {
// Extract
particles.push({x: x[i], y: y[i], type: 'U', box: settings.size, source: settings.folder + '/' + src});
org.setRoi(new Roi(1.0*x[i]-half,1.0*y[i]-half,settings.size,settings.size) );
var tmp = org.duplicate();
out.getImageStack().addSlice(tmp.getProcessor());
// out.updateImage();
}
}
}
tools.overlaps = function(pctls) {
for (var i in pctls) {
}
}
tools.displaySettings = function() {
for (var prop in settings) {
IJ.log(prop + ': '+settings[prop]);
}
}
/*
* pollenjs: A pollen classifier plugin for ImageJ
* Copyright (C) 2017 Jean-Christophe Taveau.
*
* This file is part of pollenjs
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with pollenjs. If not, see <http://www.gnu.org/licenses/>.
*
*
* Authors:
* Rudy Anne
* Jean-Christophe Taveau
*/
//Parameters
var DEBUG = false;
var particles = [];
IJ.log('\\Clear');
// 00-Dialog and wait for answer
var settings = tools.GUI();
if (settings === undefined) {
IJ.log("Script canceled");
throw "End of Script";
}
// 0- Create Particles stack and thumbnails stack if asked
var gallery = new ImagePlus("Gallery", ImageStack.create(settings.size, settings.size,1,8));
var stack;
var last = 0; // Last particle index
//**************** M A I N L O O P ****************/
var start_time = new Date();
for (var i in settings.filenames) {
var filename = settings.filenames[i].toString();
// IJ.log('Image#'+i+ ': '+ filename + '===?' + settings.extension);
if (filename.split('.').pop() === settings.extension) {
var image = IJ.openImage(settings.path + filename);
var imp = image.duplicate();
IJ.run(imp, "8-bit", "");
IJ.run(imp, "Subtract Background...", "rolling=50"+(settings.dark ? " light": "") );
// 1- DoG
var imp3 = tools.DoG(imp,settings.sig1,settings.sig2);
// 2- Find Maxima
var coords = tools.findParticles(imp3,settings.threshold);
// 3- Picking and Particles Extraction
tools.pickParticles(imp,coords, filename, gallery);
if (settings.thumb) {
IJ.run(image, "Size...", "width="+image.getWidth()/settings.scale+" height="+image.getHeight()/settings.scale+" constrain average interpolation=None");
if (stack === undefined) {
stack = new ImagePlus("Thumbnails", ImageStack.create(image.getWidth(), image.getHeight(),1,24));
}
stack.getImageStack().addSlice(image.getProcessor());
stack.getImageStack().setSliceLabel(filename, stack.getNSlices());
var ip = stack.getImageStack().getProcessor(stack.getNSlices());
ip.setColor(new java.awt.Color(255,0,255));
var small = settings.size/settings.scale;
for (var i= last; i < particles.length; i++) {
ip.drawOval(particles[i].x/settings.scale - small/2.0,particles[i].y/settings.scale - small/2.0,small,small);
}
last = particles.length;
}
}
}
gallery.getImageStack().deleteSlice(1);
gallery.show();
// 4- Classification
// 5- Print Particles Coordinates
var results = new ResultsTable();
for (var i = 0; i < particles.length; i++) {
results.incrementCounter();
results.addValue('X',particles[i].x);
results.addValue('Y',particles[i].y);
results.addValue('type',particles[i].type);
results.addValue('src',particles[i].source);
}
results.showRowNumbers(true);
results.show("Coords Table");
var end_time = new Date();
IJ.log('Operation took ' + (end_time.getTime() - start_time.getTime()) + ' msec');
if (settings.thumb) {
stack.getImageStack().deleteSlice(1);
stack.show();
}
tools.displaySettings();
// throw "End of Script";