-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNFN_count.ijm
executable file
·142 lines (114 loc) · 4.6 KB
/
NFN_count.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
/*
* Function to count the number of first nightbors. Particles on the edge are
* counted as first neighbors, but excluded from the count.
*
* INPUT: cell area segments binary image.
* OUTPUT: cell area segments with number of first neighbors highligted and
* array of first neighbors of each cell.
*
* Acknowledgements to Jan Brocher, the Biovoxxel Toolbox;
* for providing the code.
*
*/
macro "NFN_count" {
//function NFN_count() {
//Check if the cell area segment image is open.
if(nImages == 0) {
runMacro("ETT/warnings.ijm", 1);
// Check if the image is binary.
} else if(!is("binary")) {
runMacro("ETT/warnings.ijm", 2);
} else {
imgName = File.nameWithoutExtension;
//imgName = replace(getTitle(), ".tif", "");
id = getImageID();
// Generate cell positions table
run("Set Measurements...", "area centroid center perimeter bounding shape feret's fit nan redirect=None decimal=2");
run("Particles8 ", "white morphology show=Particles minimum=0 maximum=9999999 display overwrite redirect=None");
//run("Analyze Particles...", " show=Masks display clear include record in_situ");
// Variables
initialParticles = nResults; // numbr. of cells in the image
XStart = newArray(initialParticles); // cell top-left pixel
YStart = newArray(initialParticles);
cellID = newArray(initialParticles); // cell ID
XMass = newArray(initialParticles); // center of mass
YMass = newArray(initialParticles);
Area = newArray(initialParticles); // cell area projection
neighborArray = newArray(initialParticles);
neighbors = 0;
mostNeighbors = 0; // to count
/*
// get cell area projection metrics
Table.sort("YStart"); // so it matches with the final results
Major = newArray(nResults); // Major axis
Minor = newArray(nResults); // Minor axis
AR = newArray(nResults); // Aspect Ratio
Circ = newArray(nResults); // Circularity
Angle = newArray(nResults); // Angle betweem Major and X axis
Feret = newArray(nResults); // Feret diameter
FeretAngle = newArray(nResults); // Angle between Feret diameter and X axis
NAR = newArray(nResults); // Normalized aspect ratio
for(l = 0; l < nResults; l++){
Major[l] = getResult("Major", l);
Minor[l] = getResult("Minor", l);
AR[l] = getResult("AR", l);
Circ[l] = getResult("Circ.", l);
Angle[l] = getResult("Angle", l);
Feret[l] = getResult("Feret", l);
FeretAngle[l] = getResult("FeretAngle", l);
// Calculate the normalized aspect ratio (NAR)
NAR[l] = ((Major[l]/2) - (Minor[l]/2)) / ((Major[l]/2) + (Minor[l]/2));
}
*/
// Get cell coordinates
for(l = 0; l < initialParticles; l ++) {
XStart[l] = getResult("XStart", l);
YStart[l] = getResult("YStart", l);
toUnscaled(XStart[l], YStart[l]); // to get results without scale
XMass[l] = getResult("XM", l);
YMass[l] = getResult("YM", l);
toUnscaled(XMass[l], YMass[l]);
Area[l] = getResult("Area", l);
cellID[l] = l + 1;
}
// Set measurements
run("Set Measurements...", " redirect=None decimal=4");
// Count neighbors
for(i = 0; i < initialParticles; i ++) {
doWand(XStart[i],YStart[i], 0, "8-connected");
run("Enlarge...", "enlarge=2");
run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=Nothing display clear record");
neighbors = nResults-1;
neighborArray[i] = neighbors;
}
// Select cells to be marked (cells at the image edges are excluded)
selectImage(id);
run("Select None");
run("Duplicate...", "title=[" + imgName + "_NFN]");
run("Set Measurements...", "area centroid center nan redirect=None decimal=2");
run("Analyze Particles...", " show=Masks exclude clear include record in_situ");
display = getTitle();
run("Create Selection");
run("Make Inverse");
// Excluding cells at the edges
markedNeighborArray = newArray(initialParticles); // numbr. of first neighbors
Array.fill(markedNeighborArray, 0);
// Fill cells with color according to number of first neighbors
for(mark = 0; mark < initialParticles; mark ++) {
// The selection will tell the marked cells (non-edges)
if (selectionContains(XStart[mark], YStart[mark])) {
markValue = neighborArray[mark];
setForegroundColor(markValue, markValue, markValue);
floodFill(XStart[mark],YStart[mark], "8-connected");
// to count the selected (edge-excluded) particles
markedNeighborArray[mark] = markValue;
}
}
run("Select None");
run("glasbey");
//saveAs("tiff", imgName + "_NFNcount.tif");
// to show all particles count.
Array.show(imgName + "_NFN", cellID, Area, XStart, YStart, neighborArray, markedNeighborArray);
//Major, Minor, AR, Circ, Angle, Feret, FeretAngle, NAR);
}
}