Search in sources :

Example 11 with PntInt

use of imagingbook.pub.geometry.basic.Pnt2d.PntInt in project imagingbook-common by imagingbook.

the class RegionContourSegmentation method traceContour.

// Trace one contour starting at Xs in direction ds
private <T extends Contour> T traceContour(PntInt xs, final int ds, T C) {
    // C: empty inner or outer contour
    final int label = C.getLabel();
    Tuple2<PntInt, Integer> next = findNextContourPoint(xs, ds);
    PntInt x = next.get0();
    int d = next.get1();
    C.addPoint(x);
    final PntInt xt = x;
    boolean home = xs.equals(xt);
    while (!home) {
        setLabel(x, label);
        PntInt xp = x;
        int dn = (d + 6) % 8;
        next = findNextContourPoint(x, dn);
        x = next.get0();
        d = next.get1();
        // are we back at the starting position?
        // back at start pos.
        home = (xp.equals(xs) && x.equals(xt));
        if (!home) {
            C.addPoint(x);
        }
    }
    return C;
}
Also used : PntInt(imagingbook.pub.geometry.basic.Pnt2d.PntInt)

Example 12 with PntInt

use of imagingbook.pub.geometry.basic.Pnt2d.PntInt in project imagingbook-common by imagingbook.

the class RegionContourSegmentation method findNextContourPoint.

// --------------------------------------------------------------------
// Starts at point X0 in direction d0, returns a tuple holding
// the next point and the direction in which it was found if successful.
// Returns null if no successor point is found.
private Tuple2<PntInt, Integer> findNextContourPoint(final PntInt x0, final int d0) {
    final int step = (NT == N4) ? 2 : 1;
    PntInt x = null;
    int d = d0;
    int i = 0;
    boolean done = false;
    while (i < 7 && !done) {
        // N4: i = 0,2,4,6  N8: i = 0,1,2,3,4,5,6
        x = x0.plus(delta[d][0], delta[d][1]);
        if (ip.getPixel(x.x, x.y) == BACKGROUND) {
            // mark this background pixel not to be visited again
            setLabel(x, VISITED);
            d = (d + step) % 8;
        } else {
            // found a non-background pixel (next pixel to follow)
            done = true;
        }
        i = i + step;
    }
    return (done) ? Tuple2.from(x, d) : // no successor found
    Tuple2.from(x0, d0);
}
Also used : PntInt(imagingbook.pub.geometry.basic.Pnt2d.PntInt)

Example 13 with PntInt

use of imagingbook.pub.geometry.basic.Pnt2d.PntInt in project imagingbook-common by imagingbook.

the class SegmentationDepthFirst method floodFill.

private void floodFill(int u, int v, int label) {
    // stack contains pixel coordinates
    Deque<PntInt> S = new LinkedList<>();
    S.push(PntInt.from(u, v));
    while (!S.isEmpty()) {
        PntInt p = S.pop();
        int x = p.x;
        int y = p.y;
        if ((x >= 0) && (x < width) && (y >= 0) && (y < height) && getLabel(x, y) == FOREGROUND) {
            setLabel(x, y, label);
            S.push(PntInt.from(x + 1, y));
            S.push(PntInt.from(x, y + 1));
            S.push(PntInt.from(x, y - 1));
            S.push(PntInt.from(x - 1, y));
            if (NT == NeighborhoodType2D.N8) {
                S.push(PntInt.from(x + 1, y + 1));
                S.push(PntInt.from(x - 1, y + 1));
                S.push(PntInt.from(x + 1, y - 1));
                S.push(PntInt.from(x - 1, y - 1));
            }
        }
    }
}
Also used : PntInt(imagingbook.pub.geometry.basic.Pnt2d.PntInt) LinkedList(java.util.LinkedList)

Example 14 with PntInt

use of imagingbook.pub.geometry.basic.Pnt2d.PntInt in project imagingbook-common by imagingbook.

the class SegmentationRegionContour method traceContour.

// Trace one contour starting at Xs in direction ds
private <T extends Contour> T traceContour(PntInt xs, final int ds, T C) {
    // C: empty inner or outer contour
    final int label = C.getLabel();
    Tuple2<PntInt, Integer> next = findNextContourPoint(xs, ds);
    PntInt x = next.get0();
    int d = next.get1();
    C.addPoint(x);
    final PntInt xt = x;
    boolean home = xs.equals(xt);
    while (!home) {
        setLabel(x, label);
        PntInt xp = x;
        int dn = (d + 6) % 8;
        next = findNextContourPoint(x, dn);
        x = next.get0();
        d = next.get1();
        // are we back at the starting position?
        // back at start pos.
        home = (xp.equals(xs) && x.equals(xt));
        if (!home) {
            C.addPoint(x);
        }
    }
    return C;
}
Also used : PntInt(imagingbook.pub.geometry.basic.Pnt2d.PntInt)

Example 15 with PntInt

use of imagingbook.pub.geometry.basic.Pnt2d.PntInt in project imagingbook-common by imagingbook.

the class BinaryThinning method thinOnce.

// ----------------------------------------------------------------------------
// Single thinning iteration. Returns the number of deletions performed (for debugging only).
public int thinOnce(ByteProcessor ip) {
    final int M = ip.getWidth();
    final int N = ip.getHeight();
    final List<PntInt> D = new ArrayList<>();
    final byte[] NH = new byte[8];
    int n = 0;
    for (int pass = 0; pass < 2; pass++) {
        // make 2 passes
        D.clear();
        for (int u = 0; u < M; u++) {
            for (int v = 0; v < N; v++) {
                if (ip.get(u, v) > 0) {
                    // get8Neighborhood(ip, u, v, NH);
                    // int c = get8NeighborhoodIndex(NH);
                    int c = get8NeighborhoodIndex(ip, u, v);
                    if (Q[c][pass]) {
                        D.add(PntInt.from(u, v));
                        n = n + 1;
                    }
                }
            }
        }
        for (PntInt p : D) {
            ip.putPixel(p.x, p.y, 0);
        }
    }
    return n;
}
Also used : ArrayList(java.util.ArrayList) PntInt(imagingbook.pub.geometry.basic.Pnt2d.PntInt)

Aggregations

PntInt (imagingbook.pub.geometry.basic.Pnt2d.PntInt)15 LinkedList (java.util.LinkedList)4 Point (java.awt.Point)2 Pnt2d (imagingbook.pub.geometry.basic.Pnt2d)1 Contour (imagingbook.pub.regions.Contour)1 ArrayList (java.util.ArrayList)1 Stack (java.util.Stack)1 Test (org.junit.Test)1