Search in sources :

Example 11 with PointIndex_I32

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

the class ExampleFitPolygon method fitCannyEdges.

/**
 * Fits a sequence of line-segments into a sequence of points found using the Canny edge detector. In this case
 * the points are not connected in a loop. The canny detector produces a more complex tree and the fitted
 * points can be a bit noisy compared to the others.
 */
public static void fitCannyEdges(GrayF32 input) {
    BufferedImage displayImage = new BufferedImage(input.width, input.height, BufferedImage.TYPE_INT_RGB);
    // Finds edges inside the image
    CannyEdge<GrayF32, GrayF32> canny = FactoryEdgeDetectors.canny(2, true, true, GrayF32.class, GrayF32.class);
    canny.process(input, 0.1f, 0.3f, null);
    List<EdgeContour> contours = canny.getContours();
    Graphics2D g2 = displayImage.createGraphics();
    g2.setStroke(new BasicStroke(2));
    // used to select colors for each line
    Random rand = new Random(234);
    for (EdgeContour e : contours) {
        g2.setColor(new Color(rand.nextInt()));
        for (EdgeSegment s : e.segments) {
            // fit line segments to the point sequence. Note that loop is false
            List<PointIndex_I32> vertexes = ShapeFittingOps.fitPolygon(s.points, false, minSide, cornerPenalty);
            VisualizeShapes.drawPolygon(vertexes, false, g2);
        }
    }
    gui.addImage(displayImage, "Canny Trace");
}
Also used : GrayF32(boofcv.struct.image.GrayF32) Random(java.util.Random) EdgeSegment(boofcv.alg.feature.detect.edge.EdgeSegment) PointIndex_I32(boofcv.struct.PointIndex_I32) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage) EdgeContour(boofcv.alg.feature.detect.edge.EdgeContour)

Example 12 with PointIndex_I32

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

the class ShapeFitContourApp method renderVisuals.

protected void renderVisuals(Graphics2D g2, double scale) {
    BoofSwingUtil.antialiasing(g2);
    int activeAlg = controlPanel.getSelectedAlgorithm();
    g2.setStroke(new BasicStroke(3));
    if (controlPanel.contoursVisible) {
        g2.setStroke(new BasicStroke(1));
        VisualizeBinaryData.render(contours, null, Color.CYAN, 1.0, scale, g2);
    }
    if (activeAlg == 0) {
        double cornerPalty = controlPanel.getCornerPenalty();
        int minimumSplitPixels = controlPanel.getMinimumSplitPixels();
        for (Contour c : contours) {
            List<PointIndex_I32> vertexes = ShapeFittingOps.fitPolygon(c.external, true, minimumSplitPixels, cornerPalty);
            g2.setColor(Color.RED);
            visualizePolygon(g2, scale, vertexes);
            for (List<Point2D_I32> internal : c.internal) {
                vertexes = ShapeFittingOps.fitPolygon(internal, true, minimumSplitPixels, cornerPalty);
                g2.setColor(Color.GREEN);
                visualizePolygon(g2, scale, vertexes);
            }
        }
    } else if (activeAlg == 1) {
        // Filter small contours since they can generate really wacky ellipses
        for (Contour c : contours) {
            if (c.external.size() > 10) {
                FitData<EllipseRotated_F64> ellipse = ShapeFittingOps.fitEllipse_I32(c.external, 0, false, null);
                g2.setColor(Color.RED);
                g2.setStroke(new BasicStroke(2.5f));
                VisualizeShapes.drawEllipse(ellipse.shape, scale, g2);
            }
            for (List<Point2D_I32> internal : c.internal) {
                if (internal.size() <= 10)
                    continue;
                FitData<EllipseRotated_F64> ellipse = ShapeFittingOps.fitEllipse_I32(internal, 0, false, null);
                g2.setColor(Color.GREEN);
                g2.setStroke(new BasicStroke(2.5f));
                VisualizeShapes.drawEllipse(ellipse.shape, scale, g2);
            }
        }
    }
}
Also used : Contour(boofcv.alg.filter.binary.Contour) PointIndex_I32(boofcv.struct.PointIndex_I32) Point2D_I32(georegression.struct.point.Point2D_I32) ArrayList(java.util.ArrayList) List(java.util.List) FitData(boofcv.alg.shapes.FitData)

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