forked from alehandro112/ImageJ-microglia-counter-macro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Microglia counter.txt
325 lines (300 loc) · 11.6 KB
/
Microglia counter.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
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
macro "Microglia counter [1]" {
MG_distance_in_pixels = parseInt(call("ij.Prefs.get",
"MG.distance_in_pixels","1177"));
MG_known_distance = parseInt(call("ij.Prefs.get",
"MG.known_distance","300"));
MG_pixel_aspect_ratio = parseInt(call("ij.Prefs.get",
"MG.pixel_aspect_ratio","1"));
MG_unit = call("ij.Prefs.get",
"MG.unit","um");
MG_iba_threshold = parseFloat(call("ij.Prefs.get",
"MG.iba_threshold","2.4"));
MG_dapi_threshold = parseFloat(call("ij.Prefs.get",
"MG.dapi_threshold","9.4"));
MG_overlap = parseFloat(call("ij.Prefs.get",
"MG.overlap","0.55"));
MG_iba_cell_size_l = parseFloat(call("ij.Prefs.get",
"MG.iba_cell_size_l","300"));
MG_iba_cell_size_u = parseFloat(call("ij.Prefs.get",
"MG.iba_cell_size_u","Infinity"));
MG_dapi_cell_size_l = parseFloat(call("ij.Prefs.get",
"MG.dapi_cell_size_l","300"));
MG_dapi_cell_size_u = parseFloat(call("ij.Prefs.get",
"MG.dapi_cell_size_u","Infinity"));
MG_directory = call("ij.Prefs.get",
"MG.directory","");
MG_tablename = call("ij.Prefs.get",
"MG.tablename","");
MG_savedir = call("ij.Prefs.get",
"MG.savedir","Yes");
//set scale, clear results window
run("Set Scale...", "distance="+MG_distance_in_pixels+" known="+MG_known_distance+" pixel="+MG_pixel_aspect_ratio+" unit="+MG_unit+" global");
run("Clear Results");
//get filenames and mean values to establish which image is dapi and iba
//darker image will be dapi; brighter image will be iba
selectImage(1);
img1 = getInfo("image.filename");
getStatistics(area, mean);
first = mean;
selectImage(2);
img2 = getInfo("image.filename");
getStatistics(area, mean);
second = mean;
iba = "";
dapi = "";
if (first>second){
iba = img1;
dapi = img2;
}
if (second>first){
iba = img2;
dapi = img1;
}
//remember full paths for original images
iba_dir = File.directory+iba;
dapi_dir = File.directory+dapi;
//substract background noise from iba
selectImage(iba);
getStatistics(area, mean);
run("Subtract...", "value=mean");
//threshold iba
setAutoThreshold("Otsu dark");
setThreshold(lower_threshold(MG_iba_threshold), 255);
setOption("BlackBackground", false);
run("Convert to Mask");
//fill holes and analyze particles in iba
run("Fill Holes");
run("Analyze Particles...", "size="+MG_iba_cell_size_l+"-"+MG_iba_cell_size_u+" pixel show=Masks clear include in_situ");
//substract background noise from dapi
selectImage(dapi);
getStatistics(area, mean);
run("Subtract...", "value=mean");
//threshold dapi
setAutoThreshold("Otsu dark");
run("Threshold...");
setThreshold(lower_threshold(MG_dapi_threshold), 255);
run("Convert to Mask");
//fill holes, watershed and analyze particles in dapi
run("Fill Holes");
run("Watershed");
//analyze partcles will add all nuclei from dapi to ROI manager
run("Analyze Particles...", "size="+MG_dapi_cell_size_l+"-"+MG_dapi_cell_size_u+" pixel show=Masks clear include add in_situ");
//create stack, divide both stack images by 2 and Z project
run("Images to Stack", "name=Stack title=[] use");
run("Divide...", "value=2.000 stack");
run("Z Project...", "projection=[Average Intensity]");
selectWindow("Stack");
close();
selectWindow("AVG_Stack");
run("Invert LUT");
run("Clear Results");
selectWindow("AVG_Stack");
nROI = roiManager("count");
roi_array = newArray();
overlap_array = newArray();
//select each nucleus from ROI manager
for (i=0; i<nROI; i++){
roiManager("select", i);
getStatistics(area, mean, min, max);
//if max intensity less than 128 - remove
if (max<128){
run("Cut");
}
//if max intensity is 128, calculate dapi/iba overlap
if (max==128){
overlap = (mean-64)/64;
//if overlap is more than threshold value, add the ROI index and overlap fraction
//to table. draw overlap values on image
if (overlap>MG_overlap){
overlap_array = Array.concat(overlap_array, overlap);
roi_index = roiManager("index");
roi_array = Array.concat(roi_array, roi_index);
Roi.getBounds(x, y, width, height);
setFont("SanSerif", 16, "antialiased");
Overlay.drawString(overlap, x, y-10);
}
}
//show drawn overlap values
Overlay.show;
}
//create stack with processed image and two original images
open(iba_dir);
open(dapi_dir);
run("Images to Stack", "name=Stack title=[] use");
//make results window with stats
getStatistics(area);
nIBA = roi_array.length;
run("Clear Results");
setResult("Iba1-count", 0, nIBA);
setResult("DAPI-count", 0, nROI);
setResult("Iba1 %", 0, nIBA/nROI*100);
setResult("Iba1 density (per mm2)", 0, nIBA/area*1000000);
setResult("DAPI density (per mm2)", 0, nROI/area*1000000);
setOption("ShowRowNumbers", false);
updateResults;
selectWindow("ROI Manager");
run("Close");
selectWindow("Threshold");
run("Close");
//if user set to save measurements, append results to the user defined file
full_path = MG_directory+MG_tablename+".txt"; //full path of the user defined file
twname = MG_tablename+".txt";
tname = "["+twname+"]";
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
//Check if .txt file exists in user defined directory
if (MG_savedir == "Yes"){
if (!File.exists(full_path)){ //if file does not exist -> create new file
tfile = File.open(full_path);
//print(tfile);
//run("Text File... ", "open="+full_path+"");
print(tfile, "Filenames,Timestamp,Iba1-count,DAPI-count,Iba1 %,Iba1 density,DAPI density");//write headers to file
File.close(tfile);
run("Text File... ", "open="+full_path+"");
results =dapi_dir+"\n"+ iba_dir+","+dayOfMonth+"-"+month+1+"-"+year+" ; "+hour+":"+minute+","+toString(nIBA)+","+toString(nROI)+","+toString(nIBA/nROI*100)+","+toString(nIBA/area*1000000)+","+toString(nROI/area*1000000);
print (tname, results);
selectWindow(twname);
content = getInfo("window.contents");
print(tname, "\\Close");
tfile = File.open(full_path);
print(tfile, content);
File.close(tfile);
}
else {
run("Text File... ", "open="+full_path+"");
results = dapi_dir+"\n"+ iba_dir+","+dayOfMonth+"-"+month+1+"-"+year+" ; "+hour+":"+minute+","+toString(nIBA)+","+toString(nROI)+","+toString(nIBA/nROI*100)+","+toString(nIBA/area*1000000)+","+toString(nROI/area*1000000);
print (tname, results);
selectWindow(twname);
content = getInfo("window.contents");
print(tname, "\\Close");
tfile = File.open(full_path);
print(tfile, content);
File.close(tfile);
}
}
function lower_threshold(percent_threshold){
h = getHeight();
w = getWidth();
n = w*h;
nBins = 256;
histMin = 0;
histMax = 255;
getHistogram(values, counts, nBins);
cum = newArray(nBins);
cum[0] = counts[0];
cdf = newArray(nBins);
cdf[0] = cum[0]/n;
fBin = 0;
for (i = (histMin + 1); i < (histMax + 1); i++){
cum[i] = counts[i] + cum[i-1];
cdf[i] = cum[i]/n;
percent = cdf[i]*100;
if ((100-percent)>=percent_threshold){
fBin = i;
}
}
return fBin;
}
}
macro "Settings for microglia counter [0]" {
MG_distance_in_pixels = parseInt(call("ij.Prefs.get",
"MG.distance_in_pixels","1177"));
MG_known_distance = parseInt(call("ij.Prefs.get",
"MG.known_distance","300"));
MG_pixel_aspect_ratio = parseInt(call("ij.Prefs.get",
"MG.pixel_aspect_ratio","1"));
MG_unit = call("ij.Prefs.get",
"MG.unit","um");
MG_iba_threshold = parseFloat(call("ij.Prefs.get",
"MG.iba_threshold","2.4"));
MG_dapi_threshold = parseFloat(call("ij.Prefs.get",
"MG.dapi_threshold","9.4"));
MG_overlap = parseFloat(call("ij.Prefs.get",
"MG.overlap","0.55"));
MG_iba_cell_size_l = parseFloat(call("ij.Prefs.get",
"MG.iba_cell_size_l","300"));
MG_iba_cell_size_u = parseFloat(call("ij.Prefs.get",
"MG.iba_cell_size_u","Infinity"));
MG_dapi_cell_size_l = parseFloat(call("ij.Prefs.get",
"MG.dapi_cell_size_l","300"));
MG_dapi_cell_size_u = parseFloat(call("ij.Prefs.get",
"MG.dapi_cell_size_u","Infinity"));
MG_directory = call("ij.Prefs.get",
"MG.directory","");
MG_tablename = call("ij.Prefs.get",
"MG.tablename","");
MG_savedir = call("ij.Prefs.get",
"MG.savedir","Yes");
Dialog.create("Microglia Counter Settings");
Dialog.addMessage("Scaling settings")
Dialog.addNumber("Distance in pixels", MG_distance_in_pixels,0,6,"px");
Dialog.addNumber("Known distance", MG_known_distance,0,6,"");
Dialog.addNumber("Pixel aspect ratio", MG_pixel_aspect_ratio,0,6,"");
Dialog.addString("Units", MG_unit,6);
Dialog.addMessage("-----------------------------------------------------------------------------------------------------");
Dialog.addMessage("Threshold settings\nOpen 'Threshold settings' to determine the best threshold % value");
Dialog.addNumber("Iba1 threshold", MG_iba_threshold,2,6,"%");
Dialog.addNumber("DAPI threshold", MG_dapi_threshold,2,6,"%");
Dialog.addMessage("-----------------------------------------------------------------------------------------------------");
Dialog.addMessage("Overlap settings\nAdjust the overlap ratio 0.00-1.00");
Dialog.addNumber("Overlap", MG_overlap,2,6,"");
Dialog.addMessage("-----------------------------------------------------------------------------------------------------");
Dialog.addMessage("Cell size settings\nOpen 'Analyze particles' to determine the best size");
Dialog.addNumber("Iba1 lower limit", MG_iba_cell_size_l,2,6,"");
Dialog.addNumber("Iba1 upper limit", MG_iba_cell_size_u,2,6,"");
Dialog.addNumber("DAPI lower limit", MG_dapi_cell_size_l,2,6,"");
Dialog.addNumber("DAPI upper limit", MG_dapi_cell_size_u,2,6,"");
Dialog.addMessage("-----------------------------------------------------------------------------------------------------");
Dialog.addMessage("Output settings");
items = newArray("Yes", "No");
Dialog.addRadioButtonGroup("Save measurements?", items, 1, 2, MG_savedir);
Dialog.show();
MG_distance_in_pixels = Dialog.getNumber();
MG_known_distance = Dialog.getNumber();
MG_pixel_aspect_ratio = Dialog.getNumber();
MG_unit = Dialog.getString();
MG_iba_threshold = Dialog.getNumber();
MG_dapi_threshold = Dialog.getNumber();
MG_overlap = Dialog.getNumber();
MG_iba_cell_size_l = Dialog.getNumber();
MG_iba_cell_size_u = Dialog.getNumber();
MG_dapi_cell_size_l = Dialog.getNumber();
MG_dapi_cell_size_u = Dialog.getNumber();
MG_savedir = Dialog.getRadioButton();
if (MG_savedir == "Yes"){
MG_directory = getDirectory("Choose where to save measurements");
Dialog.create("Measurements filename");
Dialog.addString("File name (without spaces)", MG_tablename,7);
Dialog.addToSameRow();
Dialog.addMessage(".txt");
Dialog.show();
MG_tablename = Dialog.getString();
}
call("ij.Prefs.set", "MG.distance_in_pixels",toString
(MG_distance_in_pixels));
call("ij.Prefs.set", "MG.known_distance",toString
(MG_known_distance));
call("ij.Prefs.set", "MG.pixel_aspect_ratio",toString
(MG_pixel_aspect_ratio));
call("ij.Prefs.set", "MG.unit",toString
(MG_unit));
call("ij.Prefs.set", "MG.iba_threshold",toString
(MG_iba_threshold));
call("ij.Prefs.set", "MG.dapi_threshold",toString
(MG_dapi_threshold));
call("ij.Prefs.set", "MG.overlap",toString
(MG_overlap));
call("ij.Prefs.set", "MG.iba_cell_size_l",toString
(MG_iba_cell_size_l));
call("ij.Prefs.set", "MG.iba_cell_size_u",toString
(MG_iba_cell_size_u));
call("ij.Prefs.set", "MG.dapi_cell_size_l",toString
(MG_dapi_cell_size_l));
call("ij.Prefs.set", "MG.dapi_cell_size_u",toString
(MG_dapi_cell_size_u));
call("ij.Prefs.set", "MG.tablename",toString
(MG_tablename));
call("ij.Prefs.set", "MG.savedir",toString
(MG_savedir));
call("ij.Prefs.set", "MG.directory",toString
(MG_directory));
}