📄 houghlines.m
字号:
function lines = houghlines(f,theta,rho,rr,cc,fillgap,minlength)
%HOUGHLINES Extract line segments based on the Hough transform.
% LINES = HOUGHLINES(F,THETA,RHO,RR,CC,FILLGAP,MINLENGTH) extract line
% segments in the image F associated with particular bins in a Hough
% transform.THETA and RHO are vectors returned by function HOUGH. Vectors
% RR and CC specify the rows columns of the Hough transform bins tu use
% in searching for line segments. If HOUGHLINES finds two line segments
% associated with the same Hough transform bin that are separated by less
% than FILLGAP pixels, HOUGHLINES merges them into a single line
% segment.FILLGAP defaults to 20 if omitted.Merged line segments less
% than MINLENGTH pixels long are discarded.MINLENGTH defaults to 40 if
% omitted.
%
% LINES is a structure array whose length equals the number of merged
% line segments found. Each element of the structure array has these
% fields:
%
% point1 End-point of the line segment;two-element vector
% point2 End-point of the line segment;two-element vector
% length Distance between point1 and point2
% theta Angle (in degrees) of the Hough transform bin
% rho Rho-Axis position of the Hough transform bin
% 采取下面策略:
% 1、将像素位置旋转90-o,以便它们大概位于一条垂直线上。
% 2、按旋转的x值来对这些像素位置分排列。
% 3、使用函数diff找到裂口。忽视掉小裂口;这将合并被小空白分离的相邻线段。
% 4、返回比最小阈值长的线段的信息。
if nargin < 6
fillgap = 20;
end
if nargin < 7;
minlength = 40;
end
numlines = 0;lines=struct;
for k = 1:length(rr)
rbin = rr(k);cbin = cc(k);
%Get all pixels associated with Hough transform cell.
[r,c] = houghpixels(f,theta,rho,rbin,cbin);
if isempty(r)
continue
end
%Rotate the pixel locations about (1,1) so that they lie approximately along a vertical line.
omega = (90-theta(cbin))*pi/180;
T = [cos(omega) sin(omega);-sin(omega) cos(omega)];
xy = [r-1 c-1]*T;
x=sort(xy(:,1));
% Find the gaps larger than the threshold.
diff_x = [diff(x);Inf];
idx = [0; find(diff_x>fillgap)];
for p = 1:length(idx)-1;
x1 = x(idx(p)+1);x2=x(idx(p+1));
linelength = x2-x1;
if linelength >= minlength
point1 = [x1 rho(rbin)];point2 = [x2 rho(rbin)];
%Rotate the end-point locations back to the original angle.
Tinv = inv(T);
point1 = point1*Tinv; point2 = point2*Tinv;
numlines = numlines + 1;
lines(numlines).point1 = point1+1;
lines(numlines).point2 = point2+1;
lines(numlines).length = linelength;
lines(numlines).theta = theta(cbin);
lines(numlines).rho = rho(rbin);
end
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -