Robust line fitting to a set of 2D points is a well studied problem for which several approaches are known. They usually consider the point cloud as unstructured.
I call a digital path a sequence of pixels (points with integer coordinates) that are 8- or 4- connected. We may assume that the abscissa grows monotonously.
I wan to fit a line segment to a given path, in such a way that the deviations from the segment do not exceed a given threshold (a few pixels), while the segment is the longest possible.
Any hint ?
I am well aware of the Douglas-Peucker algorithm, but it is not suitable to find a single segment.
Update:
By means of the Melkman algorithm, we can compute the convex hull from one endpoint toward the other, in linear time N) for N pixels. From the convex hull, the minimum width can be obtained by Rotating Calipers, in time linear wrt the hull size. This would yield a total O(NH) effort to compute all widths (H pixels on the hull on average). But I suspect that the Rotating Calipers could be applied incrementally, starting from the previous position on the previous hull, resulting in a better amortized complexity. I didn't prove this formally.
Equipped with this device, we can find sections out of tolerance. But this doesn't tell us yet how to find the longest one.
Next Update:
Using a dynamic convex hull algorithm, it should be possible to let the hull grow by adding vertices in path order, until the width exceeds the given tolerance. Then remove an initial vertex, and add end vertexes until the tolerance is exceeded again, and so on.
This way, in a number of hull updates that is linear in the number of vertexes, we can find all parts of the path that are just below the tolerance. By combining with the previous idea (Rotating Calipers update upon hull update), I guess that an efficient solution can be achieved.




