Problem: Find an unwanted line in an image using Hough transform.
I have done the following,
- Apply directional filter to analyze 12 different directions, rotated with respect to 15° each other.
- Apply thresholding to obtain 12 binary images.
Now, I need to select either of those two images marked in yellow. Coz, the lines in those two images are the most prominent.
I have tried the following code. It doesn't seem to be working.
MATLAB Code
% Read 12 images into workspace.
input_images = {imread('1.png'),imread('2.png'),imread('3.png'),...
imread('4.png'),imread('5.png'),imread('6.png'),...
imread('7.png'),imread('8.png'),imread('9.png'),...
imread('10.png'),imread('11.png'),imread('12.png')};
longest_line = struct('point1',[0 0], 'point2',[0 0], 'theta', 0, 'rho', 0);
for n=1:12
%Create a binary image.
binary_image = edge(input_images{n},'canny');
%Create the Hough transform using the binary image.
[H,T,R] = hough(binary_image);
%Find peaks in the Hough transform of the image.
P = houghpeaks(H,3,'threshold',ceil(0.3*max(H(:))));
%Find lines
hough_lines = houghlines(binary_image,T,R,P,'FillGap',5,'MinLength',7);
longest_line = FindTheLongestLine(hough_lines, longest_line);
end
% Highlight the longest line segment by coloring it cyan.
plot(longest_line.point1, longest_line.point2,'LineWidth',2,'Color','cyan');
.
Relevant Source Code
function longest_line = FindTheLongestLine( hough_lines , old_longest_line)
%FINDTHELONGESTLINE Summary of this function goes here
% Detailed explanation goes here
longest_line = struct('point1',[0 0] ,'point2',[0 0],'theta', 0, 'rho', 0);
max_len = 0;
N = length(hough_lines);
for i = 1:N
% Determine the endpoints of the longest line segment
len = LenthOfLine(hough_lines(i));
if ( len > max_len)
max_len = len;
longest_line = hough_lines(i);
end
end
old_len = LenthOfLine(old_longest_line);
new_len = LenthOfLine(longest_line);
if(old_len > new_len)
longest_line = old_longest_line;
end
end
function length = LenthOfLine( linex )
%LENTHOFLINE Summary of this function goes here
% Detailed explanation goes here
length = norm(linex.point1 - linex.point2);
end
Test Images
Here are the 12 images, drive.google.com/open?id=0B-2FDw63ZNTnRnEzYlNyS0V4YVE





pi/1, pi/2, pi/3, ..., pi/12. Instead you want to usepi*(1/12), pi*(2/12), pi*(3/12), ..., pi*(12/12). Also "I couldn't make the given answer work" is very loose. It seems to do exactly what you want, so show us perhaps how you implemented it and why it doesn't work. - Wolfie