5 import ij.plugin.filter.*;
69 static final double HALFPI = 1.5707963267949;
98 private double xsum, ysum, x2sum, y2sum, xysum;
100 private int left, top, width, height;
102 private double xm, ym;
103 private double u20, u02, u11;
105 private double pw, ph;
106 private boolean record;
112 Rectangle r = ip.
getRoi();
122 void getEllipseParam() {
123 double sqrtPi = 1.772453851;
124 double a11, a12, a22, m4, z, scale, tmp, xoffset, yoffset;
128 major = (width*2) / sqrtPi;
129 minor = (height*2) / sqrtPi;
146 m4 = 4.0 * Math.abs(u02 * u20 - u11 * u11);
158 theta = 0.5 * Math.atan(2.0 * a12 / tmp);
163 else if (a12 == 0.0) {
169 }
else if (a11 != a22)
172 tmp = Math.sin(
theta);
175 z = a12 * Math.cos(
theta) / tmp;
176 major = Math.sqrt (1.0 / Math.abs(a22 + z));
177 minor = Math.sqrt (1.0 / Math.abs(a11 - z));
178 scale = Math.sqrt (bitCount / (Math.PI *
major *
minor));
180 minor = minor*scale*2.0;
189 xCenter = left + xoffset + 0.5;
193 void computeSums () {
202 for (
int y=0; y<height; y++) {
205 int offset = y*width;
206 for (
int x=0; x<width; x++) {
207 if (mask[offset+x] != 0) {
214 ysum += bitcountOfLine * y;
218 y2sum += ye*ye*bitcountOfLine;
219 bitCount += bitcountOfLine;
224 double x1, y1, x2, y2, xy;
229 x2sum += 0.08333333 * bitCount;
230 y2sum += 0.08333333 * bitCount;
239 u20 = x2 - (x1 * x1);
240 u02 = y2 - (y1 * y1);
269 if (
major==0.0 && minor==0.0)
271 int xc = (int)Math.round(
xCenter);
272 int yc = (int)Math.round(
yCenter);
275 double sint, cost, rmajor2, rminor2, g11, g12, g22, k1, k2, k3;
276 int x, xsave, ymin, ymax;
277 int[] txmin =
new int[maxY];
278 int[] txmax =
new int[maxY];
281 sint = Math.sin(
theta);
282 cost = Math.cos(
theta);
283 rmajor2 = 1.0 / sqr(
major/2);
284 rminor2 = 1.0 / sqr(minor/2);
285 g11 = rmajor2 * sqr(cost) + rminor2 * sqr(sint);
286 g12 = (rmajor2 - rminor2) * sint * cost;
287 g22 = rmajor2 * sqr(sint) + rminor2 * sqr(cost);
289 k2 = (sqr(g12) - g11 * g22) / sqr(g11);
291 ymax = (int)Math.floor(Math.sqrt(Math.abs(k3 / k2)));
298 for (
int y=0; y<=ymax; y++) {
300 j2 = Math.sqrt(k2 * sqr(y) + k3);
302 txmin[y] = (int)Math.round(j1 - j2);
303 txmax[y] = (int)Math.round(j1 + j2);
310 ip.
moveTo(xc + txmin[ymax - 1], yc + ymin);
311 for (
int y=ymin; y<ymax; y++) {
312 x = y<0?txmax[-y]:-txmin[y];
318 ip.
lineTo(xc + x, yc + y);
320 for (
int y=ymax; y>ymin; y--) {
321 x = y<0?txmin[-y]:-txmax[y];
327 ip.
lineTo(xc + x, yc + y);
343 private double sqr(
double x) {