Digital Signal Processing Reference
In-Depth Information
D.2.7 File ContourTracer.java
D Source Code
1 import ij.process.ImageProcessor;
2
3 public class ContourTracer {
4
static final byte FOREGROUND = 1;
static final byte BACKGROUND = 0;
5
6
7
ImageProcessor ip;
byte[][] pixelMap;
8
int[][] labelMap;
9
// label values in labelMap can be:
10
// 0 ... unlabeled
11
// − 1 ... previously visited background pixel
12
// > 0 ... valid region label
13
14
15
public ContourTracer (ImageProcessor ip) {
this.ip = ip;
16
int h = ip.getHeight();
17
int w = ip.getWidth();
18
pixelMap
= new byte[h+2][w+2];
19
labelMap = new int[h+2][w+2];
20
21
22
// create auxil. arrays
for (int v = 0; v < h+2; v++) {
23
for (int u = 0; u < w+2; u++) {
24
if (ip.getPixel(u-1,v-1) == 0)
25
pixelMap[v][u] = BACKGROUND;
26
else
27
pixelMap[v][u] = FOREGROUND;
28
}
29
}
30
}
31
32
33
OuterContour traceOuterContour (int cx, int cy, int label) {
OuterContour cont = new OuterContour(label, 50);
34
traceContour(cx, cy, label, 0, cont);
35
return cont;
36
}
37
38
39
InnerContour traceInnerContour(int cx, int cy, int label) {
InnerContour cont = new InnerContour(label, 50);
40
traceContour(cx, cy, label, 1, cont);
41
return cont;
42
}
43
44
45
// trace one contour starting at xS , yS in direction dir
Contour traceContour (int xS, int yS, int label, int dir,
Contour cont) {
46
int xT, yT; // T = successor of starting point S
47
int xP, yP; // P = previous“ contour point
48
Search WWH ::




Custom Search