此代码查找线的共线组.
theta = zeros(length(lines),1);
rho = zeros(length(lines),1);
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',1,'Color','green');
plot(xy(1,1),xy(1,2),'x','LineWidth',1,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',1,'Color','red');
%text(xy(1,1),xy(1,2),[' ' num2str(k)],'fontsize',10,'color',[1 1 1]);
theta(k) = lines(k).theta;
rho(k) = lines(k).rho;
end
theta_tolerance = 2;
theta2 = abs(bsxfun(@minus, theta, theta')) <= theta_tolerance;
theta2(1:size(theta2,2)+1:numel(theta2)) = 0; % zero diagonal
rho_tolerance = 1;
rho2 = abs(bsxfun(@minus, rho, rho')) <= rho_tolerance;
rho2(1:size(rho2,2)+1:numel(rho2)) = 0; % zero diagonal
rhotheta2 = sparse(rho2 & theta2);
[nc, C] = graphconncomp(rhotheta2);
paired = ismember(C,find(hist(C,1:max(C))>1)); % paired lines
colors=get(gcf,'DefaultAxesColorOrder');
for line=find(paired)
xy = [lines(line).point1; lines(line).point2];
plot(xy(:,1),xy(:,2),':','LineWidth',4,'Color',colors(C(line),:));
text(xy(1,1),xy(1,2),num2str(C(line)),'fontsize',20,'Color',colors(C(line),:));
end
它没有照顾重叠.目前尚不清楚如何处理具有3个共线段的情况,其中第一段和第二段相交?您是否需要同时考虑包含第一个和第三个的集以及包含第二个和第三个段的集合?