I'm looking for a way to trace a closed curve (i.e. a long list of pixel values all connected by lines in between) that approximates a bitmap image of black and white lines.
The image starts out as an arbitrary bitmap image. I've been using this one for testing.
Then it's converted to greyscale, passed through an edge detection filter, and finally converted to a true monochrome black/white bitmap. The end result of this preprocessing looks like this.
My goal is to trace a closed curve around the white pixels.
My naive approach was to make a list of all the white pixels in the image, then use a greedy travelling-salesman solver to draw a path around them. Which results in a path looking like this.
This works, there's a lot of artifacts but it works. But it's very slow. This relatively simple image takes a couple minutes to process. Any images more complex than this one need to be reduced in quality by a substantial amount before the time taken is reasonable. The resulting path is then almost unrecognizable.
When looking up algorithms for tracing images, I find a lot of things either built into image editing software (I need an algorithm that I can implement in Python), or overkill bitmap-to-vector algorithms designed to preserve color and line thickness.
All I need is a list of pixel values that trace out the lines.
There's got to be a better way than straight up solving travelling-salesman, right?