问题:使用Hough变换在图像中找到一条不必要的线。
我尝试了以下代码。它似乎没有起作用。
MATLAB 代码
我已经完成了以下工作:
- 应用方向滤波器分析12个不同方向,每个方向相对于另一个方向旋转15度。
- 应用阈值处理以获取12个二进制图像。
我尝试了以下代码。它似乎没有起作用。
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');
.
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
测试图片
这里有12张图片,drive.google.com/open?id=0B-2FDw63ZNTnRnEzYlNyS0V4YVE
pi/1, pi/2, pi/3, ..., pi/12
,那么你并没有旋转15°。相反,你应该使用pi*(1/12), pi*(2/12), pi*(3/12), ..., pi*(12/12)
。此外,“我无法使给定的答案起作用”这句话非常含糊。它似乎正好做到了你想要的,所以请向我们展示你是如何实现它的,以及为什么它不起作用。 - Wolfie