1

I have continuous digital curves that are close to a line segment, except maybe at both ends.

How would you delimit the linear section (say remove the red parts) ? The solution needn't be very accurate, but fast.

enter image description here

  • @Piglet: given that we already have the curve, Hough is probably overkill, unless specialized to this case. And Hough doesn't give the endpoints. –  Feb 26 '18 at 16:07
  • Do you have the points as numeric values in a list, or a CSV or an array? – Mark Setchell Feb 26 '18 at 18:34
  • That's just a hand-made an illustration. I have a program that generates dozens of such curves in an array. –  Feb 26 '18 at 19:08

1 Answers1

0

There is an algorithm called Douglas-Peucker, which takes a curve described by points (or rather the straight lines between them) and simplifies it to be described by fewer points. The output is an approximation consisting of longer straight lines.

I would walk along your line, extracting the coordinates of each pixel in the right order, then apply Douglas-Peucker. The longest line segment in the output will be the one you need to keep. The trick there is to find the right parameter to the algorithm (the parameter is the maximum distance allowed between the approximation and the original line).

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • That's right, Douglas-Peucker is an option. In this case the allowed deviation would be 2 or 3 pixels. As far as I know there's an efficient O(N Log N) version, but a little complex. –  Feb 26 '18 at 19:10
  • @YvesDaoust: I have a trivial implementation in C++ that works really fast. If no simplification is possible, it is O(n^2), but that never happens. According to Wikipedia the expected running time is O(n log n), I never checked. That article has pseudo-code for the algorithm that looks a lot like my C++ implementation. – Cris Luengo Feb 26 '18 at 19:33
  • Thanks, I have my own implementation. I didn't know about the expected time. I assume we won't be in a bad case here. –  Feb 26 '18 at 19:44