Search in sources :

Example 6 with PntInt

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

the class EdgeTrace method main.

// ---------------------------------------------------------
public static void main(String[] args) {
    EdgeTrace trace = new EdgeTrace();
    trace.addPoint(PntInt.from(1, 2));
    trace.addPoint(PntInt.from(3, 4));
    trace.addPoint(PntInt.from(5, 6));
    for (Pnt2d p : trace) {
        System.out.println(p);
    }
    System.out.println();
    trace.addPoint(PntInt.from(7, 8));
    for (PntInt p : trace.getPointArray()) {
        System.out.println(p);
    }
    System.out.println();
    for (Point p : trace.getAwtPoints()) {
        System.out.println(p);
    }
    System.out.println(trace.getPoint(3));
    System.out.println(trace.getPoint(4));
    PntInt[] copy = Arrays.copyOfRange(trace.getPointArray(), 0, 7);
    System.out.println(copy.length);
    for (PntInt p : copy) {
        System.out.println(p);
    }
}
Also used : Pnt2d(imagingbook.pub.geometry.basic.Pnt2d) Point(java.awt.Point) PntInt(imagingbook.pub.geometry.basic.Pnt2d.PntInt)

Example 7 with PntInt

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

the class CannyEdgeDetector method traceAndThreshold.

/**
 * Recursively collect and mark all pixels of an edge that are 8-connected to
 * (u0,v0) and have a gradient magnitude above loThr.
 * @param u0 start coordinate (x)
 * @param v0 start coordinate (y)
 * @param loThr low threshold (min. edge magnitude to continue tracing)
 * @param markVal value used for marking pixels on an edge trace
 * @return a list of Point objects.
 */
private EdgeTrace traceAndThreshold(int u0, int v0, float loThr, int markVal) {
    // IJ.log("  working point " + u + " " + v);
    Stack<PntInt> pointStack = new Stack<>();
    EdgeTrace trace = new EdgeTrace();
    pointStack.push(PntInt.from(u0, v0));
    while (!pointStack.isEmpty()) {
        PntInt p = pointStack.pop();
        int up = p.x;
        int vp = p.y;
        // mark this edge point
        Ebin.putPixel(up, vp, markVal);
        trace.addPoint(p);
        // (up > 0) ? up-1 : 0;
        int uL = Math.max(up - 1, 0);
        // (up < M-1) ? up+1 : M-1;
        int uR = Math.min(up + 1, M - 1);
        // (vp > 0) ? vp - 1 : 0;
        int vT = Math.max(vp - 1, 0);
        // (vp < N-1) ? vp+1 : N-1;
        int vB = Math.min(vp + 1, N - 1);
        for (int u = uL; u <= uR; u++) {
            for (int v = vT; v <= vB; v++) {
                if (Ebin.get(u, v) == 0 && Enms.getf(u, v) >= loThr) {
                    pointStack.push(PntInt.from(u, v));
                }
            }
        }
    }
    return trace;
}
Also used : PntInt(imagingbook.pub.geometry.basic.Pnt2d.PntInt) Point(java.awt.Point) Stack(java.util.Stack)

Example 8 with PntInt

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

the class Pnt2dDistanceTest method testL1DistanceInt.

@Test
public void testL1DistanceInt() {
    PntInt p1 = PntInt.from(3, 8);
    PntInt p2 = PntInt.from(-2, 7);
    int dist = 6;
    Assert.assertEquals(dist, p1.distL1(p2));
    Assert.assertEquals(dist, p2.distL1(p1));
}
Also used : PntInt(imagingbook.pub.geometry.basic.Pnt2d.PntInt) Test(org.junit.Test)

Example 9 with PntInt

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

the class SegmentationRegionContour method applySegmentation.

@Override
protected boolean applySegmentation() {
    for (int v = 0; v < height; v++) {
        // scan top to bottom, left to right
        // reset label, scan through horiz. line:
        int label = 0;
        for (int u = 0; u < width; u++) {
            if (ip.getPixel(u, v) > 0) {
                // hit an unlabeled FOREGROUND pixel
                if (label != 0) {
                    // keep using the same label
                    setLabel(u, v, label);
                } else {
                    // label == 0
                    label = getLabel(u, v);
                    if (label == 0) {
                        // new (unlabeled) region is hit
                        // assign a new region label
                        label = getNextLabel();
                        PntInt xs = PntInt.from(u, v);
                        int ds = 0;
                        Contour.Outer c = traceContour(xs, ds, new Contour.Outer(label));
                        outerContours.add(c);
                        setLabel(u, v, label);
                    }
                }
            } else {
                // hit a BACKGROUND pixel
                if (label != 0) {
                    // exiting a region
                    if (getLabel(u, v) == BACKGROUND) {
                        // unlabeled - new inner contour
                        PntInt xs = PntInt.from(u - 1, v);
                        int ds = (NT == N4) ? 2 : 1;
                        Contour.Inner c = traceContour(xs, ds, new Contour.Inner(label));
                        innerContours.add(c);
                    }
                    label = 0;
                }
            }
        }
    }
    return true;
}
Also used : PntInt(imagingbook.pub.geometry.basic.Pnt2d.PntInt)

Example 10 with PntInt

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

the class RegionContourSegmentation method applySegmentation.

@Override
protected boolean applySegmentation() {
    for (int v = 0; v < height; v++) {
        // scan top to bottom, left to right
        // reset label, scan through horiz. line:
        int label = 0;
        for (int u = 0; u < width; u++) {
            if (ip.getPixel(u, v) > 0) {
                // hit an unlabeled FOREGROUND pixel
                if (label != 0) {
                    // keep using the same label
                    setLabel(u, v, label);
                } else {
                    // label == 0
                    label = getLabel(u, v);
                    if (label == 0) {
                        // new (unlabeled) region is hit
                        // assign a new region label
                        label = getNextLabel();
                        PntInt xs = PntInt.from(u, v);
                        int ds = 0;
                        Contour.Outer c = traceContour(xs, ds, new Contour.Outer(label));
                        outerContours.add(c);
                        setLabel(u, v, label);
                    }
                }
            } else {
                // hit a BACKGROUND pixel
                if (label != 0) {
                    // exiting a region
                    if (getLabel(u, v) == BACKGROUND) {
                        // unlabeled - new inner contour
                        PntInt xs = PntInt.from(u - 1, v);
                        int ds = (NT == N4) ? 2 : 1;
                        Contour.Inner c = traceContour(xs, ds, new Contour.Inner(label));
                        innerContours.add(c);
                    }
                    label = 0;
                }
            }
        }
    }
    return true;
}
Also used : Contour(imagingbook.pub.regions.Contour) 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