MatLab - Can not detect all lines in a “simple” image using houghlines

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:

[H,T,R]=hough(BW);
P=houghpeaks(H,10,'threshold',ceil(0.2*max(H(:))));

LINES=houghlines(BW,T,R,P,'FillGap',2,'MinLength',3);
figure, imshow(BW),hold on;

for k=1:length(LINES)
xy=[LINES(k).point1; LINES(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',1,'Color','Green');

plot(xy(1,1),xy(1,2),'x','LineWidth',1,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',1,'Color','red');
end


EDIT 1: Including the original RGB image here:

And the image stored in the BW:

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

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?

• Are you restricting yourself to 10 peaks only? Depends on how many maxima you select in the accumulator space. – Tolga Birdal Jan 11 '18 at 12:02
• 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! – xCuse Jan 11 '18 at 12:17
• Why don't you also post the original image? – Tolga Birdal Jan 11 '18 at 19:54
• 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. – xCuse Jan 11 '18 at 20:01
• Both maybe, so that people could use it and tune the parameters / change the algorithm and etc. – Tolga Birdal Jan 11 '18 at 20:02

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: