@ [toc]
一、hough函數
1、語法
[H,theta,rho] = hough(BW)
[H,theta,rho] = hough(BW,Name,Value,...)
詳細說明:
** [H,theta,rho] =霍夫(BW)**計算二值圖像BW的標準霍夫變換(SHT)。 該函數返回rho(沿垂直於線的矢量從原點到直線的距離)和theta(x軸與該矢量之間的角度,以度爲單位)。該函數返回函數還返回標準Hough變換H,它是[H,theta,rho] =霍夫(BW,名稱,值,…)計算二值圖像BW的標準霍夫變換(SHT),其中指定的參數影響計算。
可選的參數有
參數 | 主題 |
---|---|
‘RhoResolution’ | Hough變換沿rho軸的bin的間距,交替爲1 |
‘Theta’ | 輸出矩陣H的對應列的Theta值,其中包含’Theta’和[-90,90)範圍內的實數數值向量,最小-90:89 |
2,舉個栗子
RGB = imread('septagon.tif');
I = im2double(RGB);
BW = edge(I,'canny');
figure, imshow(BW);
[H, theta, rho] = hough(BW, 'RhoResolution', 0.5, 'Theta', -90:0.2:89);
figure
imshow(H,[],'XData',theta,'YData',rho,'InitialMagnification','fit');
title('霍夫變換圖'), xlabel('\theta'), ylabel('\rho');
axis on , axis normal
hold on
- 代碼的第6行設置了ρ和θ的分辨率
- 代碼的第9行imshow函數裏面,
[]
表示將結果標定化(即用H除以H的最大值),設置x和y軸爲theta和rho,'InitialMagnification','fit'
的目的是顯示H的時候讓長寬自適應的調整
結果如下:可以發現,參數空間中含有很多正弦曲線
查看rho、theta和H的矩陣大小:
>> size(rho)
ans =
1 523
>> size(theta)
ans =
1 180
>> size(H)
ans =
523 180
二、houghpeaks函數
聽名字就知道這個函數是尋找霍夫變換得到的結果中的一些極大值
1、語法
peaks = houghpeaks(H,numpeaks)
peaks = houghpeaks(H,numpeaks,Name,Value)
詳細說明
(1)peaks = houghpeaks(H,numpeaks) locates peaks in the Hough transform matrix, H, generated by the hough function. numpeaks specifies the maximum number of peaks to identify. The function returns peaks a matrix that holds the row and column coordinates of the peaks.
(2)peaks = houghpeaks(H,numpeaks,Name,Value) controls aspects of the operation using name-value pair arguments.
參數說明
參數 | 含義 |
---|---|
H | 霍夫變換得到的矩陣 |
numpeaks | 限定要識別的峯值的個數,默認值爲1 |
‘Threshold’ | 可被認爲是峯值的最小值,比如0.2*max(max(H)) |
2、舉個栗子
# 接上上面的代碼
% Find peaks in the Hough transform of the image.
P = houghpeaks(H,8,'threshold', ceil(0.3*max(H(:))));
x = theta(P(:,2));
y = rho(P(:,1));
plot(x,y,'s','color','white');
- 這裏將threshold設置爲0.3倍的最大值,目的是防止很短的直線也被檢測出來,影響視覺效果
- 繪圖的時候用方塊square標出返回的peaks在參數平面中的位置(注意P的第1列表示ρ,第二列表示θ)
結果如下:
查看返回的peaks矩陣:
P =
442 142
303 33
189 32
325 151
317 102
410 139
240 23
三、houghlines函數
houghlines函數基於hough變換來提取線段
1、語法
lines = houghlines(BW,theta,rho,peaks)
lines = houghlines(___,Name,Value,...)
說明
(1) lines = houghlines(BW,theta,rho,peaks) 提取圖像 BW 中與 Hough 變換中的特定 bin 相關聯的線段。theta 和 rho 是函數 hough 返回的向量。peaks 是由 houghpeaks 函數返回的矩陣,其中包含 Hough 變換 bin 的行和列座標,用於搜索線段。返回值 lines 是結構體數組,其長度等於找到的合併後的線段數。
(2)lines = houghlines(___,Name,Value,…) 提取圖像 BW 中的線段,其中指定的參數影響運算。
參數說明
參數 | 含義 |
---|---|
‘FillGap’ | 與同一 Hough 變換 bin 相關聯的兩個線段之間的距離,默認值爲20,當線段之間的距離小於指定值時,houghlines 函數會將這些線段合併爲一條線段 |
‘MinLength’ | 最小線條長度,指定爲正實數標量。houghlines 丟棄短於指定值的線條,double類型 |
返回值
lines
爲找到的線條,以結構體數組形式返回,其長度等於找到的合併線段數。結構體數組的每個元素都有以下字段:
2、舉個栗子
lines = houghlines(BW,theta,rho,P,'FillGap',10,'MinLength',5);
查看lines的返回值:
>> lines
lines =
1x7 struct array with fields:
point1
point2
theta
rho
>> size(lines)
ans =
1 7
>> lines.point1
ans =
414 591
ans =
404 61
ans =
144 426
ans =
191 183
ans =
192 181
ans =
539 312
ans =
539 312
lines爲結構體類型,包含四個矩陣,其中point1爲線段的起始點座標,point2爲線段的終點座標,theta和rho就是hough得到的theta和rho
四、繪製線段
% Find lines and plot them.
lines = houghlines(BW,theta,rho,P,'FillGap',10,'MinLength',5);
figure, imshow(RGB), hold on
max_len = 0;
for k = 1 : length(lines)
xy = [lines(k).point1; lines(k).point2]; % k=1, xy=[539 312; 685 374]
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
%通過將最長的線段着色爲青色突出顯示。
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');
結果如下:
霍夫變換的原理可以參考 小白學習圖像處理7——Hough變換檢測直線
hough變換部分完結〜