主要内容

houghlines

基于霍夫变换提取线段

说明

lines = houghlines(BW,theta,rho,peaks) 提取图像 BW 中与霍夫变换中的特定 bin 相关联的线段。thetarho 是函数 hough 返回的向量。peaks 是由 houghpeaks 函数返回的矩阵,其中包含霍夫变换 bin 的行和列坐标,用于搜索线段。返回值 lines 包含有关提取的线段的信息。

示例

lines = houghlines(BW,theta,rho,peaks,Name=Value) 使用名称-值参量来控制线条提取的各个方面。

示例

示例

全部折叠

将图像读入工作区。

I  = imread('circuit.tif');

旋转图像。

rotI = imrotate(I,33,'crop');

创建二值图像。

BW = edge(rotI,'canny');

使用二值图像创建霍夫变换。

[H,T,R] = hough(BW);
imshow(H,[],'XData',T,'YData',R,...
            'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;

Figure contains an axes object. The axes object with xlabel theta, ylabel rho contains an object of type image.

查找图像的霍夫变换中的峰值。

P  = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
x = T(P(:,2)); y = R(P(:,1));
plot(x,y,'s','color','white');

Figure contains an axes object. The axes object with xlabel theta, ylabel rho contains 2 objects of type image, line. One or more of the lines displays its values using only markers

查找线条并对其绘图。

lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
figure, imshow(rotI), hold on
max_len = 0;
for k = 1:length(lines)
   xy = [lines(k).point1; lines(k).point2];
   plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');

   % Plot beginnings and ends of lines
   plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
   plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');

   % Determine the endpoints of the longest line segment
   len = norm(lines(k).point1 - lines(k).point2);
   if ( len > max_len)
      max_len = len;
      xy_long = xy;
   end
end

Figure contains an axes object. The hidden axes object contains 37 objects of type image, line. One or more of the lines displays its values using only markers

将最长的线段设为青色以突出显示。

plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');

Figure contains an axes object. The hidden axes object contains 38 objects of type image, line. One or more of the lines displays its values using only markers

输入参数

全部折叠

二值图像,指定为二维逻辑矩阵或二维数值矩阵。对于数值输入,任何非零像素都被视为 1 (true)。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

线条旋转角度,以度为单位,指定为数值矩阵。角度是在 x 轴和 rho 向量之间测量的角度。

数据类型: double

距坐标原点的距离,指定为数值矩阵。坐标原点是图像的左上角 (0,0)。

数据类型: double

霍夫变换 bin 的行和列坐标,指定为数值矩阵。

数据类型: double

名称-值参数

全部折叠

将可选参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但对各个参量对组的顺序没有要求。

示例: lines = houghlines(BW,T,R,P,MinLength=7) 指定最小行长度为 7 个像素。

如果使用的是 R2021a 之前的版本,请使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: lines = houghlines(BW,T,R,P,"MinLength",7);

与同一个霍夫变换 bin 相关联的两个线段之间的距离,指定为正数。当线段之间的距离小于指定值时,houghlines 函数会将这些线段合并为一条线段。

数据类型: double

最小线条长度,指定为正数。houghlines 丢弃短于指定值的线条。

数据类型: double

输出参量

全部折叠

检测到的线条,以结构体数组形式返回,其长度等于找到的合并线段数。结构体数组的每个元素都有以下字段:

字段

描述

point1

指定线段端点坐标的二元素向量 [X Y]

point2

指定线段端点坐标的二元素向量 [X Y]

theta

霍夫变换 bin 的角度(以度为单位)

rho

霍夫变换 bin 的 rho 轴位置

扩展功能

全部展开

版本历史记录

在 R2006a 之前推出

另请参阅

|