我正在使用下面的代码来检测形状并提取找到的每个形状的顶点列表(存储在列表顶点中)。如何变换形状的顶点以确保在绘制顶点时形状将位于图像的中心?
在这里完成> VisualizeShapes.drawPolygon(顶点,真实,G2);
是否有使用BoofCV的内置方式或必须手动完成?
/**
* 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");
}