matlab 霍夫變換(hough) 檢測直線


  霍夫變換是一種特征檢測(feature extraction),被廣泛應用在圖像分析(image analysis)、電腦視覺 (computer vision)以及數位影像處理 (digital image processing)。 霍夫變換是用來辨別找出物件中的特征,例如:線條。他的算法流程大致如下,給定一個物件、要辨別的形狀的種類,算法會在參數空間(parameter space)中執行投票來決定物體的形狀, 而這是由累加空間(accumulator space)里的局部最大值(local maximum)來決定。  

  Hough變換的基本原理在於,利用點與線的對偶性,將圖像空間的線條變為參數空間的聚集點,從而檢測給定圖像是否存在給定性質的曲線。

clc;clear ; close 
set(0,'defaultfigurecolor',[1,1,1]) 
load DATA2.mat
data = D1;
BW = data;

figure(1)
imshow(BW)
title('原圖像');
figure(2) subplot 211; %%進行霍夫變換 [H, theta , rho] = hough (BW); %%繪制霍夫空間 imshow(imadjust(mat2gray(H)),[],'XData',theta,'YData',rho,... 'InitialMagnification','fit'); xlabel('\theta (degrees)'), ylabel('\rho'); axis on, axis normal, hold on; colormap(hot); title('霍夫空間') %%峰值 P = houghpeaks(H,5,'threshold',0.5*max(H(:))); x = theta(P(:,2)); y = rho(P(:,1)); plot(x,y,'s','color','black'); %lines = houghlines(BW,theta,rho,P,'FillGap',10,'MinLength',10); lines = houghlines(BW,theta,rho,P,'FillGap',10,'MinLength',10); subplot 212 imshow(BW) ,hold on max_len = 0; count = 1; points = zeros(2,2); for k = 1:length(lines) points(count,1) = lines(k).point1(1); points(count,2) = lines(k).point1(2); count =count +1; points(count,1) = lines(k).point2(1); points(count,2) = lines(k).point2(2); count =count +1; 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'); end title('直線檢測');

  效果如下:

 


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM