use of boofcv.struct.PointIndex_I32 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");
}
use of boofcv.struct.PointIndex_I32 in project BoofCV by lessthanoptimal.
the class ShapeFitContourApp method visualizePolygon.
private void visualizePolygon(Graphics2D g2, double scale, List<PointIndex_I32> vertexes) {
g2.setStroke(new BasicStroke(2));
VisualizeShapes.drawPolygon(vertexes, true, scale, g2);
if (controlPanel.isCornersVisible()) {
g2.setColor(Color.BLUE);
g2.setStroke(new BasicStroke(2f));
for (PointIndex_I32 p : vertexes) {
VisualizeFeatures.drawCircle(g2, scale * (p.x + 0.5), scale * (p.y + 0.5), 5);
}
}
}
use of boofcv.struct.PointIndex_I32 in project BoofCV by lessthanoptimal.
the class ShapeFittingOps method fitPolygon.
/**
* <p>Fits a polygon to the provided sequence of connected points. The found polygon is returned as a list of
* vertices. Each point in the original sequence is guaranteed to be within "toleranceDist' of a line segment.</p>
*
* <p>Internally a split-and-merge algorithm is used. See referenced classes for more information. Consider
* using internal algorithms directly if this function is a performance bottleneck.</p>
*
* @param sequence Ordered and connected list of points.
* @param loop If true the sequence is a connected at both ends, otherwise it is assumed to not be.
* @param minimumSideLength The minimum allowed side length in pixels. Try 10
* @param cornerPenalty How much a corner is penalized. Try 0.25
* @return Vertexes in the fit polygon.
* @see PolylineSplitMerge
*/
public static List<PointIndex_I32> fitPolygon(List<Point2D_I32> sequence, boolean loop, int minimumSideLength, double cornerPenalty) {
PolylineSplitMerge alg = new PolylineSplitMerge();
alg.setLoops(loop);
alg.setMinimumSideLength(minimumSideLength);
alg.setCornerScorePenalty(cornerPenalty);
alg.process(sequence);
PolylineSplitMerge.CandidatePolyline best = alg.getBestPolyline();
DogArray<PointIndex_I32> output = new DogArray<>(PointIndex_I32::new);
if (best != null) {
indexToPointIndex(sequence, best.splits, output);
}
return new ArrayList<>(output.toList());
}
use of boofcv.struct.PointIndex_I32 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);
// Only external contours are relevant
List<Contour> contours = BinaryImageOps.contourExternal(binary, ConnectRule.EIGHT);
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) {
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");
}
use of boofcv.struct.PointIndex_I32 in project BoofCV by lessthanoptimal.
the class ShapeFittingOps method indexToPointIndex.
/**
* Converts the list of indexes in a sequence into a list of {@link PointIndex_I32}.
* @param sequence Sequence of points.
* @param indexes List of indexes in the sequence.
* @param output Output list of {@link PointIndex_I32}.
*/
public static void indexToPointIndex(List<Point2D_I32> sequence, GrowQueue_I32 indexes, FastQueue<PointIndex_I32> output) {
output.reset();
for (int i = 0; i < indexes.size; i++) {
int index = indexes.data[i];
Point2D_I32 p = sequence.get(index);
PointIndex_I32 o = output.grow();
o.x = p.x;
o.y = p.y;
o.index = index;
}
}
Aggregations