问题:使用Hough变换在图像中找到不需要的线。使用hough变换选择线
我已经做了以下内容,
- 应用方向滤波器以分析12个不同的方向上,转动时相对于15°彼此。
- 应用阈值获得12个二值图像。
现在,我需要选择标记为黄色,这两个图像。 Coz,这两幅图像中的线条最为突出。
我试过下面的代码。它似乎没有工作。
MATLAB代码
% 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');
。
相关的源代码
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
测试图片
这里有12幅图像,drive.google.com/open?id=0B-2FDw63ZNTnRnEzYlNyS0V4YVE
你能解释一下你的代码中的问题和/或提供的答案? – m7913d
@ m7913d,我的代码无法选择所需的特定行。我也无法让给定的答案也起作用。 – anonymous
算法的输出是什么?你能分别上传二进制图像吗?而不是寻找最长的线,选择具有最高hough变换值的线可能会很有用。 – m7913d