www.gusucode.com > IPCV_Eval_Kit_R2019a_0ce6858工具箱matlab程序源码 > IPCV_Eval_Kit_R2019a_0ce6858/code/demo_files/I2_09_2_houghTransform.m

    %% 僴僼曄姺 僨儌
clear;clc;close all;imtool close all

%% 屒棫揰偺惗惉
BW = false(40);
BW(20,15)=1;
figure;imshow(BW);

%% 屒棫揰偵懳偡傞Hough曄姺丒昞帵
%      H:     僴僼曄姺峴楍
%      theta: x幉抣 : 妏搙(亱)   -90亱乣89亱  惓偺X幉偵懳偟偰帪寁夞傝偵掕媊
%      rho  : y幉抣 : 尨揰偐傜偺嫍棧
[H,theta,rho] = hough(BW);
figure, imshow(imadjust(mat2gray(H)),[],'XData',theta,'YData',rho,...
        'InitialMagnification','fit');
xlabel('\theta (degrees)'), ylabel('\rho');
axis on; axis normal         % 嵗昗昞帵
colormap(hot)

%% 3揰偵懳偡傞Hough曄姺\帵
BW(22,17)=1;
BW(24,19)=1;
figure;imshow(BW);

%% Hough曄姺丒昞帵
%      H:     僴僼曄姺峴楍
%      theta: x幉抣 : 妏搙(亱)   -90亱乣89亱  惓偺X幉偵懳偟偰帪寁夞傝偵掕媊
%      rho  : y幉抣
[H,theta,rho] = hough(BW);
figure, imshow(imadjust(mat2gray(H)),[],'XData',theta,'YData',rho,...
        'InitialMagnification','fit');
xlabel('\theta (degrees)'), ylabel('\rho');
axis on; axis normal         % 嵗昗昞帵
colormap(hot)

%% 娭悢 houghpeaks 傪巊偭偰僴僼曄姺峴楍 H 撪偺僺乕僋揰傪専弌丒僾儘僢僩
peak = houghpeaks(H)
hold on
   % 僺乕僋偺theta偲rho抣傪庢摼偟偰巐妏傪Plot
plot(theta(peak(:,2)), rho(peak(:,1)), 's','color','red','MarkerFaceColor','red');
hold off

%% 娭悢 houghlines 傪巊偭偰僀儊乕僕撪偺慄傪専弌丅
%    lines丗 point1, point2, theta, rho
%    摨堦捈慄忋偱丄慄暘娫偺嫍棧偑巜掕抣(5)傛傝傕彫偝偄応崌2偮偺慄暘傪侾偮偵寢崌
lines = houghlines(BW, theta, rho, peak, 'FillGap', 5, 'MinLength', 3);
% 尦偺僀儊乕僕偵廳偹偰慄傪僾儘僢僩偟傑偡丅
xy = [reshape([lines.point1],2,[]); reshape([lines.point2],2,[])]';
BW1 = insertShape(uint8(BW), 'line', xy,'SmoothEdges',false);
imtool(BW1);

[lines.rho]          % 尨揰偐傜捈慄傑偱偺嫍棧
[lines.theta]        % 悅慄偺妏搙 (亱)



%% LSI偺幨恀偺夝愅 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;clc;close all;imtool close all            % 弶婜壔

I  = imread('circuit.tif');
rotI = imrotate(I,33,'crop');
figure; imshow(rotI);

%% 僄僢僕専弌
BW = edge(rotI,'canny');
figure, imshow(BW);

%% Hough曄姺丒昞帵
%      H:     僴僼曄姺峴楍
%      theta: x幉抣 : 妏搙(亱)   -90亱乣89亱  惓偺X幉偵懳偟偰帪寁夞傝偵掕媊
%      rho  : y幉抣
[H,theta,rho] = hough(BW);

figure, imshow(imadjust(mat2gray(H)),[],'XData',theta,'YData',rho,...
        'InitialMagnification','fit');
xlabel('\theta (degrees)'), ylabel('\rho');
axis on; axis normal         % 嵗昗昞帵
colormap(hot)

%% 娭悢 houghpeaks 傪巊偭偰僴僼曄姺峴楍 H 撪偺僺乕僋揰傪専弌(5屄)丒僾儘僢僩
%    嵟戝抣偺0.3偵枮偨側偄傕偺偼僺乕僋偲偟側偄
%    僺乕僋偺揰偺theta偲rho偺斣崋慻(慄暘偑愗傟偰偄傞壜擻惈 => 5偮偲偼尷傜側偄)
hold on
peak = houghpeaks(H, 5, 'threshold', ceil(0.3*max(H(:))));
plot(theta(peak(:,2)), rho(peak(:,1)), 's','color','black','MarkerFaceColor','green');   % theta偲rho偺斣崋偐傜抣傪庢摼偟偰巐妏傪Plot
hold off

%% 娭悢 houghlines 傪巊偭偰僀儊乕僕撪偺慄傪専弌偟傑偡丅
%    lines丗 point1, point2, theta, rho
%    摨堦捈慄忋偱丄慄暘娫偺嫍棧偑巜掕抣(5)傛傝傕彫偝偄応崌2偮偺慄暘傪侾偮偵寢崌
lines = houghlines(BW, theta, rho, peak, 'FillGap', 5, 'MinLength', 7);

% 尦偺僀儊乕僕偵廳偹偰慄傪僾儘僢僩偟傑偡丅
figure, imshow(rotI), hold on
for k = 1:length(lines)
   %   xy : [x1 y1; x2 y2]
   xy = [lines(k).point1; lines(k).point2];
   plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
end
[lines.theta]
hold off

%% 廔椆






% 
% 
%    % 巒揰廔揰偵儅乕僋昁梫側偲偒丗for撪偵埲壓傪擖傟傞
%    plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
%    plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
% 
%    % 堦斣挿偄慄傪愒偔昞帵昁梫側偲偒
%     %for偺慜偵
%          max_len = 0;
%    % 壓婰傪for撪
%          len = norm(lines(k).point1 - lines(k).point2);
%          if ( len > max_len)
%            max_len = len;
%            xy_long = xy;
%          end
%   %  for 偺屻偵
%          % 堦斣挿偄慄傪丄愒偱昞帵
%          plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','red');
   
   


%% LSI偺幨恀偺夝愅 (戝偒偄夋憸丗娭悢偱張棟偟偨応崌)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;clc;close all;imtool close all            % 弶婜壔

I  = imread('circuit.tif');
rotI = imrotate(I,33,'crop');
figure; imshow(rotI);

%% 僄僢僕専弌
BW = edge(rotI,'canny');
BW = imresize(BW,10,'bicubic');
figure, imshow(BW);

%% Hough曄姺丒昞帵 %%%%%%%%%%%%%%
%      H:     僴僼曄姺峴楍
%      theta: x幉抣 : 妏搙(亱)   -90亱乣89亱  惓偺X幉偵懳偟偰帪寁夞傝偵掕媊
%      rho  : y幉抣
[H,theta,rho] = hough(BW);

figure, imshow(imadjust(mat2gray(H)),[],'XData',theta,'YData',rho,...
        'InitialMagnification','fit');
xlabel('\theta (degrees)'), ylabel('\rho');
axis on; axis normal         % 嵗昗昞帵
colormap(hot)

%% 娭悢 houghpeaks 傪巊偭偰僴僼曄姺峴楍 H 撪偺僺乕僋揰傪専弌(5屄)丒僾儘僢僩
%    嵟戝抣偺0.3偵枮偨側偄傕偺偼僺乕僋偲偟側偄
%    僺乕僋偺揰偺theta偲rho偺斣崋慻(慄暘偑愗傟偰偄傞壜擻惈 => 5偮偲偼尷傜側偄)
hold on
peak = houghpeaks(H, 5, 'threshold', ceil(0.3*max(H(:))));
plot(theta(peak(:,2)), rho(peak(:,1)), 's','color','black','MarkerFaceColor','green');   % theta偲rho偺斣崋偐傜抣傪庢摼偟偰巐妏傪Plot
hold off

%% 娭悢 houghlines 傪巊偭偰僀儊乕僕撪偺慄傪専弌偟傑偡丅
%    lines丗 point1, point2, theta, rho
%    摨堦捈慄忋偱丄慄暘娫偺嫍棧偑巜掕抣(5)傛傝傕彫偝偄応崌2偮偺慄暘傪侾偮偵寢崌
lines = houghlines(BW, theta, rho, peak, 'FillGap', 5, 'MinLength', 7);

% 尦偺僀儊乕僕偵廳偹偰慄傪僾儘僢僩偟傑偡丅
figure, imshow(BW), hold on
for k = 1:length(lines)
   %   xy : [x1 y1; x2 y2]
   xy = [lines(k).point1; lines(k).point2];
   plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
end
[lines.theta]
hold off

%% 廔椆

% Copyright 2014 The MathWorks, Inc.