1

I use the following code to extract lines from a given 25x25 black&white-image:

[H, theta, rho] = hough(image);
peaks = houghpeaks(H, 20,'NHoodSize',[19 19]);
lines = houghlines(image, theta, rho, peaks, 'FillGap', 1, 'MinLength', 3);

I then plot the found lines on the given image. The result looks like this:

The lines found by my hough transform

What I can't understand is, why this procedure does not find a line on the left border of the image, going from top to bottom (or vice versa). Instead for example the pink line is found, which I would think has less evidence in hough space to be there (since it touches less white pixels). Does anyone have an intuition why this might be the case?

I tried changing the parameters a little bit or add some padding to the image, but nothing has worked so far.

edit: original image as requested:

original image

In

3
  • 1
    How did you pad it? Edge replication or zero padding? My guess is that it's being ignored since it's DIRECTLY on the edge. Zero padding would fix this.
    – Raab70
    May 8, 2014 at 12:24
  • padded it using image = padarray(image, [3 3]). did not help May 8, 2014 at 12:30
  • Can you include the original image so we can test it?
    – Raab70
    May 8, 2014 at 12:32

1 Answer 1

2

The default threshold value is too high so the line is not found. I also reduced the nhood size since you want to find horizontal and vertical lines and not angles, so they will all be very close to each other. Also note at the top I set the edges to zero, in the image you posted there is a thin border of 204's around the outside, this just elmiminates the border. Here is my script.

clc;clearvars;close all;
im=imread('B5oOc.png');
im=rgb2gray(im);
im(:,1:2)=0;
im(1,:)=0;
im(end,:)=0;
im(:,end)=0;
BW=edge(im,'canny');

[H, T, R] = hough(BW);
P = houghpeaks(H, 20,'NHoodSize',[1 1],'threshold',ceil(0.3*max(H(:))));
lines = houghlines(BW, T, R, P, 'FillGap', 1, 'MinLength', 3);

imshow(imadjust(mat2gray(H)),'XData',T,'YData',R,...
      'InitialMagnification','fit');
title('Hough Transform of Image');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
colormap(hot);

x = T(P(:,2));
y = R(P(:,1));
plot(x,y,'s','color','blue');

figure;
imagesc(im);hold on;colormap gray;
axis image; 
max_len = 0;
for k = 1:length(lines)
   xy = [lines(k).point1; lines(k).point2];
   plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');

   % Plot beginnings and ends of lines
   plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
   plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');

   % Determine the endpoints of the longest line segment
   len = norm(lines(k).point1 - lines(k).point2);
   if ( len > max_len)
      max_len = len;
      xy_long = xy;
   end
end

% highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','red');

The output is this: Hough transform result

2
  • Hi! Thanks for your answer. Well, I also want non horizontal/vertical lines to be found. Also, I don't see why in my code the pink line is found but not the line at the right border. Why would it do that? A low threshold cannot possibly be the reason? I also think there is a difference between using the *.png and the 25*25 matrix. May 12, 2014 at 13:44
  • Unfortunately, I can't reproduce the matrix for the first example image, but there is another one with similar behavior. Indices which have to be set to true = [10 11 12 13 14 15 16 33 43 57 70 82 107 132 147 148 157 158 169 170 171 172 183 193 194 208 217 218 232 233 242 257 267 282 292 307 317 332 333 342 343 358 368 375 376 377 378 383 393 403 404 408 418 429 433 443 454 458 468 473 478 479 483 493 498 508 518 533 543 544 558 569 584] May 12, 2014 at 13:45

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.