好吧,由于限制,仅检测平行于 y 轴的线,不需要完整的霍夫变换。只需将所有点投影到 x 轴上,累积它们并找到峰值。
%assuming you want a resolution of 0.01 covering x space from 0 to 1
points = rand(1000,2);
figure(1);
plot(points(:,1),points(:,2),'b.');
minX = 0;
maxX = 1;
resolution = 0.01;
xValues = minX:resolution:maxX;
accu = zeros(1,length(xValues));
for i = 1:length(points)
xVal = points(i,1); % access x value of point;
idx = round(((xVal-minX)/resolution))+1;
accu(idx) = accu(idx) +1;
end
现在您有了一个累加器,您可以在其中搜索最大值。
[pks,idx] = findpeaks(accu);
您可能只想考虑至少有minPoints 点的峰:
minPoints = 10;
idx = idx(pks>minPoints);
然后你可以进一步处理这行:
for i = 1:length(idx)
% select all points corresponding to line:
idc = abs(points(:,1)-xValues(idx(i))) < resolution/2;
pointsOnLine = points(idc,:);
figure(1);
hold on;
plot(pointsOnLine(:,1),pointsOnLine(:,2),'ro');
minY = min(pointsOnLine(:,2));
maxY = max(pointsOnLine(:,2));
plot([xValues(idx(i)),xValues(idx(i))],[minY,maxY],'r-');
end
要删除有大间隙的线,您可以使用sort()根据它们的y值对点进行排序,然后使用diff找到大的跳跃。