How to fit curve through cloud of points? OpenCV/C++

Vladimir picture Vladimir · Sep 15, 2015 · Viewed 10.9k times · Source

Simple question. How to draw curve through all these points like human would do?

My example input image is here:

enter image description here

Answer

ilke444 picture ilke444 · Sep 15, 2015

You have many options.

1) If the points are mostly on a line as in the example, the easiest one would be to use morphological transformations (http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_morphological_ops/py_morphological_ops.html). For example, if you use closing (which is dilate and erode), you will first make the points larger by dilating (so that they'll touch each other), and then you will erode (so that they will go back to their original size while still touching each other). That can imitate a line.

2) You can use fitLine function of OpenCV, which fits a line to a 2D or 3D set. (http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html)

3) You can join point by point. However for that you need to guess an orientation for the line segments, and order the points accordingly. You can use OBB (oriented bounding box) structures to hold the points that belong to some line segments, and then use the dominant axis of OBB as the line segment.