Matte
Matte

Reputation: 339

Detect parallel lines using Hough transform in matlab

Suppose there is a binary image of a black background and white lines "plotted" on it, i.e., they aren't burnt onto the image. For example:

enter image description here

I need to retain only the lines that are parallel to atleast one of the other lines in the picture. If not perfectly parallel, at least close enough to be parallel(perhaps a variable that can control the degree of parallelism would help with that). In other words, if I choose a particular line and it has one or more lines that are parallel to it, I retain it, else I discard it. I need to do that for all lines in the image.

I came across Hough transform but I'm having trouble understanding how to use the bins to check the orientations and determine parallel lines. Or is there a better way to go about this?

Also, since the lines aren't a part of the image and are just plotted on it, I don't have an image to feed into the Hough Transform function. Can I use the output of the plot function as input directly? This is the code I wrote to plot the white lines:

Location1 is a m-by-2 matrix that contains the coordinates to draw the lines.

figure; imshow(blackImage); 
hold on ;
for i=1:size(Location1,1)-1
   h = plot([Location1(i,1) Location1(i+1,1)], [Location1(i,2) Location1(i+1,2)]) ;
   set(h,'linewidth', .1, 'color', 'b') ;
end

Any help would be appreciated.

Upvotes: 0

Views: 1292

Answers (1)

Cris Luengo
Cris Luengo

Reputation: 60494

Given that

for i=1:size(Location1,1)-1
   % one line = Location1(i,:) to Location1(i+1,:)
end

then

theta = atan2(diff(Location1(:,2)),diff(Location1(:,1)));

But since lines are parallel even if their theta is in the opposite direction, you want to map all angles to half a circle:

theta = mod(theta,pi/2);

Now theta is in the range [-π/2,π/2].

To find similar angles:

[s,i] = sort(theta);
k = find(diff(s)<0.01); % diff(s) is always positive because s is sorted
i = i([k,k+1]);
theta(i) % <-- sets of similar angles
% Location1(i,:),Location1(i+1,:) <- corresponding lines

Upvotes: 2

Related Questions