-
Notifications
You must be signed in to change notification settings - Fork 0
/
MuscleMetrics.ijm
483 lines (445 loc) · 15.2 KB
/
MuscleMetrics.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* **********************************************
MuscleMetrics.ijm
**********************************************
Author: Winnok H. De Vos
Date Created: November 25, 2016
Date Last Modified: December 20, 2016
Interactive tools to facilitate assessment of muscle organisation
Includes a segmentation of connected structures and measurement of size, shape and local thickness
Also includes an option to measure directionality
Segmentation of muscle fibers relies on a directional derivative for which the plugin FeatureJ is used (written by E. Meijiering, https://imagescience.org/meijering/software/featurej/)
Elliptic fourier descriptors requires an additional plugin written by Thomas Boudier and Ben Tupper (http://imagejdocu.tudor.lu/doku.php?id=plugin:analysis:fourier_shape_analysis:start)
Written for Ineke D'Hondt and Bart Braeckman, Ugent
**********************************************
*/
/*
***********************
Variable initiation
***********************
*/
// Numbers
var line_thickness = 200; // thickness of selection for straightening
var gauss_scale = 1; // gauss/laplace scale
var min_size = 50; // minimum structure size
var max_size = 1000000; // maximum structure size
var pixel_size = 0.15; // pixel size in micron
var enhance_radius = 10; // radius for local contrast enhancement
var bg_radius = 0; // radius for background correction
// Strings
var threshold_method = "Yen"; // autothreshold method
var micron = getInfo("micrometer.abbreviation"); // micron symbol
var enhancer = "laplace"; // muscle fiber enhancement algorithm
// Arrays
var thresholds = getList("threshold.methods"); // autothreshold algorithms
var enhancers = newArray("gauss","laplace"); // muscle fiber enhancement algorithms
// Booleans
var thickness = true; // include local thickness measurement
var skeleton = true; // include skeleton analysis
var efd = false; // include elliptic fourier descriptor extraction
var corner = true; // include corner detection
/*
***********************
Macro Tools
***********************
*/
macro "Autorun"
{
erase();
setOptions();
}
macro "Settings Action Tool - C438 T5f16S"
{
setOptions()
setup();
}
macro "Sarcomere Selection Action Tool - C333 R33dd"
{
erase();
run("Set Measurements...", " fit redirect=None decimal=4");
id = getImageID;
title = getTitle;
if(selectionType()==-1)
{
setTool("polygon");
waitForUser("First Draw a Polygon Around One Sarcomer and Press OK");
}
run("Duplicate...","title=["+title+"] duplicate channels=1");
sarcomere_id = getImageID;
run("Clear Outside", "slice");
run("Measure");
angle = getResult("Angle",0);
run("Select None");
run("Rotate... ", "angle="+angle+" grid=1 interpolation=Bilinear enlarge");
selectImage(id); close;
erase();
}
macro "Straighten Action Tool - C333 P0347b8fc0 "
{
line_thickness = getNumber("Line Thickness",line_thickness);
id = getImageID;
if(selectionType()==-1)
{
setTool("polyline");
waitForUser("First Draw a Segmented line and Press OK");
}
run("Properties... ", " width="+line_thickness);
run("Straighten...", "title=Straightened line="+line_thickness+" process");
selectImage(id);
close;
}
macro "Analyze Directionality Action Tool - C333 T3e16< T3e16>"
{
id = getImageID;
selectImage(id);
run("Directionality", "method=[Fourier components] nbins=90 histogram=-90");
}
macro "Analyze Objects Action Tool - C333 T3f16A"
{
erase();
setBatchMode(true);
id = getImageID;
decalibrateImage(id);
obj_nr = segmentObjects(id);
if(obj_nr > 0) analyzeObjects(id,obj_nr);
calibrateImage(id);
setBatchMode("exit and display");
toggleOverlay();
}
macro "Save And Close Action Tool - C333 T3f16C"
{
dir = getDirectory("");
id = getImageID;
title = getTitle;
prefix = getString("Prefix", title);
// save image
selectImage(id);
run("Remove Overlay");
saveAs(".tif", dir+prefix+"_Cropped.tif");
close;
// save results
selectWindow("Object_Results");
saveAs("Results", dir+prefix+"_Object_Results.txt");
run("Close");
// save log
selectWindow("Log");
saveAs("Text", dir+prefix+"_Log.txt");
run("Close");
// save rois
roiManager("deselect");
roiManager("Save", dir+prefix+"_RoiSet.zip");
erase();
}
macro "Toggle Overlay Action Tool - Caaa O11ee"
{
toggleOverlay();
}
macro "[t] Toggle Overlay"
{
toggleOverlay();
}
/*
***********************
Functions
***********************
*/
function erase()
{
run("Clear Results");
roiManager("reset");
run("Collect Garbage");
}
function setOptions()
{
run("Options...", "iterations = 1 count = 1");
run("Colors...", "foreground=white background=black selection=yellow");
run("Appearance...", " antialiased menu = 0");
run("Overlay Options...", "stroke = red width = 1 fill = none");
setOption("BlackBackground", false);
run("Set Measurements...", "area mean min fit perimeter shape feret's redirect=None decimal=4");
run("Misc...", "divide=Infinity reverse");
run("Conversions...", " ");
setBackgroundColor(0, 0, 0);
setForegroundColor(255,255,255);
}
function getMoment()
{
MonthNames = newArray("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
DayNames = newArray("Sun", "Mon","Tue","Wed","Thu","Fri","Sat");
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
time_string ="Date: "+DayNames[dayOfWeek]+" ";
if (dayOfMonth<10) {time_string = time_string+"0";}
time_string = time_string+dayOfMonth+"-"+MonthNames[month]+"-"+year+"\nTime: ";
if (hour<10) {time_string = time_string+"0";}
time_string = time_string+hour+":";
if (minute<10) {time_string = time_string+"0";}
time_string = time_string+minute+":";
if (second<10) {time_string = time_string+"0";}
time_string = time_string+second;
return time_string;
}
function setup()
{
setOptions();
Dialog.create("MuscleMetrics - Object Analysis");
Dialog.addNumber("Pixel Size (ignored if calibrated)",pixel_size,3,5,micron);
Dialog.addNumber("Background Correction Radius",bg_radius,0,5,"");
Dialog.addNumber("Local Contrast Enhance Radius",enhance_radius,0,5,"");
Dialog.addChoice("Object Enhancement Algorithm",enhancers,enhancer);
Dialog.addNumber("Gaussian Smoothing Radius",gauss_scale,0,5,"");
Dialog.addChoice("Autothreshold Algorithm",thresholds,threshold_method);
Dialog.addNumber("Minimum Object Area ",min_size,0,5,"pixels");
Dialog.addNumber("Maximum Object Area ",max_size,0,5,"pixels");
labels = newArray(4);
defaults = newArray(4);
labels[0] = "Incl. Local Thickness";
defaults[0] = thickness;
labels[1] = "Incl. Skeleton Analysis";
defaults[1] = skeleton;
labels[2] = "Incl. Elliptic Descriptors";
defaults[2] = efd;
labels[3] = "Incl. Corner Detection";
defaults[3] = corner;
Dialog.addCheckboxGroup(2,2,labels,defaults);
Dialog.show;
print("\\Clear");
moment = getMoment();
print(moment+"\n");
print("*******************************************");
print(" Settings");
print("*******************************************");
pixel_size = Dialog.getNumber(); print("image pixel size:",pixel_size);
bg_radius = Dialog.getNumber(); print("backkground correction radius:",bg_radius);
enhance_radius = Dialog.getNumber(); print("local contrast enhancement radius:",enhance_radius);
enhancer = Dialog.getChoice(); print("object enhancer:",enhancer);
gauss_scale = Dialog.getNumber(); print("gauss smoothing scale:",gauss_scale);
threshold_method = Dialog.getChoice(); print("autothreshold method:",threshold_method);
min_size = Dialog.getNumber(); print("min object size:",min_size);
max_size = Dialog.getNumber(); print("max object size:",max_size);
thickness = Dialog.getCheckbox(); print("include local thickness:",thickness);
skeleton = Dialog.getCheckbox(); print("include skeletonization:",skeleton);
efd = Dialog.getCheckbox(); print("include EFD:",efd);
corner = Dialog.getCheckbox(); print("include corner detection:",corner);
print("*******************************************");
}
function calibrateImage(id)
{
selectImage(id);
getPixelSize(unit, pixelwidth, pixelheight);
if(indexOf(unit,"pixel")>=0)run("Properties...", " unit=µm pixel_width="+pixel_size+" pixel_height="+pixel_size);
else pixel_size = pixelwidth;
print("Image Calibrated, Calibration =",pixel_size,micron,"per pixel");
}
function decalibrateImage(id)
{
selectImage(id);
getPixelSize(unit, pixel_width, pixel_height);
if(indexOf(unit,"pixel")==0)pixel_size = pixel_width;
run("Properties...", " unit=pixel pixel_width=1 pixel_height=1");
print("Image Decalibrated, Calibration = 1 pixel per pixel");
}
function toggleOverlay()
{
roiManager("Show All without labels");
roiManager("Show None");
run("Select None"); roiManager("deselect");
if(Overlay.size == 0)run("From ROI Manager");
else run("Remove Overlay");
}
function segmentObjects(id)
{
// segment individual muscle fibers and return ROIset
selectImage(id);
// pre-processing
if(enhance_radius>0)run("Enhance Local Contrast (CLAHE)", "blocksize="+enhance_radius+" histogram=256 maximum=2 mask=*None* fast_(less_accurate)");
if(bg_radius>0)run("Subtract Background...", "rolling="+bg_radius);
// object enhancement
if(enhancer == "gauss")
{
run("Duplicate...","title=Enhanced");
enh_id = getImageID;
selectImage(enh_id);
run("Gaussian Blur...", "sigma="+gauss_scale);
run("Invert");
}
else if(enhancer == "laplace")
{
run("FeatureJ Derivatives", "x-order=0 y-order=2 z-order=0 smoothing="+gauss_scale);
enh_id = getImageID;
selectImage(enh_id);
rename("Enhanced");
}
// binarization and object detection
selectImage(enh_id);
setAutoThreshold(""+threshold_method);
setOption("BlackBackground", false);
run("Convert to Mask");
run("Analyze Particles...", "size="+min_size+"-"+max_size+"pixel show=Nothing clear include add");
selectImage(enh_id); close;
obj_nr = roiManager("count");
return(obj_nr);
}
function analyzeObjects(id,obj_nr)
{
// create binary image of objects
selectImage(id);
getDimensions(im_width, im_height, channels, slices, frames);
newImage("Objects", "8-bit black", im_width, im_height, 1);
obj_id = getImageID;
selectImage(obj_id);
roiManager("Deselect");
roiManager("Fill");
setThreshold(1,255);
setOption("BlackBackground", false);
run("Convert to Mask");
// elliptic fourier descriptors (limited version to detect deviation from very modest approximation, i.e. using 5 descriptors)
if(efd)
{
run("Set Measurements...", " perimeter redirect=None decimal=4");
efd_sum = newArray(obj_nr);
efd_perim = newArray(obj_nr);
run("Clear Results");
selectImage(obj_id);
for(i = 0; i < obj_nr; i++)
{
roiManager("select",i);
//run("Elliptic Fourier D.", "number=10 results");
run("Elliptic Fourier D.", "number=5 reconstruction results");
IJ.renameResults("Results-EFD-Objects","Results");
result_nr = nResults;
for(v = 2; v < result_nr; v++) // first two coeffs can be ignored
{
efd_sum[i] += getResult("efd",v);
}
run("Clear Results");
selectWindow("Objects-EFD");
run("Measure");
close;
efd_perim[i] = getResult("Perim.",0);
selectWindow("Results");
run("Close");
selectImage(obj_id);
run("Select None");
}
}
// measure local thickness
if(thickness)
{
run("Set Measurements...", "area standard mean redirect=None decimal=4");
av_thickness = newArray(obj_nr); // average thickness
sd_thickness = newArray(obj_nr); // standard deviation
cv_thickness = newArray(obj_nr); // coeff. of variation
run("Clear Results");
selectImage(obj_id);
run("Local Thickness (complete process)", "threshold=1");
selectWindow("Objects_LocThk");
thick_id = getImageID;
selectImage(thick_id);
roiManager("Deselect");
roiManager("Measure");
for(i = 0; i < obj_nr; i++)
{
av_thickness[i] = getResult("Mean",i);
sd_thickness[i] = getResult("StdDev",i);
cv_thickness[i] = getResult("StdDev",i)/getResult("Mean",i);
}
selectImage(thick_id); close;
run("Clear Results"); run("Collect Garbage");
}
// analyze skeleton
if(skeleton)
{
skeleton_area = newArray(obj_nr); // skeleton length
skeleton_mean = newArray(obj_nr); // mean intensity
branch_point_nr = newArray(obj_nr); // nr of branch points
skeleton_feret = newArray(obj_nr); // skeleton feret diameter
straightness = newArray(obj_nr); // straightness of the skeleton (1 is a straight line)
selectImage(obj_id);
run("Select None");
run("Skeletonize");
run("Analyze Skeleton (2D/3D)", "prune=none");
tag_id = getImageID;
selectImage(tag_id);
run("Macro...", "code=if(v!=70)v=0"); // 70 = branch points
run("Clear Results");
run("Set Measurements...", "area mean feret's redirect=[Tagged skeleton] decimal=4");
selectImage(obj_id);
run("Analyze Particles...", "size=3-Inf pixel show=Nothing display clear include");
for(i = 0; i < obj_nr; i++)
{
skeleton_area[i] = getResult("Area",i);
branch_point_nr[i] = getResult("Mean",i)*getResult("Area",i)/70;
skeleton_feret[i] = getResult("Feret",i);
straightness[i] = getResult("Feret",i)/getResult("Area",i);
}
selectImage(tag_id); close;
run("Clear Results"); run("Collect Garbage");
}
// detect bending points (corners)
if(corner)
{
bending_energy = newArray(obj_nr);
corner_nr = newArray(obj_nr);
run("Set Measurements...", "mean integrated redirect=None decimal=4");
selectImage(obj_id); // now the objects have been skeletonized
run("FeatureJ Structure", "smallest smoothing=1.0 integration=1");
struct_id = getImageID;
selectImage(struct_id);
roiManager("Deselect");
roiManager("Measure");
for(i = 0; i < obj_nr; i++)
{
bending_energy[i] = getResult("RawIntDen",i);
}
run("Clear Results");
selectImage(struct_id);
run("Find Maxima...", "noise=50 output=[Single Points]");
selectWindow("Objects smallest structure eigenvalues Maxima");
max_id = getImageID;
selectImage(struct_id); close;
selectImage(max_id);
roiManager("Deselect");
roiManager("Measure");
for(i = 0; i < obj_nr; i++)
{
corner_nr[i] = getResult("RawIntDen",i)/255;
}
selectImage(max_id); close;
run("Clear Results"); run("Collect Garbage");
}
// measure general object parameters
run("Set Measurements...", "area mean integrated standard fit perimeter shape feret's redirect=None decimal=4");
roiManager("Deselect");
selectImage(id);
roiManager("Measure");
// measure objects and complete the results table
for(i = 0; i < obj_nr; i++)
{
if(efd)
{
setResult("EFD_Sum",i,efd_sum[i]);
setResult("Curvature",i,efd_perim[i]/getResult("Perim.",i));
}
if(thickness)
{
setResult("Mean_Thickness",i,av_thickness[i]);
setResult("Std_Thickness",i,sd_thickness[i]);
setResult("CoV_Thickness",i,cv_thickness[i]);
}
if(skeleton)
{
setResult("Skeleton_Length",i,skeleton_area[i]);
setResult("Skeleton_Mean",i,skeleton_mean[i]);
setResult("Branch_Point_Nr",i,branch_point_nr[i]);
setResult("Skeleton_Feret",i,skeleton_feret[i]);
setResult("Straightness",i,straightness[i]);
}
if(corner)
{
setResult("Bending_Energy",i,bending_energy[i]);
setResult("Bend_Point_Nr",i,corner_nr[i]);
}
}
updateResults;
IJ.renameResults("Results","Object_Results");
selectImage(obj_id); close;
}