以下のコードを使用して形状を検出し、見つかった各形状の頂点のリストを抽出します (リスト頂点に格納されています)。頂点を描画するときに形状がイメージの中心に配置されるように、形状の頂点を変換するにはどうすればよいですか?
ここで実行> 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");
}