albusdemens
albusdemens

Reputation: 6624

Find points forming lines using Hough transform

I have a dataset and I would like to group the points forming lines parallel to the Y axis (see image). I guess the way to do it is by using the Hough transform; do you have any suggestion of how to do it in Matlab?

enter image description here

Upvotes: 0

Views: 452

Answers (1)

Steffen
Steffen

Reputation: 2441

Well, with the restriction, only to detect lines parallel to the y axis, a full hough transform is not necessary. Just project all points on the x-axis accumulate them and find peaks.

%assuming you want a resolution of 0.01 covering x space from 0 to 1
points = rand(1000,2);
figure(1);
plot(points(:,1),points(:,2),'b.');
minX = 0;
maxX = 1;
resolution = 0.01;
xValues = minX:resolution:maxX;
accu = zeros(1,length(xValues));
for i = 1:length(points)
    xVal = points(i,1); % access x value of point;
    idx = round(((xVal-minX)/resolution))+1;
    accu(idx) = accu(idx) +1;
end

Now you have an accumulator where you can search for maximas.

[pks,idx] = findpeaks(accu);

You might only want to consider peaks with at least minPoints points:

minPoints = 10;
idx = idx(pks>minPoints);

Then you can process this lines further:

for i = 1:length(idx)
    % select all points corresponding to line:
    idc = abs(points(:,1)-xValues(idx(i))) < resolution/2;
    pointsOnLine = points(idc,:);
    figure(1);
    hold on;
    plot(pointsOnLine(:,1),pointsOnLine(:,2),'ro');
    minY = min(pointsOnLine(:,2));
    maxY = max(pointsOnLine(:,2));
    plot([xValues(idx(i)),xValues(idx(i))],[minY,maxY],'r-');
end

To remove lines with large gaps, you can use sort() to sort the points according to their y values and then find large jumps with diff.

Upvotes: 1

Related Questions