Reputation: 1863
I have a black-and-white (binary) image. It contains a set of black blobs, surrounded by white. I am trying to write a C# program that will find a single path to cross through all of the shaded area, while leaving out as much of the white area as possible. This is very similar to finding a toolhead path for a 3D printer for any given layer; it needs to fill the solid parts, while only crossing in to empty space when it needs to get to another separate blob.
For example, here's a test image that I have created, that includes most of the challenges that I am facing (with only two blobs for simplicity):
I would want to find a path that would cross through all the black areas, while only crossing the white once to jump between the two shapes (where they are closest). I would also want the path to be as short as possible.
My program is written in C#, and I am already using AForge for image manipulation up to this point, so I already have that accessible.
So far, the closest that I got was with a flood-fill-like algorithm. It would start at a corner, and find the closest pixel(horizontally first, then vertically), to continue the path. But the paths didn't always go everywhere, and crossing over usually created a long and largely extraneous path. I also tried tracing the edges and going inward, but it still didn't work very well when a blob had a non-straight path. Searching didn't come up with much either; I tried searching for things relating to 3D printing algorithms, flood-fills, etc, but not much came up.
So, how should I tackle this?
Upvotes: 2
Views: 776
Reputation: 51903
filling the blobs
movements between blobs
Upvotes: 2
Reputation: 19621
In the case when the black areas shrink to points, this seems to be turning into the traveling salesman problem, so I suspect that there is no easy answer.
This suggests to me that you work out the closest distance between all pairs of black regions and then use some approximate solution to the traveling salesman problem on this distance matrix. A search finds a closest distance algorithm described at http://cgm.cs.mcgill.ca/~orm/mind2p.html. The TSP approximation algorithms that come to mind are those based on http://en.wikipedia.org/wiki/Minimum_spanning_tree, and on bitonic tours - e.g. in http://www.cs.helsinki.fi/u/jwkangas/daaa2013/sol-II.pdf.
Upvotes: 1