www.gusucode.com > vision 源码程序 matlab案例代码 > vision/DetectLongestLineInAnImageExample.m

    %% Detect Longest Line In An Image

% Copyright 2015 The MathWorks, Inc.


%% Read the intensity image.
    I = imread('circuit.tif');
    
%% Create an edge detector, Hough transform, local maxima finder, and Hough lines objects.
    hedge = vision.EdgeDetector;
    hhoughtrans = vision.HoughTransform(pi/360,'ThetaRhoOutputPort', true);
    hfindmax = vision.LocalMaximaFinder(1,	'HoughMatrixInput', true);
    hhoughlines = vision.HoughLines('SineComputation','Trigonometric function');
    
%% Find the edges in the intensity image
 	BW = step(hedge, I);

%% Run the edge output through the transform
	[ht, theta, rho] = step(hhoughtrans, BW);

%% Find the location of the max value in the Hough matrix.
 	idx = step(hfindmax, ht);

%% Find the longest line.
 	linepts = step(hhoughlines, theta(idx(1)-1), rho(idx(2)-1), I);
 
%% View the image superimposed with the longest line.
   imshow(I); hold on;
   line(linepts([1 3])-1, linepts([2 4])-1,'color',[1 1 0]);