Search in sources :

Example 1 with PointIndex_I32

use of boofcv.struct.PointIndex_I32 in project narchy by automenta.

the class WebcamObjectTrack method fitCannyBinary.

/**
 * Detects contours inside the binary image generated by canny.  Only the external contour is relevant. Often
 * easier to deal with than working with Canny edges directly.
 */
public static void fitCannyBinary(ImageFloat32 input, Graphics2D overlay) {
    BufferedImage displayImage = new BufferedImage(input.width, input.height, BufferedImage.TYPE_INT_RGB);
    ImageUInt8 binary = new ImageUInt8(input.width, input.height);
    final int blurRadius = 2;
    // Finds edges inside the image
    CannyEdge<ImageFloat32, ImageFloat32> canny = FactoryEdgeDetectors.canny(blurRadius, false, true, ImageFloat32.class, ImageFloat32.class);
    canny.process(input, 0.1f, 0.3f, binary);
    List<Contour> contours = BinaryImageOps.contour(binary, ConnectRule.EIGHT, null);
    overlay.setStroke(new BasicStroke(4));
    final int iterations = 80;
    for (Contour c : contours) {
        // Only the external contours are relevant.
        List<PointIndex_I32> vertexes = ShapeFittingOps.fitPolygon(c.external, true, toleranceDist, toleranceAngle, iterations);
        overlay.setColor(new Color(rand.nextInt()));
        VisualizeShapes.drawPolygon(vertexes, true, overlay);
    }
// ShowImages.showWindow(displayImage, "Canny Contour");
}
Also used : Contour(boofcv.alg.filter.binary.Contour) PointIndex_I32(boofcv.struct.PointIndex_I32) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.core.image.ConvertBufferedImage)

Example 2 with PointIndex_I32

use of boofcv.struct.PointIndex_I32 in project BoofCV by lessthanoptimal.

the class ShapeFitContourApp method visualizePolygon.

private void visualizePolygon(Graphics2D g2, double scale, List<PointIndex_I32> vertexes) {
    g2.setStroke(new BasicStroke(2));
    VisualizeShapes.drawPolygon(vertexes, true, scale, g2);
    if (controlPanel.isCornersVisible()) {
        g2.setColor(Color.BLUE);
        g2.setStroke(new BasicStroke(2f));
        for (PointIndex_I32 p : vertexes) {
            VisualizeFeatures.drawCircle(g2, scale * (p.x + 0.5), scale * (p.y + 0.5), 5);
        }
    }
}
Also used : PointIndex_I32(boofcv.struct.PointIndex_I32)

Example 3 with PointIndex_I32

use of boofcv.struct.PointIndex_I32 in project BoofCV by lessthanoptimal.

the class ShapeFittingOps method fitPolygon.

/**
 * <p>Fits a polygon to the provided sequence of connected points. The found polygon is returned as a list of
 * vertices. Each point in the original sequence is guaranteed to be within "toleranceDist' of a line segment.</p>
 *
 * <p>Internally a split-and-merge algorithm is used. See referenced classes for more information. Consider
 * using internal algorithms directly if this function is a performance bottleneck.</p>
 *
 * @param sequence Ordered and connected list of points.
 * @param loop If true the sequence is a connected at both ends, otherwise it is assumed to not be.
 * @param minimumSideLength The minimum allowed side length in pixels. Try 10
 * @param cornerPenalty How much a corner is penalized. Try 0.25
 * @return Vertexes in the fit polygon.
 * @see PolylineSplitMerge
 */
public static List<PointIndex_I32> fitPolygon(List<Point2D_I32> sequence, boolean loop, int minimumSideLength, double cornerPenalty) {
    PolylineSplitMerge alg = new PolylineSplitMerge();
    alg.setLoops(loop);
    alg.setMinimumSideLength(minimumSideLength);
    alg.setCornerScorePenalty(cornerPenalty);
    alg.process(sequence);
    PolylineSplitMerge.CandidatePolyline best = alg.getBestPolyline();
    DogArray<PointIndex_I32> output = new DogArray<>(PointIndex_I32::new);
    if (best != null) {
        indexToPointIndex(sequence, best.splits, output);
    }
    return new ArrayList<>(output.toList());
}
Also used : PointIndex_I32(boofcv.struct.PointIndex_I32) ArrayList(java.util.ArrayList) PolylineSplitMerge(boofcv.alg.shapes.polyline.splitmerge.PolylineSplitMerge) DogArray(org.ddogleg.struct.DogArray)

Example 4 with PointIndex_I32

use of boofcv.struct.PointIndex_I32 in project BoofCV by lessthanoptimal.

the class ExampleFitPolygon method fitCannyBinary.

/**
 * Detects contours inside the binary image generated by canny. Only the external contour is relevant. Often
 * easier to deal with than working with Canny edges directly.
 */
public static void fitCannyBinary(GrayF32 input) {
    BufferedImage displayImage = new BufferedImage(input.width, input.height, BufferedImage.TYPE_INT_RGB);
    GrayU8 binary = new GrayU8(input.width, input.height);
    // Finds edges inside the image
    CannyEdge<GrayF32, GrayF32> canny = FactoryEdgeDetectors.canny(2, false, true, GrayF32.class, GrayF32.class);
    canny.process(input, 0.1f, 0.3f, binary);
    // Only external contours are relevant
    List<Contour> contours = BinaryImageOps.contourExternal(binary, ConnectRule.EIGHT);
    Graphics2D g2 = displayImage.createGraphics();
    g2.setStroke(new BasicStroke(2));
    // used to select colors for each line
    Random rand = new Random(234);
    for (Contour c : contours) {
        List<PointIndex_I32> vertexes = ShapeFittingOps.fitPolygon(c.external, true, minSide, cornerPenalty);
        g2.setColor(new Color(rand.nextInt()));
        VisualizeShapes.drawPolygon(vertexes, true, g2);
    }
    gui.addImage(displayImage, "Canny Contour");
}
Also used : GrayF32(boofcv.struct.image.GrayF32) Random(java.util.Random) Contour(boofcv.alg.filter.binary.Contour) EdgeContour(boofcv.alg.feature.detect.edge.EdgeContour) PointIndex_I32(boofcv.struct.PointIndex_I32) GrayU8(boofcv.struct.image.GrayU8) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage)

Example 5 with PointIndex_I32

use of boofcv.struct.PointIndex_I32 in project BoofCV by lessthanoptimal.

the class ShapeFittingOps method indexToPointIndex.

/**
 * Converts the list of indexes in a sequence into a list of {@link PointIndex_I32}.
 * @param sequence Sequence of points.
 * @param indexes List of indexes in the sequence.
 * @param output Output list of {@link PointIndex_I32}.
 */
public static void indexToPointIndex(List<Point2D_I32> sequence, GrowQueue_I32 indexes, FastQueue<PointIndex_I32> output) {
    output.reset();
    for (int i = 0; i < indexes.size; i++) {
        int index = indexes.data[i];
        Point2D_I32 p = sequence.get(index);
        PointIndex_I32 o = output.grow();
        o.x = p.x;
        o.y = p.y;
        o.index = index;
    }
}
Also used : PointIndex_I32(boofcv.struct.PointIndex_I32) Point2D_I32(georegression.struct.point.Point2D_I32)

Aggregations

PointIndex_I32 (boofcv.struct.PointIndex_I32)12 Contour (boofcv.alg.filter.binary.Contour)5 BufferedImage (java.awt.image.BufferedImage)5 Point2D_I32 (georegression.struct.point.Point2D_I32)4 EdgeContour (boofcv.alg.feature.detect.edge.EdgeContour)3 ConvertBufferedImage (boofcv.io.image.ConvertBufferedImage)3 GrayU8 (boofcv.struct.image.GrayU8)3 GrayF32 (boofcv.struct.image.GrayF32)2 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 Term (nars.term.Term)2 EdgeSegment (boofcv.alg.feature.detect.edge.EdgeSegment)1 FitData (boofcv.alg.shapes.FitData)1 PolylineSplitMerge (boofcv.alg.shapes.polyline.splitmerge.PolylineSplitMerge)1 ConvertBufferedImage (boofcv.core.image.ConvertBufferedImage)1 Polygon2D_I32 (georegression.struct.shapes.Polygon2D_I32)1 Rectangle2D_I32 (georegression.struct.shapes.Rectangle2D_I32)1 List (java.util.List)1 DogArray (org.ddogleg.struct.DogArray)1