9 static final int UP=0, DOWN=1, UP_OR_DOWN=2, LEFT=3, RIGHT=4, LEFT_OR_RIGHT=5, NA=6;
13 private int maxPoints = 1000;
15 public int[]
xpoints =
new int[maxPoints];
17 public int[]
ypoints =
new int[maxPoints];
20 private byte[] bpixels;
21 private int[] cpixels;
22 private short[] spixels;
23 private float[] fpixels;
24 private int width, height;
25 private float lowerThreshold, upperThreshold;
29 if (ip instanceof ByteProcessor)
33 else if (ip instanceof ShortProcessor)
35 else if (ip instanceof FloatProcessor)
41 private float getColorPixel(
int x,
int y) {
42 if (x>=0 && x<width && y>=0 && y<height)
43 return cpixels[y*width + x];
45 return Float.MAX_VALUE;
48 private float getBytePixel(
int x,
int y) {
49 if (x>=0 && x<width && y>=0 && y<height)
50 return bpixels[y*width + x] & 0xff;
52 return Float.MAX_VALUE;
55 private float getShortPixel(
int x,
int y) {
56 if (x>=0 && x<width && y>=0 && y<height)
57 return spixels[y*width + x] & 0xffff;
59 return Float.MAX_VALUE;
62 private float getFloatPixel(
int x,
int y) {
63 if (x>=0 && x<width && y>=0 && y<height)
64 return fpixels[y*width + x];
66 return Float.MAX_VALUE;
69 private float getPixel(
int x,
int y) {
71 return getBytePixel(x,y);
72 else if (spixels!=null)
73 return getShortPixel(x,y);
74 else if (fpixels!=null)
75 return getFloatPixel(x,y);
77 return getColorPixel(x,y);
80 private boolean inside(
int x,
int y) {
83 value = getBytePixel(x,y);
84 else if (spixels!=null)
85 value = getShortPixel(x,y);
86 else if (fpixels!=null)
87 value = getFloatPixel(x,y);
89 value = getColorPixel(x,y);
90 return value>=lowerThreshold && value<=upperThreshold;
94 boolean isLine(
int xs,
int ys) {
98 if (xmax>=width) xmax=width-1;
102 if (ymax>=height) ymax=height-1;
105 for (
int x=xmin; (x<=xmax); x++)
106 for (
int y=ymin; y<=ymax; y++) {
112 IJ.log((((
double)insideCount)/area>=0.75?
"line ":
"blob ")+insideCount+
" "+area+
" "+IJ.d2s(((
double)insideCount)/area));
113 return ((
double)insideCount)/area>=0.75;
125 lowerThreshold = upperThreshold = getPixel(startX, startY);
126 do {x++;}
while (inside(x,y));
128 lowerThreshold = upperThreshold = getPixel(x, y);
131 if (!inside(x-1,y-1))
133 else if (inside(x,y-1))
138 traceEdge(x, y, direction);
143 public void autoOutline(
int startX,
int startY,
double lower,
double upper) {
148 lowerThreshold = (float)lower;
149 upperThreshold = (float)upper;
151 do {x++;}
while (inside(x,y));
152 if (!inside(x-1,y-1))
154 else if (inside(x,y-1))
159 do {x++;}
while (!inside(x,y) && x<width);
161 if (x>=width)
return;
163 traceEdge(x, y, direction);
167 public void autoOutline(
int startX,
int startY,
int lower,
int upper) {
168 autoOutline(startX, startY, (
double)lower, (
double)upper);
171 void traceEdge(
int xstart,
int ystart,
int startingDirection) {
195 int direction = startingDirection;
197 boolean UL = inside(x-1, y-1);
198 boolean UR = inside(x, y-1);
199 boolean LL = inside(x-1, y);
200 boolean LR = inside(x, y);
212 newDirection = table[index];
213 if (newDirection==UP_OR_DOWN) {
214 if (direction==RIGHT)
219 if (newDirection==LEFT_OR_RIGHT) {
223 newDirection = RIGHT;
225 if (newDirection!=direction) {
230 int[] xtemp =
new int[maxPoints*2];
231 int[] ytemp =
new int[maxPoints*2];
232 System.arraycopy(
xpoints, 0, xtemp, 0, maxPoints);
233 System.arraycopy(
ypoints, 0, ytemp, 0, maxPoints);
240 switch (newDirection) {
245 UL = inside(x-1, y-1);
259 UL = inside(x-1, y-1);
270 direction = newDirection;
271 }
while ((x!=xstart || y!=ystart || direction!=startingDirection));