Search in sources :

Example 1 with EdgeContour

use of boofcv.alg.feature.detect.edge.EdgeContour in project BoofCV by lessthanoptimal.

the class ShowEdgeContourApp method doProcess.

private void doProcess() {
    if (input == null)
        return;
    final BufferedImage temp;
    if (activeAlg == 0) {
        if (previousBlur != barCanny.getBlurRadius()) {
            previousBlur = barCanny.getBlurRadius();
            canny = FactoryEdgeDetectors.canny(previousBlur, true, true, imageType, derivType);
        }
        double thresh = barCanny.getThreshold() / 100.0;
        canny.process(workImage, (float) thresh * 0.1f, (float) thresh, null);
        List<EdgeContour> contours = canny.getContours();
        temp = VisualizeBinaryData.renderContours(contours, null, workImage.width, workImage.height, null);
    } else {
        // create a binary image by thresholding
        GThresholdImageOps.threshold(workImage, binary, barBinary.getThreshold(), barBinary.isDown());
        contour.process(binary, labeled);
        List<Contour> contours = BinaryImageOps.convertContours(contour);
        temp = VisualizeBinaryData.renderContours(contours, null, 0xFF1010, workImage.width, workImage.height, null);
    }
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            panel.setImage(temp);
            panel.repaint();
        }
    });
}
Also used : Contour(boofcv.alg.filter.binary.Contour) EdgeContour(boofcv.alg.feature.detect.edge.EdgeContour) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage) EdgeContour(boofcv.alg.feature.detect.edge.EdgeContour)

Example 2 with EdgeContour

use of boofcv.alg.feature.detect.edge.EdgeContour in project BoofCV by lessthanoptimal.

the class VisualizeImageData method drawEdgeContours.

/**
 * Draws each contour using a unique color.  Each segment of each edge is drawn using the same colors.
 *
 * @param contours List of edge contours
 * @param colors RGB color for each edge
 * @param output Where the output is written to
 * @param storage Optional working buffer for Bitmap image. Can be null.
 */
public static void drawEdgeContours(List<EdgeContour> contours, int[] colors, Bitmap output, byte[] storage) {
    if (output.getConfig() != Bitmap.Config.ARGB_8888)
        throw new IllegalArgumentException("Only ARGB_8888 is supported");
    if (storage == null)
        storage = declareStorage(output, null);
    else
        Arrays.fill(storage, (byte) 0);
    for (int i = 0; i < contours.size(); i++) {
        EdgeContour e = contours.get(i);
        int c = colors[i];
        for (int j = 0; j < e.segments.size(); j++) {
            EdgeSegment s = e.segments.get(j);
            for (int k = 0; k < s.points.size(); k++) {
                Point2D_I32 p = s.points.get(k);
                int index = p.y * 4 * output.getWidth() + p.x * 4;
                storage[index++] = (byte) (c >> 16);
                storage[index++] = (byte) (c >> 8);
                storage[index++] = (byte) c;
                storage[index] = (byte) 0xFF;
            }
        }
    }
    output.copyPixelsFromBuffer(ByteBuffer.wrap(storage));
}
Also used : EdgeSegment(boofcv.alg.feature.detect.edge.EdgeSegment) Point2D_I32(georegression.struct.point.Point2D_I32) EdgeContour(boofcv.alg.feature.detect.edge.EdgeContour)

Example 3 with EdgeContour

use of boofcv.alg.feature.detect.edge.EdgeContour in project BoofCV by lessthanoptimal.

the class VisualizeImageData method drawEdgeContours.

/**
 * Draws each contour using a single color.
 *
 * @param contours List of edge contours
 * @param color The RGB color that each edge pixel should be drawn
 * @param output Where the output is written to
 * @param storage Optional working buffer for Bitmap image. Can be null.
 */
public static void drawEdgeContours(List<EdgeContour> contours, int color, Bitmap output, byte[] storage) {
    if (output.getConfig() != Bitmap.Config.ARGB_8888)
        throw new IllegalArgumentException("Only ARGB_8888 is supported");
    if (storage == null)
        storage = declareStorage(output, null);
    else
        Arrays.fill(storage, (byte) 0);
    byte r = (byte) ((color >> 16) & 0xFF);
    byte g = (byte) ((color >> 8) & 0xFF);
    byte b = (byte) (color);
    for (int i = 0; i < contours.size(); i++) {
        EdgeContour e = contours.get(i);
        for (int j = 0; j < e.segments.size(); j++) {
            EdgeSegment s = e.segments.get(j);
            for (int k = 0; k < s.points.size(); k++) {
                Point2D_I32 p = s.points.get(k);
                int index = p.y * 4 * output.getWidth() + p.x * 4;
                storage[index++] = b;
                storage[index++] = g;
                storage[index++] = r;
                storage[index] = (byte) 0xFF;
            }
        }
    }
    output.copyPixelsFromBuffer(ByteBuffer.wrap(storage));
}
Also used : EdgeSegment(boofcv.alg.feature.detect.edge.EdgeSegment) Point2D_I32(georegression.struct.point.Point2D_I32) EdgeContour(boofcv.alg.feature.detect.edge.EdgeContour)

Example 4 with EdgeContour

use of boofcv.alg.feature.detect.edge.EdgeContour 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 5 with EdgeContour

use of boofcv.alg.feature.detect.edge.EdgeContour in project BoofCV by lessthanoptimal.

the class ExampleCannyEdge method main.

public static void main(String[] args) {
    BufferedImage image = UtilImageIO.loadImage(UtilIO.pathExample("simple_objects.jpg"));
    GrayU8 gray = ConvertBufferedImage.convertFrom(image, (GrayU8) null);
    GrayU8 edgeImage = gray.createSameShape();
    // Create a canny edge detector which will dynamically compute the threshold based on maximum edge intensity
    // It has also been configured to save the trace as a graph.  This is the graph created while performing
    // hysteresis thresholding.
    CannyEdge<GrayU8, GrayS16> canny = FactoryEdgeDetectors.canny(2, true, true, GrayU8.class, GrayS16.class);
    // The edge image is actually an optional parameter.  If you don't need it just pass in null
    canny.process(gray, 0.1f, 0.3f, edgeImage);
    // First get the contour created by canny
    List<EdgeContour> edgeContours = canny.getContours();
    // The 'edgeContours' is a tree graph that can be difficult to process.  An alternative is to extract
    // the contours from the binary image, which will produce a single loop for each connected cluster of pixels.
    // Note that you are only interested in external contours.
    List<Contour> contours = BinaryImageOps.contour(edgeImage, ConnectRule.EIGHT, null);
    // display the results
    BufferedImage visualBinary = VisualizeBinaryData.renderBinary(edgeImage, false, null);
    BufferedImage visualCannyContour = VisualizeBinaryData.renderContours(edgeContours, null, gray.width, gray.height, null);
    BufferedImage visualEdgeContour = new BufferedImage(gray.width, gray.height, BufferedImage.TYPE_INT_RGB);
    VisualizeBinaryData.render(contours, (int[]) null, visualEdgeContour);
    ListDisplayPanel panel = new ListDisplayPanel();
    panel.addImage(visualBinary, "Binary Edges from Canny");
    panel.addImage(visualCannyContour, "Canny Trace Graph");
    panel.addImage(visualEdgeContour, "Contour from Canny Binary");
    ShowImages.showWindow(panel, "Canny Edge", true);
}
Also used : ListDisplayPanel(boofcv.gui.ListDisplayPanel) Contour(boofcv.alg.filter.binary.Contour) EdgeContour(boofcv.alg.feature.detect.edge.EdgeContour) GrayS16(boofcv.struct.image.GrayS16) GrayU8(boofcv.struct.image.GrayU8) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage) EdgeContour(boofcv.alg.feature.detect.edge.EdgeContour)

Aggregations

EdgeContour (boofcv.alg.feature.detect.edge.EdgeContour)6 EdgeSegment (boofcv.alg.feature.detect.edge.EdgeSegment)4 ConvertBufferedImage (boofcv.io.image.ConvertBufferedImage)3 Point2D_I32 (georegression.struct.point.Point2D_I32)3 BufferedImage (java.awt.image.BufferedImage)3 Contour (boofcv.alg.filter.binary.Contour)2 ListDisplayPanel (boofcv.gui.ListDisplayPanel)1 PointIndex_I32 (boofcv.struct.PointIndex_I32)1 GrayF32 (boofcv.struct.image.GrayF32)1 GrayS16 (boofcv.struct.image.GrayS16)1 GrayU8 (boofcv.struct.image.GrayU8)1 Random (java.util.Random)1