-
Notifications
You must be signed in to change notification settings - Fork 49
/
MaxFeretDiameter3D.java
205 lines (180 loc) · 5.75 KB
/
MaxFeretDiameter3D.java
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
/**
*
*/
package inra.ijpb.measure.region3d;
import java.util.ArrayList;
import java.util.Map;
import ij.measure.Calibration;
import ij.measure.ResultsTable;
import ij.ImageStack;
import inra.ijpb.geometry.Point3D;
import inra.ijpb.geometry.PointPair3D;
/**
* Computes maximum Feret Diameter for each region of a 3D binary or label
* image.
*
* @author dlegland
*
*/
public class MaxFeretDiameter3D extends RegionAnalyzer3D<PointPair3D>
{
// ==================================================
// Static methods
public final static PointPair3D[] maxFeretDiameters(ImageStack image, int[] labels, Calibration calib)
{
return new MaxFeretDiameter3D().analyzeRegions(image, labels, calib);
}
/**
* Computes Maximum Feret diameter of a set of points.
*
* Note: it is often a good idea to compute convex hull before computing
* Feret diameter.
*
* @param points
* a collection of planar points
* @return the maximum Feret diameter of the point set
*/
public final static PointPair3D maxFeretDiameter(ArrayList<? extends Point3D> points)
{
double distMax = Double.NEGATIVE_INFINITY;
PointPair3D maxDiam = null;
for (Point3D p1 : points)
{
for (Point3D p2 : points)
{
double dist = p1.distance(p2);
if (dist > distMax)
{
maxDiam = new PointPair3D(p1, p2);
distMax = dist;
}
}
}
return maxDiam;
}
// ==================================================
// Constructor
/**
* Default constructor
*/
public MaxFeretDiameter3D()
{
}
// ==================================================
// Implementation of RegionAnalyzer interface
/**
* Converts the result of maximum Feret diameters computation to a
* ResultsTable that can be displayed within ImageJ.
*
* @param maxDiamsMap
* the map of PointPair3D for each label within a label image
* @return a ResultsTable instance
*/
public ResultsTable createTable(Map<Integer, PointPair3D> maxDiamsMap)
{
// Create data table
ResultsTable table = new ResultsTable();
// compute ellipse parameters for each region
for (int label : maxDiamsMap.keySet())
{
table.incrementCounter();
table.addLabel(Integer.toString(label));
// add coordinates of origin pixel (IJ coordinate system)
PointPair3D maxDiam = maxDiamsMap.get(label);
table.addValue("Diameter", maxDiam.diameter());
table.addValue("P1.x", maxDiam.p1.getX());
table.addValue("P1.y", maxDiam.p1.getY());
table.addValue("P1.z", maxDiam.p1.getZ());
table.addValue("P2.x", maxDiam.p2.getX());
table.addValue("p2.y", maxDiam.p2.getY());
table.addValue("p2.z", maxDiam.p2.getZ());
}
// return the created array
return table;
}
/**
* Computes maximum Feret Diameter for each label of the input label image.
*
* Computes diameter between corners of image pixels, so the result is
* always greater than or equal to one.
*
* @param image
* a label image (8, 16 or 32 bits)
* @param labels
* the set of labels within the image
* @param calib
* the spatial calibration of the image
* @return an array of PointPair3D representing the coordinates of extreme
* points, in calibrated coordinates.
*/
public PointPair3D[] analyzeRegions(ImageStack image, int[] labels, Calibration calib)
{
// Check validity of parameters
if (image == null)
return null;
// Extract spatial calibration of image
double sx = 1, sy = 1, sz = 1;
double ox = 0, oy = 0, oz = 0;
if (calib != null)
{
sx = calib.pixelWidth;
sy = calib.pixelHeight;
sz = calib.pixelDepth;
ox = calib.xOrigin;
oy = calib.yOrigin;
oz = calib.zOrigin;
}
int nLabels = labels.length;
// For each label, create a list of corner points
fireStatusChanged(this, "Find Label Corner Points");
ArrayList<Point3D>[] labelCornerPointsArray = RegionBoundaries3D.regionsCornersArray(image, labels);
// Compute the oriented box of each set of corner points
PointPair3D[] labelMaxDiams = new PointPair3D[nLabels];
fireStatusChanged(this, "Compute feret Diameters");
for (int i = 0; i < nLabels; i++)
{
this.fireProgressChanged(this, i, nLabels);
ArrayList<Point3D> hull = labelCornerPointsArray[i];
// calibrate coordinates of hull vertices
for (int iv = 0; iv < hull.size(); iv++)
{
Point3D vertex = hull.get(iv);
vertex = new Point3D(vertex.getX() * sx + ox, vertex.getY() * sy + oy, vertex.getZ() * sz + oz);
hull.set(iv, vertex);
}
// compute Feret diameter of calibrated hull
labelMaxDiams[i] = maxFeretDiameter(hull);
}
fireProgressChanged(this, 1, 1);
fireStatusChanged(this, "");
return labelMaxDiams;
}
// /**
// * Computes Maximum Feret diameter from a single region in a binary image.
// *
// * Computes diameter between corners of image pixels, so the result is
// * always greater than or equal to one.
// *
// * @param image
// * a binary image representing the particle.
// * @param calib
// * the spatial calibration
// * @return the maximum Feret diameter of the binary region
// */
// public PointPair3D analyzeBinary(ImageStack image, double[] calib)
// {
// ArrayList<Point2D> points = RegionBoundaries.binaryParticleCorners(image);
// ArrayList<Point2D> convHull = Polygons2D.convexHull_jarvis(points);
//
// // calibrate coordinates of convex hull vertices
// for (int i = 0; i < convHull.size(); i++)
// {
// Point2D vertex = convHull.get(i);
// vertex = new Point2D.Double(vertex.getX() * calib[0], vertex.getY() * calib[1]);
// convHull.set(i, vertex);
// }
//
// // compute Feret diameter of calibrated vertices
// return maxFeretDiameter(convHull);
// }
}