How to detect hand drawn lines using hough transfo

2019-09-04 06:36发布

问题:

I'm working on a matlab image processing project which basically extracts components and connections from an image of hand-drawn circuit diagram.

After preproccessing and obtaining skeleton image, I tried to use Hough transform to detect the lines so that i can identify corners and connection paths.

Here is the code:

[H,T,R] = hough(im);
peaks = houghpeaks(H,50,'Threshold',ceil(0.3*max(H(:))));
lines = houghlines(im, T,R,peaks, 'Fillgap', 20, 'MinLength', 20);

figure; imshow(im);
title('Lines detected');
hold on;
for l=1:length(lines)
    xy = [lines(l).point1; lines(l).point2];
    if ((lines(l).theta == 0)||(lines(l).theta >= 355 && lines(l).theta < 5)) || (lines(l).theta < 95 && lines(l).theta > 85) % detect only approx. horizontal and vertical lines 
        plot(xy(:,1),xy(:,2), 'LineWidth', 2);
    end
end

This is the input and output that i got when executing:

I need to detect all the line segments, that are almost horizontal or vertical, having minimum length, with some irregularities due to hand drawn nature.

In given screenshot, the output image shows only few detected lines, and some of lines are partially detected. It should actually detect all the wires used to connect components

How can i tune Hough transform functions or use any other methods to achieve this requirement ?

回答1:

(lines(l).theta >= 355 && lines(l).theta < 5) is impossible.So the only accepted theta values are 0 and [86,94].

To be honest, I don't entire understand why that gives horizontal and vertial lines - I'd think the result would be in radians and obviously 86 radians to 94 radians makes no sense.

Having said that, you not only want to check for 0 degrees / 0 radians but also 180 degrees / pi radians, and the same for 270 degrees = 3/2 pi = -1/2 pi.