Geometrical Methods for Shape and Pattern Recognition

advertisement
Geometrical Methods in Shape and Pattern Recognition
Lecture 7, Monday 6 March 2006
Helmut Alt
Notes and illustrations by Hans van Piggelen (0123668)
Solutions to assignments Homework 2 (February 21, 2006)
2.1 Convex hull algorithm
Show that the convex hull of a set of n points can be constructed in O(n) time once the points
are sorted by x-coordinate (and the ones having the same x-coordinate by y-coordinate).
A solution to this problem is the so-called Andrew's Monotone Chain Algorithm. After
sorting the point set S, pmin,min is the outermost left point, while pmax,max is the outmost right
point. If there are more points with a minimum x-coordinate, then pmin,max is defined as the
point with the largest y-coordinate of these points. The line Lmin from pmin,min to pmax,max
separates the point set into two subsets S1 and S2. When pmin,max is available Lmax is the line
from pmin,max to pmax,max splitting the upper subset S2 into two new subsets S2 and S3. Because
all defines points p are part of the convex hull, the points of the middle subset (S2) do not have
to be considered anymore.
Figure 1a. The set S after dividing it into three subsets,
formed by the lines Lmax and Lmin..
Figure 1b. The set S with its convex hull Ω formed by
combining the two hull chains Ωlower and Ωupper.
After splitting the set, two stacks are formed; one for each lower and upper subset. From the
lower subset S1, pmin,min and the next point are added to the lower stack. Then, the next point is
checked whether is lies left from the line between the two top points on the stack. If so,
proceed to the next point, if not, pop the last added point from the stack and check again for
the now two top points on the stack. Repeat this until pmax,max is reached. Do the same for the
upper subset S3, but then in decreasing order and the points are stored in the upper stack.
After this procedure, the two stacks form two hull chains Ωlower and Ωupper. By combining the
two, thereby checking for duplicate endpoints, the convex hull Ω of the whole set is obtained.
Because each point is considered only once, the algorithm’s runtime equals O(n).
2.2 Implementation
Implement a convex hull algorithm, either the one from exercise 2.1 or the Graham scan.
Here, the Graham Scan Algorithm is considered. In this algorithm, first an extreme point p0
of a point set S is determined (which is part of the convex hull to be found). Then the other
points are sorted by increasing counter-clockwise angle relative to the x-axis through p0. If
two points have the same angle, pop the one with the smallest distance to p0 (Fig 2a).
Figure 2a. Point p2 is left to point p1 when the angle is
considered relative to the imaginary x-axis through p0. So
these points are already in correct order.
Figure 2b. Point pk is checked. Because it lies right to the
lines p3-p4 and p2-p3, points p3 and p4 are popped from the
hull stack and pk is added.
After the sorting, the points are processes one-by-one and pulled on a stack. First p0 and p1 are
put on the stack, then for each point pk, it is checked whether this point lies left or right to the
line formed by the two top points of the stack. If it is left, then pk is added to the stack, if not
(thus lying right to the line), the stack’s top point is popped and the check is done again for
the now two top points on the stack. This continues until either pk lies left of the line, or only
one point is left on the stack. In both cases the pk is simply added to the stack (Fig 2b). Then
the algorithm proceeds with the next point until the last point is reached. Then this point is
checked with the first point in the stack p0 whether is does belong to the convex hull.
Finally, the points on the stack form the convex hull Ω of point set S.
The total runtime of this algorithm is O(n log(n)), where n is the number of points in the set.
The Graham Scan algorithm is mainly bounded by the sorting algorithm in it. The
determination of the convex hull after sorting takes only O(n) time.
In pseudo code, the algorithm can be summarized as follows:
Input: point set S = {P0, P1, P2, Pk, …} (size N)
Select the rightmost lowest point P0 in S
Sort S by angle about P0 as the center point
If two angles are equal, discard one with shortest distance
Push P0 and P1 onto convex hull stack C.
while (k < N)
{
Let R1 and R2 be the two top points on C
if(Pk is strictly left of the line R1 to R2)
Push Pk onto C
Increase k
else
Pop the top point R1 off stack C
}
Output: C = the convex hull of S.
2.3 Nearest neighbors
Let S be a set of n points in the plane. A point q of S is called the nearest neighbor of a point p
in S if p and q are not equal and all other points in S are not closer to p than q. Give an
efficient algorithm to compute the nearest neighbors of all points in S assuming that the
Voronoi diagram is available. Prove that your algorithm is correct.
Figure 3a. The Voronoi diagram VD(A) of an arbitrary set
of points A. The distance d2(p,q) between point p to the
nearest point q equals twice the distance d2(p,e) between
point p and the nearest bisector of p.
Figure 3b. The proof of the algorithm for finding the
nearest neighbor of a point p in a set. Point q is the
determined nearest neighbor.
The algorithm is as follows: compute for every point in S the distance to its surrounding
bisectors and keep track of the smallest distance to a bisector per point. The nearest neighbor
per point is at twice the distance to its nearest bisector (Fig 3a). The proof of this algorithm is
shown in Figure 3b. Here the closest Voronoi edge for point p and corresponding point q are
shown. It follows that no other point can lay in circle D, because otherwise the Voronoi edge
would not lay there. Next to this, if an other point would lie in circle C, then that point would
be the closest to p and the closest Voronoi edge would be a different one as well. So the
closest point to p is point q.
The runtime of this algorithm is O(n), where n is the number of points in the set.
Hausdorff distance between two sets of line segments
Find the Hausdorff distance dH(A, B) between two sets of non-intersecting line segments A
and B. Recall that the Hausdorff distance is defined as
dH(A, B) = min(a of A) max(b of B) d2(a, b)
Figure 4. The Voronoi diagram VD(B) (solid red line) of set B (solid black lines) and set A (solid blue line). The
Hausdorff distance occurs at one of the indicates blue dots on the line segment of set A: either the end points of the
line segment or the intersection point of the line segment with the Voronoi diagram of B.
Fact 1: while traversing a line (segment) e
a. the distance to a point p is first monotone decreasing and then monotone increasing
(Fig 5a)
b. the distance to a given line l is first monotone decreasing and then monotone
increasing (Fig 5b)
Consequence
As a consequence one can state that the largest distance between some line segment e of set A
to set B occurs either at a candidate point found at
a. an intersection point of e with VD(B), or
b. at an end point of e
When line segment e intersects an edge of VD(B), the line segment is decomposed into two
new line segments, of which either has the intersection point as one of the end points
(Figure 4).
This reduces to finitely many candidate points s, where one point of set s is the point having
the Hausdorff distance dH(A, B). In fact, if |A| = n and |B| = m, then there are 2n endpoints.
Figure 5a. The distance d(t) (blue lines) between a line
segment e to a point p while traversing e (upper left graph)
forms, when plotted, a parabola, as is indicated in the lower
right graph.
Figure 5b. The distance d(t) (blue lines) between a line
segment e and a line l while traversing e (upper left graph)
forms, when plotted, two linear lines, one decreasing,
increasing one thereafter. The zero point in the graph
corresponds to the intersection point of e and l.
For a Voronoi diagram having O(m) Voronoi edges, each edge is either a parabula or a line
segment. If an edge is intersected by a line segment of set A, in case it is a line segment it has
one intersection and in case it is a parabola it has two intersections. It follows that each
Voronoi edge intersects each segment of A at most twice.
This way one has O(mn) intersections and 2n endpoints, leading to O(mn) candidate points for
the Hausdorff distance.
Algorithm
The statements above make way for an algorithm that determines the Hausdorff distance
dH(A,B):
Input: two non-intersecting sets of line segments A and B
- Compute the Voronoi diagram VD(B) of set B.
- for each line segment e of A {
for each Voronoi edge e’ of VD(B) {
consider all intersection points and determine distance to closest
site in B
}
determine distance of end points of e to closest site in B
}
- Take the maximum of all distances as dH(A,B)
Output: Hausdorff distance dH(A, B)
The runtime of the construction of the Voronoi diagram VD(B) of set B is O(m log m). The
determination of the maximum of all points considered takes O(nm) time. Then the total
runtime of the whole algorithm becomes O(m log m + nm).
When swapping the roles of A and B to determine dH(B, A), the total runtime becomes O(n log
n + nm). This, when N = n + m, equals O(N 2).
Download