Search in sources :

Example 6 with Contour

use of boofcv.alg.filter.binary.Contour in project BoofCV by lessthanoptimal.

the class ExampleFitEllipse method main.

public static void main(String[] args) {
    // load and convert the image into a usable format
    BufferedImage image = UtilImageIO.loadImage(UtilIO.pathExample("particles01.jpg"));
    GrayF32 input = ConvertBufferedImage.convertFromSingle(image, null, GrayF32.class);
    GrayU8 binary = new GrayU8(input.width, input.height);
    // the mean pixel value is often a reasonable threshold when creating a binary image
    double mean = ImageStatistics.mean(input);
    // create a binary image by thresholding
    ThresholdImageOps.threshold(input, binary, (float) mean, true);
    // reduce noise with some filtering
    GrayU8 filtered = BinaryImageOps.erode8(binary, 1, null);
    filtered = BinaryImageOps.dilate8(filtered, 1, null);
    // Find the contour around the shapes
    List<Contour> contours = BinaryImageOps.contour(filtered, ConnectRule.EIGHT, null);
    // Fit an ellipse to each external contour and draw the results
    Graphics2D g2 = image.createGraphics();
    g2.setStroke(new BasicStroke(3));
    g2.setColor(Color.RED);
    for (Contour c : contours) {
        FitData<EllipseRotated_F64> ellipse = ShapeFittingOps.fitEllipse_I32(c.external, 0, false, null);
        VisualizeShapes.drawEllipse(ellipse.shape, g2);
    }
    // ShowImages.showWindow(VisualizeBinaryData.renderBinary(filtered, false, null),"Binary",true);
    ShowImages.showWindow(image, "Ellipses", true);
}
Also used : GrayF32(boofcv.struct.image.GrayF32) Contour(boofcv.alg.filter.binary.Contour) EllipseRotated_F64(georegression.struct.curve.EllipseRotated_F64) GrayU8(boofcv.struct.image.GrayU8) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage)

Example 7 with Contour

use of boofcv.alg.filter.binary.Contour 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);
    List<Contour> contours = BinaryImageOps.contour(binary, ConnectRule.EIGHT, null);
    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) {
        // Only the external contours are relevant.
        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 8 with Contour

use of boofcv.alg.filter.binary.Contour 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 9 with Contour

use of boofcv.alg.filter.binary.Contour in project BoofCV by lessthanoptimal.

the class DetectCalibrationSquareGridApp method getContours.

@Override
protected List<Contour> getContours() {
    BinaryContourFinder contour = alg.getDetectorSquare().getDetector().getContourFinder();
    List<Contour> contours = BinaryImageOps.convertContours(contour);
    return contours;
}
Also used : Contour(boofcv.alg.filter.binary.Contour) BinaryContourFinder(boofcv.abst.filter.binary.BinaryContourFinder)

Example 10 with Contour

use of boofcv.alg.filter.binary.Contour in project BoofCV by lessthanoptimal.

the class ExampleFitPolygon method fitBinaryImage.

/**
 * Fits polygons to found contours around binary blobs.
 */
public static void fitBinaryImage(GrayF32 input) {
    GrayU8 binary = new GrayU8(input.width, input.height);
    BufferedImage polygon = new BufferedImage(input.width, input.height, BufferedImage.TYPE_INT_RGB);
    // the mean pixel value is often a reasonable threshold when creating a binary image
    double mean = ImageStatistics.mean(input);
    // create a binary image by thresholding
    ThresholdImageOps.threshold(input, binary, (float) mean, true);
    // reduce noise with some filtering
    GrayU8 filtered = BinaryImageOps.erode8(binary, 1, null);
    filtered = BinaryImageOps.dilate8(filtered, 1, null);
    // Find the contour around the shapes
    List<Contour> contours = BinaryImageOps.contour(filtered, ConnectRule.EIGHT, null);
    // Fit a polygon to each shape and draw the results
    Graphics2D g2 = polygon.createGraphics();
    g2.setStroke(new BasicStroke(2));
    for (Contour c : contours) {
        // Fit the polygon to the found external contour.  Note loop = true
        List<PointIndex_I32> vertexes = ShapeFittingOps.fitPolygon(c.external, true, minSide, cornerPenalty);
        g2.setColor(Color.RED);
        VisualizeShapes.drawPolygon(vertexes, true, g2);
        // handle internal contours now
        g2.setColor(Color.BLUE);
        for (List<Point2D_I32> internal : c.internal) {
            vertexes = ShapeFittingOps.fitPolygon(internal, true, minSide, cornerPenalty);
            VisualizeShapes.drawPolygon(vertexes, true, g2);
        }
    }
    gui.addImage(polygon, "Binary Blob Contours");
}
Also used : Contour(boofcv.alg.filter.binary.Contour) EdgeContour(boofcv.alg.feature.detect.edge.EdgeContour) PointIndex_I32(boofcv.struct.PointIndex_I32) Point2D_I32(georegression.struct.point.Point2D_I32) GrayU8(boofcv.struct.image.GrayU8) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage)

Aggregations

Contour (boofcv.alg.filter.binary.Contour)14 EdgeContour (boofcv.alg.feature.detect.edge.EdgeContour)8 BufferedImage (java.awt.image.BufferedImage)8 ConvertBufferedImage (boofcv.io.image.ConvertBufferedImage)6 GrayU8 (boofcv.struct.image.GrayU8)6 Point2D_I32 (georegression.struct.point.Point2D_I32)6 PointIndex_I32 (boofcv.struct.PointIndex_I32)5 GrayF32 (boofcv.struct.image.GrayF32)3 ListDisplayPanel (boofcv.gui.ListDisplayPanel)2 BinaryContourFinder (boofcv.abst.filter.binary.BinaryContourFinder)1 FitData (boofcv.alg.shapes.FitData)1 ConvertBufferedImage (boofcv.core.image.ConvertBufferedImage)1 GrayS16 (boofcv.struct.image.GrayS16)1 GrayS32 (boofcv.struct.image.GrayS32)1 EllipseRotated_F64 (georegression.struct.curve.EllipseRotated_F64)1 Line2D (java.awt.geom.Line2D)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Random (java.util.Random)1 Term (nars.term.Term)1