-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab_conti.ijm
168 lines (154 loc) · 4.8 KB
/
lab_conti.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
//Macro by Félix de Carpentier, 2020, CNRS / Sorbonne University / Paris-Saclay University, France
//Inspired by Will Armour, 2018 (https://willarmour.science/how-to-automate-image-particle-analysis-by-creating-a-macro-in-imagej/)
//Choose directories and create a list of files
inputFolder=getDirectory("Choose input folder");
outputFolder=getDirectory("Choose output folder for the results");
list=getFileList(inputFolder);
//Create an option dialog box
Dialog.create("Options");
Dialog.addNumber("Distance in pixels", 2.85);
Dialog.addNumber("Known distance", 1);
Dialog.addNumber("Minimum area (unit"+fromCharCode(0x00B2)+")", 10);
Dialog.addCheckbox("Activate Watershed", false);
Dialog.addCheckbox("White Balance", true);
Dialog.show();
disPix = Dialog.getNumber();
disKnown = Dialog.getNumber();
minArea = Dialog.getNumber();
watershed = Dialog.getCheckbox();
whiteBalance = Dialog.getCheckbox();
//Set method, watershed and white balance labels for the output names
method="_conti"; //Used method
watershedLabel = "";
if(watershed!=false) watershedLabel="_ws";
whiteBalancelabel ="";
if(whiteBalance!=false) whiteBalancelabel="_wb";
//Choose what you need to measure
run("Set Measurements...", "area mean min redirect=None decimal=4");
run("Clear Results");
//Processing loop of the images
for(i=0; i<list.length; i++)
{
//Open the images
imgPath=inputFolder+list[i];
open(imgPath);
//Setup output path
outputPath=outputFolder+list[i];
fileExtension=lastIndexOf(outputPath,".");
if(fileExtension!=-1) outputPath=substring(outputPath,0,fileExtension);
//Set scale according to user inputs
run("Set Scale...", "distance="+disPix+" known="+disKnown+" unit=µm");
if(nImages>=1)
{
currentNResults = nResults; //Save the number of the last result
if(whiteBalance!=false) autoWhite(); //Ajust automatically the white balance
getRoi(); //Add all the particles to the ROI manager
selectWindow(whiteBalancelabel+list[i]);
getB(); //Creates a grey image with LAB-b* channel (green particles)
getMes(); //Measure the values of all particles and creates green/red overlay
//This add the file name in a row
for (row = currentNResults; row < nResults; row++)
{
setResult("Image", row, list[i]);
}
//Transfer the ROI overlay to the original image and save
selectWindow(whiteBalancelabel+list[i]);
roiManager("Show All without labels");
run("Flatten");
saveAs("Jpeg", outputPath+method+whiteBalancelabel+watershedLabel+".jpg");
roiManager("Delete"); //Clear the ROI manager
close("*"); //Close all images
}
showProgress(i, list.length); //Shows a progress bar
}
saveAs("results", outputFolder+"results"+method+whiteBalancelabel+watershedLabel+ ".csv");
closeWin("Results"); closeWin("ROI Manager");
function getRoi()
{
run("Duplicate...", " ");
rename(whiteBalancelabel+list[i]+"_ori");
run("Duplicate...", " ");
run("8-bit");
run("Gaussian Blur...", "sigma=2"); //Blur the particles to select the objects and not the sub-objects
setAutoThreshold("Default");
run("Convert to Mask");
if(watershed!=false) run("Watershed");
run("Fill Holes");
run("Erode");
run("Dilate");
run("Analyze Particles...","size="+minArea+"-Infinity add");
}
function getB()
{
run("RGB to CIELAB");
for(i=0;i<2;i++)
{
run("Delete Slice");
}
}
function getMes()
{
roiManager("Measure");
roiManager("Deselect");
roiManager("Set Line Width", 3);
roiManager("Set Color", "#B8293B"); //Overlay for dead particles
for (iRow = 0; iRow < nResults-currentNResults; iRow++)
{
maxParticle=getResult("Max", iRow+currentNResults);
if (maxParticle>40)
{
roiManager("Select", iRow);
roiManager("Set Color", "#F5FB82"); //Overlay for living particles
}
}
roiManager("Show All without labels"); //transfer the ROI
run("Flatten");
saveAs("Jpeg", outputPath+method+whiteBalancelabel+watershedLabel+"_LAB_b.jpg");
}
function autoWhite()
{
// Original code by Vytas Bindokas; Oct 2006, Univ. of Chicago
// Code modified by Patrice Mascalchi, 2014, Univ. of Cambridge UK
run("Select None");
origBit = bitDepth;
if (bitDepth() != 24) exit("Active image is not RGB");
run("RGB Stack");
run("Restore Selection");
val = newArray(3);
for (s=1;s<=3;s++)
{
setSlice(s);
run("Measure");
val[s-1] = getResult("Mean");
Table.deleteRows(currentNResults, currentNResults);
}
run("Select None");
run("16-bit");
run("32-bit");
Array.getStatistics(val, min, max, mean);
for (s=1; s<=3; s++)
{
setSlice(s);
dR = val[s-1] - mean;
if (dR < 0)
{
run("Add...", "slice value="+ abs(dR));
} else if (dR > 0) {
run("Subtract...", "slice value="+ abs(dR));
}
}
run("16-bit");
run("Convert Stack to RGB");
rename(whiteBalancelabel+list[i]);
selectWindow(whiteBalancelabel+list[i]);
selectWindow(list[i]);
close();
}
function closeWin(winName)
{
if (isOpen(winName))
{
selectWindow(winName);
run("Close");
}
}