How can I use BoofCV to transform a list of vertices so that the center of the polygon they form will be moved to the middle of the image?

495 views Asked by At

I'm using the code below to detect shapes and extract a list of vertices for each shape found (stored in the List Vertexes). How can I transform the vertices for a shape to ensure that the shape will be located at the center of the image when I draw the vertices?

done here> VisualizeShapes.drawPolygon(vertexes,true,g2);

Is there a built-in way to do it using BoofCV or does it have to be done manually?

/**
 * Fits polygons to found contours around binary blobs.
 */
public static void fitBinaryImage(ImageFloat32 input) 
{
    ImageUInt8 binary = new ImageUInt8(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
    ImageUInt8 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,toleranceDist,toleranceAngle,100);
        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,toleranceDist,toleranceAngle,100);
            //********************************************************************************************************************
            //** how can I transform the vertexes here so that the polygon they form is centered at the middle of the image ? ****
            //********************************************************************************************************************
            VisualizeShapes.drawPolygon(vertexes,true,g2);
        }

        System.out.println(vertexes.toString());
        System.out.println(vertexes.size());

    }

    UtilImageIO.saveImage(polygon, "/Users/m/temp/3_fitbinaryimage.png");

}
0

There are 0 answers