# How can I detect every line with Hough method?

3 views (last 30 days)
Furkan Bayazit on 20 Oct 2022
Answered: Aritra on 23 Nov 2022
I am trying to detect all possible lines in an image with hough method, however I need to adjust the "MinLength" and "FİllGap" for better results, my question as I mentioned in the headline, is there any way to detect all possible lines with a special function or something else? The code I used is given and the image is added.
BW2gray = rgb2gray(I);
BW = edge(BW2gray,"canny");
[H,theta,rho] = hough(BW);
P = houghpeaks(H,10,'threshold',ceil(0.3*max(H(:))));
x = theta(P(:,2));
y = rho(P(:,1));
lines = houghlines(BW,theta,rho,P,'FillGap',20,'MinLength',50);
figure, imshow(BW), hold on
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','cyan');
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','red');
plot(xy(2,1),xy(2,2),'>','LineWidth',2,'Color','cyan');
end
for i = 1:length(lines)
velocityX(i) = lines(i).point2(1,1)-lines(i).point1(1,1);
velocityY(i) = -lines(i).point2(1,2)-lines(i).point1(1,2);
fprintf("the velocity vector of line %d is %fi %dj \n",i,velocityX(i),velocityY(i));
end

Aritra on 23 Nov 2022
Hi Furkan,
As per my understanding you are trying to detect all possible lines in an image using the Hough method. This can be achieved by using the houghlines(BW,theta,rho,peaks)function which is designed to extract line segments in images based on Hough transform.

### Categories

Find more on Hough Transform in Help Center and File Exchange

R2021b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!