use of georegression.metric.ClosestPoint2D_F64 in project BoofCV by lessthanoptimal.
the class TestSnapToLineEdge method fit_noisy_affine.
public void fit_noisy_affine(Affine2D_F64 affine, Class imageType) {
setup(affine, imageType);
double tol = 0.5;
SnapToLineEdge alg = new SnapToLineEdge(10, 2, imageType);
Polygon2D_F64 input = new Polygon2D_F64(4);
AffinePointOps_F64.transform(affine, new Point2D_F64(x0, y0), input.get(0));
AffinePointOps_F64.transform(affine, new Point2D_F64(x0, y1), input.get(1));
AffinePointOps_F64.transform(affine, new Point2D_F64(x1, y1), input.get(2));
AffinePointOps_F64.transform(affine, new Point2D_F64(x1, y0), input.get(3));
LineGeneral2D_F64[] found = new LineGeneral2D_F64[input.size()];
for (int i = 0; i < found.length; i++) {
found[i] = new LineGeneral2D_F64();
}
for (int i = 0; i < 10; i++) {
// add some noise
Polygon2D_F64 noisy = input.copy();
addNoise(noisy, 2);
alg.setImage(image);
for (int j = 0; j < noisy.size(); j++) {
LineSegment2D_F64 edge = noisy.getLine(j, null);
double slopeX = edge.slopeX();
double slopeY = edge.slopeY();
double r = Math.sqrt(slopeX * slopeX + slopeY * slopeY);
slopeX /= r;
slopeY /= r;
// shrink it slightly to avoid weirdness along the corner
edge.a.x += 2 * slopeX;
edge.a.y += 2 * slopeY;
edge.b.x -= 2 * slopeX;
edge.b.y -= 2 * slopeY;
// optimize it a few times to get a good solution
for (int k = 0; k < 5; k++) {
assertTrue(alg.refine(edge.a, edge.b, found[j]));
edge.a.set(ClosestPoint2D_F64.closestPoint(found[j], edge.a, null));
edge.b.set(ClosestPoint2D_F64.closestPoint(found[j], edge.b, null));
}
}
// compute the corners and see how it did
for (int j = 0; j < input.size(); j++) {
int k = (j + 1) % input.size();
Point2D_F64 c = new Point2D_F64();
Intersection2D_F64.intersection(found[j], found[k], c);
double d = c.distance(input.get(k));
assertTrue(d <= tol);
}
}
}
Aggregations