Matan Touti
Matan Touti

Reputation: 191

Reduce Close Points

input: C matrix 2xN (2D points)

output: C matrix 2xM (2D points) with equal or less points.

Lets say we have C matrix 2xN that contains several 2D points, and it looks something like that:

enter image description here

What we want is to group "close" points to one point, measured by the average of the other points. For example, in the second image, every group of blue circle will be one point, the point coordinate will be the average point off all points in the blue circle. also by saying "close", I mean that: their distance one to each other will be smaller then DELTA (known scalar). So wanted output is:

enter image description here

About running time of the algorithm, I don't have upper-limit request but I call that method several times...
What i have tried:

function C = ReduceClosePoints(C ,l_boundry)
    x_size = abs(l_boundry(1,1)-l_boundry(1,2)); %220
    DELTA = x_size/10;
    T = [];

    for i=1:size(C,2)
        sum = C(:,i);
        n=1;
        for j=1:size(C,2)
            if i~=j     %not in same point
                D = DistancePointToPoint(C(:,i),C(:,j));
                if D < DELTA
                    sum = sum + C(:,j);
                    n=n+1;
                end
            end
        end
        sum = sum./n; %new point -> save in T matrix
        T = [T sum];
    end
    C = T;
end

I am using Matlab. Thank you

Upvotes: 0

Views: 78

Answers (2)

Matan Touti
Matan Touti

Reputation: 191

I forgot to remove points that i have tested before.
If that code will be useful to someone use that code:

function C = ReduceClosePoints(C ,l_boundry)
     x_size = abs(boundry(1,1)-boundry(1,2)); %220 / 190
     DELTA = x_size/10;
     i=1;
     while i~=size(C,2)+1
         sum = C(:,i);
         n=1;
         j=i;
         while j~=size(C,2)+1
             if i~=j     %not same point
                 D = DistancePointToPoint(C(:,i),C(:,j));
                 if D < DELTA
                     sum = sum + C(:,j);
                     n=n+1;
                     C(:,j) = [];
                     j=j-1;
                 end
             end
             j=j+1;
         end
         C(:,i) = sum./n; %change to new point
         i=i+1;
     end
end

Upvotes: 0

Notlikethat
Notlikethat

Reputation: 20934

The simplest way to remove the duplicates from the output is in the final step, by replacing:

C = T;

with:

C = unique(T', 'rows')';

Note that unique() in matrix context only works row-wise, so we have to transpose twice.

Upvotes: 1

Related Questions