简体   繁体   中英

Java library for 2d path comparison

I have one 2d line (it can be a curved line, with loops and so on), and multiple similar paths. I want to compare the first path with the rest, and determine which one is the most similar (in percentage if possible).

I was thinking maybe transforming the paths into bitmaps and then using a library to compare the bitmaps, but that seems like overkill. In my case, I have only an uninterrupted path, made of points, and no different colors or anything.

Can anyone help me?

Edit:

So the first line is the black one. I compare all other lines to it. I want a library or algorithm that can say: the red line is 90% accurate (because it has almost the same shape, and is close to the black one); the blue line is 5% accurate - this percentage is made up for this example... - because it has a similar shape, but it's smaller and not close to the black path.

So the criterion of similarity would be:

  • how close the lines are one to another
  • what shape do they have
  • how big they are

(color doesn't matter)

I know it's impossible to find a library that considers all this. But the most important comparisons should be: are they the same shape and size? The distance I can calculate on my own. 在此输入图像描述

I can think of two measures to express similarity between two lines N (defined as straight line segments between points p0, p1... pr) M (with straight line segments between q0, q1, ...qs). I assume that p0 and q0 are always closer than p0 and qs.

1) Area

Use the sum of the areas enclosed between N and M, where N and M are more different as the area gets larger. To get N and M to form a closed shape you should connect p0 and q0 and pr and qs with straight line segments. To be able to calculate the surface of the enclosed areas, introduce new points at the intersections between segments of N and M, so that you get one or more simple polygons without holes or self-intersections. The area of such a polygon is relatively straightforward to compute (search for "polygon area calculation" around on the web), sum the areas and you have your measure of (dis)similarity.

2) Sampling

Take a predefined number (say, 1000) of sample points O that lie on N (either evenly spaced with respect to the entire line, or evenly spaced over each line segment of N). For each sample point o in O, we'll calculate the distance to the closest corresponding point on M: the result is the sum of these distances. Next, reverse the roles: take the sample points from M and calculate each closest corresponding point on N, and sum their distances. Whichever of these two produces the smallest sum (they're likely not the same!) is the measure of (dis)similarity.
Note : To locate the closest corresponding point on M, locate the closest point for every straight line segment in M (which is simple algebra, google for "shortest distance between a point and a straight line segment"). Use the result from the segment that has the smallest distance to o.

Comparison

Method 1 requires several geometric primitives (point, line segment, polygon) and operations on them (such as calculating intersection points and polygon areas), in order to implement. This is more work, but produces a more robust result and is easier to optimize for lines consisting of lots of line segments.

Method 2 requires picking a "correct" number of sample points, which can be hard if the lines have alternating parts with little detail and parts with lots of detail (ie a lot of line segments close together), and its implementation is likely to quickly get (very) slow with a large number of sample points (matching every sample point against every line segment is a quadratic operation). On the upside, it doesn't require a lot of geometric operations and is relatively easy to implement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM