doowah2004 has asked for the wisdom of the Perl Monks concerning the following question:
------------------------------------------- | | | * | | | | * | | * | | | | | | * | | * | | | | * * | | | | | -------------------------------------------
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Interesting problem fitting points to a template (algorithm?)
by FoxtrotUniform (Prior) on Oct 05, 2004 at 21:22 UTC | |
I need to select the points with in the regions that closest match the template, with the knowlede that the region may not contain any valid points. A Voronoi diagram of your template will give you a set of "nearest-neighbour" regions. That is, each template point will have a Voronoi cell associated with it; every input point in that cell is closer to that cell's template point than to any other template point. (That would be so much easier to say in LaTeX math-mode.) If your points are on a grid, you can build a bitmap from the Voronoi diagram, with a unique colour for each template point's Voronoi cell. Checking inputs is simply a matter of looking up their position in the bitmap (finding the colour, and thus which template point they're closest to), and calculating the distance to that point. Simple and O(n) once you have the Voronoi diagram. In the continuous domain, things are probably a bit trickier. Find a good computational geometry text in your local university library; it should have plenty of stuff on this matter. -- | [reply] |
by tachyon (Chancellor) on Oct 05, 2004 at 22:40 UTC | |
cheers tachyon | [reply] |
|
Re: Interesting problem fitting points to a template (algorithm?)
by tachyon (Chancellor) on Oct 06, 2004 at 00:34 UTC | |
Given there are probably going to be rotation and x-y registration issues it seems to me that reducing the problem from 2 dimensions to a single dimension vector may work very swell in practice. It is easier to explain in code but the guts of the idea is simple. No matter how rotated or displaced, any planar figure (and the points define such a figure) will have a constant geometric centroid. Each point will have a constant distance from this centriod in one dimensional space regardless of rotation or displacement. We can use this to generate a very useful vector. We can also make the matching as precise of fuzzy as desired.
So in one dimensional space our vector tells us that the 3 lines all match the template. The effects of rotation and displacement have been removed. You can also deal with scaling simply by using normalisation. Matching the single dimension vector against a template set can be achieved in many ways. Stringifying the vector at a reliable precision and using a hash lookup against the template vectors is an obvious method. This vector process has some caveats. For any given 1 dimensional vector there are an infinite number of 2 dimensional shapes that map to it. For example a vector of 1,1,1,1 in 1 dimensional space can represent any number of rectangles or a square in 2 dimensional space.
cheers tachyon | [reply] [d/l] [select] |
by doowah2004 (Monk) on Oct 06, 2004 at 13:03 UTC | |
1. I have seven vertices making up a 2d shape, this shape may have suffered rotational and translational displacement. I have a rough idea of the maximum displayment, so I look at a given size ROI around where the points would be with no displacement. After discriminating the noise for size of points I am usually left with 1-2 possible points (not bad), but I have seen as many as twelve. So looking at a worse case (but very possible), if I am left with 10 points in each ROI, then starting with one ROI and working through each set of points I have 10^7 possible geometric objects that roughly look like my template. lots of checks.... 2. The distance between the points in the ROI << then the distance to the calculated centroid, so I am concerned about the ability to consistantly choose the seven correct points and not seven almost true points (similar to what you were saying about infinite number of shapes containg the same centroid). In your example it only looks at rotational displacement, there will be translation displacement, but that is easily delt with looking at the relative distances from the points to the centroid....so still valid. 3. What if the ROI contains points, but does not contain the True point? This does give me some thoughts that I will work through. I am going to take all of the suggestions, try to hash something out in the next couple of days, and repost what I have done. Cameron | [reply] |
by tachyon (Chancellor) on Oct 06, 2004 at 23:06 UTC | |
The method copes fine with 360 degrees of rotation or any ammount x-y translation (there is an even an example of that). This is simple a function of mapping from 2D to 1D space. In one dimensional space the concepts or rotation and x-y tranlation simply have no meaning. However the method shown relies on having *all* the points, no extra points and no missing points. This is because it is using the centriod to create the vector. A single missing or additional point will result in a different centroid, and vastly different vector. It is not clear what the real problem you are dealing with is. Curious? It seems now that missing and additional points are an issue. If so you can modify the method as follows. Without an accurate centroid we must choose one (or more) arbitrary points to use for our 2D->1D mapping. You could choose x_min, or y_min or any other arbitrary point but given templates with ~ 10 points I would just choose all of them in sequence, ie arbitratily declare each point the 'centroid' in turn and calculate the 1D distance mapping vector as before. Insert this data into some sort of easy lookup hash structure. You probably won't need the sort anymore which is O(logn) anyway. You now have all 10 possible 1D mappings of your template object from every possible point, thus rotation, translation, and added/missing points are not an issue. For any given point set you can rapidly check that sets correlation against all the templates in O(n) time. I will be interested to hear how you get on. cheers tachyon | [reply] |
|
Re: Interesting problem fitting points to a template (algorithm?)
by BrowserUk (Patriarch) on Oct 06, 2004 at 00:23 UTC | |
The following code and output attempts to demonstrate that it is possible to generate a single numerical "signature" or digest for your sets of points that will cause similar sets of points to sort close together. By generating these digests for each set in your database and then generating one for your new film as you receive it you can use a binary search/tree to locate the insertion point into the database, and so find those that have a similar signature. You then do a more thorough spacial comparison of a few either side to locate the best matches. This is not an exact science as the following output shows. Read more... (5 kB)
The first set of data (above the ----- line) are the signatures and related datasets read from the DATA section of the code. The second set of data includes all the first set again, plus the same datasets with modifications made, sorted by sugnature. In some I have deleted one or more pairs, other I've adjusted some values at random by +/-10. The output show that the algorithm I'm using (the third I tried) does a fairly good job of grouping the modified dataset close or adjacent to the original it is derived from. It's not perfect. With more tweaking and testing it should be possible to adjust it to do a better (more sensitive) job, but that requires a detailed knowledge (and sample datasets) to do properly. The basic approach (see calcSig()) is : Here's the code. It's not documented. If you are interested in pursuing the idea further and have questions, fell free to ask.
| [reply] [d/l] [select] |
by doowah2004 (Monk) on Oct 06, 2004 at 21:04 UTC | |
I have been thinking about filtering by point instead of collection (at least initially). For good registration, I need at least 4 points. Knowing the distance between the points I can work through each of the points and check the distance between each of the other points. If I set some tolerance, then I can discriminate the points that are not within the tolereance for at least 4 of the 6 distances. This should be fairly quick { 6 distances per point max ~ 10 points per ROI => 420 checks, and if I splice the array for the ROI then this number should decrease as bad points are thrown out. } If I can run a few tests then I will see what we typically have left. If it is only 1-3 points per ROI then your solution is much more viable. Quick question (I have not looked for this yet, thought of it while writting), do you or does anyone know of a good module for doing the permutations I need? i.e. N arrays with M elements, creating new unique arrays containing only 1 element from each of the N arrays? I am sure that it is out there, I will look for it tonight. Thanks for you help, Cameron | [reply] |
by BrowserUk (Patriarch) on Oct 06, 2004 at 21:58 UTC | |
...does anyone know of a good module for doing the permutations... Algorithm::Permute and NextPermute() from tye's Algorithm::Loops module are both very good implementations. I think that the idea of generating a signature could be made to work well for your application. I saw the extra information that you posted in reply to tachyon and I think I see the problems your describing, and also some of a possible solution. The key to taking the thoughts further is having some test data to work with. If you felt like placing some on your doowah2004's scratchpad, and /msg'ing me, I enjoy playing with it. That said, I'm still not entirely sure that I understand the full scope of the problem, so maybe I should just accept your more informed judgement on the matter :) | [reply] |
|
Re: Interesting problem fitting points to a template (algorithm?)
by fglock (Vicar) on Oct 05, 2004 at 21:46 UTC | |
How about Algorithm::Points::MinimumDistance | [reply] |
|
Re: Interesting problem fitting points to a template (algorithm?)
by doowah2004 (Monk) on Oct 05, 2004 at 21:41 UTC | |
The points are puntures through the film by a physical template, so the distance between the true points should be very close to the measured distance for the template. But when the holes are made the film might be slightly rotated. What this means is that the scanned film x,y values are meaningless when comparing to the template x,y values. BUT the delta values (relative distances) between the points should closely correlate. I hope that this is now clear. Cameron I will have to read more about Voronoi Diagrams before I can comment on the previous post. | [reply] |
by FoxtrotUniform (Prior) on Oct 05, 2004 at 22:06 UTC | |
The points are puntures through the film by a physical template, so the distance between the true points should be very close to the measured distance for the template. But when the holes are made the film might be slightly rotated. What this means is that the scanned film x,y values are meaningless when comparing to the template x,y values. BUT the delta values (relative distances) between the points should closely correlate. You might have more luck with a rigid point correspondence algorithm, then. Edit: Oops, posted too soon. Provided that the deltas are pretty small, the Voronoi diagram solution should still work. If it does, it will probably be easier to implement and much quicker than a point-correspondence algorithm (most of the PC approaches I'm aware of start by building an affinity matrix between all of the feature points, then do an eigenvalue decomposition on that, so they're going to be pretty slow compared to a Voronoi-cell lookup). If you're looking for hella speed and have the resources to do it, you could probably do the Voronoi lookup fairly easily with programmable graphics hardware (read: consumer graphics cards no more than, say, two years old). Iterating through, say, a thousand possible rotations of the film onto the template should be a piece of cake; that's exactly the sort of thing that modern graphics cards are optimized for. If this sounds like something you have the resources to do, send me a /msg -- we're getting a bit off-topic here, but we're also getting closer to my area of research. -- | [reply] |
|
Re: Interesting problem fitting points to a template (algorithm?)
by doowah2004 (Monk) on Oct 13, 2004 at 16:34 UTC | |
| [reply] [d/l] |