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?
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:


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