-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathROI_Checker.ijm
313 lines (277 loc) · 8.75 KB
/
ROI_Checker.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
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
/*
* Automatically segements localization generated data from a results table and image
*
* 2021 - 2022
*
* Author: Kierran Falloon
* University of Strathclyde, Glasgow, Scotland
* 4th Year MPhys Physics with Advanced Research
*
* Created with help and guidance from Sebastian van de Linde (s.vandelinde@strath.ac.uk)
* To be used in conjunction with associated python scripts to estimate precision
*
*/
pixelsize = 100
choices = initialdialog(); // Initial GUI
operation = choices[0];
pixelsize = choices[1];
running = choices[2];
while(running == true){ // Checks each function if relaunch is ticked
if(isOpen("Results") == false) exit("Please load a localization file");
if(operation == "Segment"){
segment();
choices = initialdialog(); // Returns to initial GUI
operation = choices[0];
pixelsize = choices[1];
running = choices[2];
}
if(operation == "Circularise ROIs"){
if(isOpen("ROI Manager") == false){
run("ROI Manager...");
}
circularise_rois();
choices = initialdialog();
operation = choices[0];
pixelsize = choices[1];
running = choices[2];
}
if(operation == "Mean-centre ROIs"){
if(isOpen("ROI Manager") == false){
run("ROI Manager...");
roiManager("delete");
}
mean_centre(pixelsize);
choices = initialdialog();
operation = choices[0];
pixelsize = choices[1];
running = choices[2];
}
}
if(running == false){ // If relaunch is not ticked
if(operation == "Segment"){
if(isOpen("Results") == false) exit("Please load a localization file");
segment();
}
if(operation == "Circularise ROIs"){
if(isOpen("ROI Manager") == false){
run("ROI Manager...");
roiManager("delete");
}
circularise_rois();
}
if(operation == "Mean-centre ROIs"){
if(isOpen("Results") == false) exit("Please load a localization file");
if(isOpen("ROI Manager") == false){
run("ROI Manager...");
roiManager("delete");
}
mean_centre(pixelsize);
}
run("Quit"); // End once function selected is complete
}
/* Functions */
function initialdialog(){
/***** GUI *****/
version = "1.0";
filename = File.getName("Filter-segment.ijm");
items = newArray("Segment", "Circularise ROIs", "Mean-centre ROIs");
html = html_message();
Dialog.create("ROI Checker " + version);
Dialog.setInsets(0,0,0);
Dialog.addMessage("Choose an operation:");
Dialog.addChoice("Operation", items);
Dialog.addNumber("Pixel size [nm]", pixelsize);
Dialog.addCheckbox("Relaunch?", 1);
Dialog.addHelp(html);
Dialog.show();
operation = Dialog.getChoice();
pixelsize = Dialog.getNumber();
running = Dialog.getCheckbox();
return newArray(operation, pixelsize, running);
}
function segment(){
Dialog.create("Segment");
/*** GUI ***/
Dialog.addMessage("Define ROI");
Dialog.addCheckbox("Draw ROI", 1);
Dialog.addMessage("Threshold options:");
Dialog.addCheckbox("Auto Threshold", 1);
thresh_options = getList("threshold.methods");
Dialog.addChoice("Method", thresh_options);
Dialog.show();
draw = Dialog.getCheckbox();
auto = Dialog.getCheckbox();
threshchoice = Dialog.getChoice();
if(draw == false){
if(auto == true){
Dialog.create("Options")
Dialog.addMessage("ROI options:");
Dialog.addNumber("x [µm]", 0);
Dialog.addNumber("y [µm]", 0);
Dialog.addNumber("w [µm]", 0);
Dialog.addNumber("h [µm]", 0);
Dialog.show();
roix = Dialog.getNumber()*pixelsize;
roiy = Dialog.getNumber()*pixelsize;
roiw = Dialog.getNumber()*pixelsize;
roih = Dialog.getNumber()*pixelsize;
}else{
Dialog.create("Options")
Dialog.addMessage("ROI options:");
Dialog.addNumber("x [µm]", 0);
Dialog.addNumber("y [µm]", 0);
Dialog.addNumber("w [µm]", 0);
Dialog.addNumber("h [µm]", 0);
Dialog.addMessage("Threshold options:");
Dialog.addSlider("Upper threshold", 0, 1000, 1e30);
Dialog.addSlider("Lower threshold", 0, 0, 0);
Dialog.show();
roix = (Dialog.getNumber()*1e-6)/pixelsize*1e-9;
roiy = (Dialog.getNumber()*1e-6)/pixelsize*1e-9;
roiw = (Dialog.getNumber()*1e-6)/pixelsize*1e-9;
roih = (Dialog.getNumber()*1e-6)/pixelsize*1e-9;
uthresh = Dialog.getNumber();
lthresh = Dialog.getNumber();
}
}
if(draw==true){
if(auto == true){
} // No GUI needed for these options
else{
Dialog.create("Options")
Dialog.addMessage("Threshold options:");
Dialog.addSlider("Upper threshold", 0, 1000, 1e30);
Dialog.addSlider("Upper threshold", 0, 0, 0);
Dialog.show();
uthresh = Dialog.getNumber();
lthresh = Dialog.getNumber();
}
}
title = getTitle(); // General for any file name
selectWindow(title);
dupetitle = title+"_dupe";
run("Duplicate...", dupetitle); // Make copy of image
dupetitle = getTitle();
if(auto == true){ // Each combination of options
setAutoThreshold(threshchoice+" dark no-reset");
} else{
call("ij.plugin.frame.ThresholdAdjuster.setMethod",threshchoice);
setThreshold(lthresh, uthresh);
}
selectWindow(dupetitle);
run("Create Mask");
if(draw == true){
waitForUser("Draw ROI");
} else{
makeRectangle(roix, roiy, roiw, roih);
}
selectWindow(dupetitle);
run("Restore Selection");
run("Clear Outside");
run("Create Selection");
selectWindow("mask");
close();
selectWindow(dupetitle);
keepchoice = newArray("Yes", "No"); // Segment validation
Dialog.create("Validation");
getThreshold(lower, upper);
Dialog.addMessage(threshchoice+": "+lower+"-"+upper);
Dialog.addChoice("Keep segment?", keepchoice);
Dialog.show();
keep = Dialog.getChoice();
if(keep == "Yes"){
selectWindow(title);
run("Restore Selection");
run("Clear Outside");
selectWindow(dupetitle);
close();
} else{
selectWindow(dupetitle);
close();
selectWindow(title);
segment();
}
}
function circularise_rois(){
/*** GUI ***/
Dialog.create("Circularise ROIs");
Dialog.addMessage("Circularise ROIs");
Dialog.addNumber("Define sigma:", 1);
Dialog.addSlider("Lower circularity threshold", 0.0, 1, 0.9);
Dialog.show();
sigma = Dialog.getNumber();
circlow = Dialog.getNumber();
title = getTitle();
getDimensions(width, height, channels, slices, frames);
run("Gaussian Blur...", sigma);
makeRectangle(0, 0, width, height);
setOption("BlackBackground", false);
run("Convert to Mask");
run("Analyze Particles...", " circularity=circlow 1 show=Overlay exclude include add");
selectWindow(title);
run("Restore Selection");
}
function mean_centre(pixelsize){
numROIs = roiManager("count");
SumROIs = 0;
f = File.open("");
print(f, "\"x [nm]\" "+","+ "\"y [nm]\"");
Rows = 0;
headings = split(Table.headings(), "\t" ); // Get table headings (different for thunderstorm and rapidstorm)
x = Table.getColumn(headings[2]);
y = Table.getColumn(headings[3]);
for(i=0; i<numROIs; i++) { // loop through ROIs (analyzed particles)
showProgress(-i+1/numROIs, numROIs);
roiManager("Select", i); // Select i-th roi
Rows = 0;
for (j = 0; j < nResults(); j++) { // Loop through Results table
if (Roi.contains(floor(x[j]/pixelsize), floor(y[j]/pixelsize)) == true) { // If coordinate in ROI
Rows += 1;
}
}
xarray = newArray(Rows); // Creates appropriately sized array for ROi
yarray = newArray(Rows); // To avoid 0's (if sized generically) impacting mean of array
k=0;
for (j = 0; j < nResults(); j++) { // Loop through Results table
if (Rows == NaN)
break;
if (Roi.contains(floor(x[j]/pixelsize), floor(y[j]/pixelsize)) == true) { // If coordinate in ROI
xarray[k] = x[j]/pixelsize;
yarray[k] = y[j]/pixelsize;
k+=1;
}
}
// Weighing each ROI against the mean
Array.getStatistics(xarray, min, max, mean, stdDev);
for (j = 0; j < xarray.length; j++) {
if (Rows == NaN)
break;
xarray[j] = xarray[j] - mean;
}
Array.getStatistics(yarray, min, max, mean, stdDev);
for (j = 0; j < yarray.length; j++) {
if (Rows == NaN)
break;
yarray[j] = yarray[j] - mean;
}
for (j = 0; j < yarray.length; j++) {
if (Rows == NaN)
break;
print(f, xarray[j]+","+yarray[j]);
SumROIs += 1;
}
} File.close(f);
print("Mean localizations per ROI = "+(SumROIs / numROIs));
}
function html_message(){
text = "<html>"
+"<h3>ROI Checker</h3>"
+"<h4>Operations</h4>"
+"<b>Operation:</b> select one of the following:<br>"
+"<b>Segment</b>: Segments data according to process laid out at <A HREF=https://imagej.net/imaging/segmentation> the ImageJ wiki </A>.<br>"
+"<b>Circularise ROIs</b>: Circularises each cluster of localizations using a gaussian fit with user-specified sigma.<br>"
+"<b>Mean-centre ROIs</b>: Subtracts the mean of localization positions within each individual ROI from the position of the ROI, <br>"
+"such that all localizations are centered around (0,0). It is then saved to 'log.txt', in CSV format, for postprocessing.<br>"
+"<small> Kierran Falloon, submitted in partial fulfilment for the degree of MPhys Physics with Advanced Research "
+"</font>";
return text;