You can use partition for this:
partition splits a set of elements into equivalence classes. You can define your equivalence class as all points within a given Euclidean distance (radius tolerance)
If you have C ++ 11, you can simply use the lambda function:
int th_distance = 18; // radius tolerance int th2 = th_distance * th_distance; // squared radius tolerance vector<int> labels; int n_labels = partition(pts, labels, [th2](const Point& lhs, const Point& rhs) { return ((lhs.x - rhs.x)*(lhs.x - rhs.x) + (lhs.y - rhs.y)*(lhs.y - rhs.y)) < th2; });
Otherwise, you can just build a functor (see details in the code below).
With the appropriate distance radius (I found that 18 works well in this image), I got:

Full code:
#include <opencv2\opencv.hpp>
source share