I try to detect all lines in an image with some common geometric figures. The obvious choice, hough of MATLAB wouldn't print out the desired results.

I used the following MATLAB code:


figure, imshow(BW),hold on;

for k=1:length(LINES)
    xy=[LINES(k).point1; LINES(k).point2];


EDIT 1: Including the original RGB image here: enter image description here

And the image stored in the BW: enter image description here

The result of this code is shown in the next image. enter image description here

I think it is visible that hough method did not detect every line leaving quiet a few still to be detected. Does someone have a solution as to why it did not detect those lines and how to optimize the code so that it does detect them?

  • $\begingroup$ Are you restricting yourself to 10 peaks only? Depends on how many maxima you select in the accumulator space. $\endgroup$ Jan 11, 2018 at 12:02
  • $\begingroup$ I am not restricted to 10 peaks and indeed when I increase the number of peaks houghlines is detecting more lines. I tried using 80 peaks and after that 400 peaks but there was not much of a difference between those two. Both of them for example detect the right and bottom line of the bottom right square, but none of them detects the upper and left line. To be honest im not quiet sure how the number of peaks and the threshold work in houghlines. If possible I would appreciate an explanation, but every other small help is appreciated as well! $\endgroup$
    – Cion
    Jan 11, 2018 at 12:17
  • $\begingroup$ Why don't you also post the original image? $\endgroup$ Jan 11, 2018 at 19:54
  • $\begingroup$ Are you referring to the original RGB image or the image which the is assigned to the variable BW. If it is the latter the image you see up there is the image stored in BW excluding the drawn in lines. I will include the original RGB image I used in the first post. $\endgroup$
    – Cion
    Jan 11, 2018 at 20:01
  • $\begingroup$ Both maybe, so that people could use it and tune the parameters / change the algorithm and etc. $\endgroup$ Jan 11, 2018 at 20:02

1 Answer 1


houghpeaks function in MATLAB accepts a parameter named NHoodSize that specifies the size of the neighborhood where a non-maximum suppression (NMS) is carried out. In simple terms, NMS removes all duplicate peaks which lie in a certain window. This is what explains your double-lines with a single line. If you set the window size lower, such as $[7,7]$, you will see that it will start detecting them. For instance, modify the call as:

P=houghpeaks(H,20,'threshold',ceil(0.3*max(H(:))), 'NHoodSize',[7 7]);

You will then see the following result: enter image description here

You should play with those parameters till you find your ideal setting: Not many lines, and correct ones not missed.

  • $\begingroup$ Thank you very much for your reply. So basicly I can not get into a hough-linedetection with a predefined set of values and expect to get the best results for every image but instead I have to carefully select those values for every different image to get the best results, right ? $\endgroup$
    – Cion
    Jan 11, 2018 at 20:47
  • 1
    $\begingroup$ Yes and this is generally true for many image-processing/computer-vision algorithms. Though, if you have a dataset of similar images, you might find a more or less good parameter set working for a majority of images on this dataset. $\endgroup$ Jan 11, 2018 at 20:48
  • $\begingroup$ Alright, thank you very much for your time helping me out! $\endgroup$
    – Cion
    Jan 11, 2018 at 21:45
  • $\begingroup$ no worries @xCuse. $\endgroup$ Jan 11, 2018 at 21:55

Your Answer

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

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