www.gusucode.com > 《图像配准技术及其Matlab编程实现》--源码程序 > 《图像配准技术及其Matlab编程实现》/match/LineDetect.m

    function LineDetect(filename,direction)
%线检测
%参数:filename:被检测图像文件名
%     direction:检测直线的方向:取值为0、45、90、135时分别表示水平直线、斜率为1
%               的直线、垂直直线和斜率为-1的直线
%功能:从被检测图像中检测出指定方向的直线。
f=imread(filename);
figure,imshow(f);
[width,height]=size(f);
number=100;
h=zeros(width,height);
if number>width*height
    number=width*height
end
df=im2double(f);
switch direction
    case 0
        w=[-1 -1 -1;4 4 4;-1 -1 -1];
    case 45
        w=[-1 -1 4;-1 4 -1;4 -1 -1];
    case 90
        w=[-1 4 -1;-1 4 -1;-1 4 -1];
    case 135
        w=[4 -1 -1;-1 4 -1;-1 -1 4];
    otherwise
        w=[-1 -1 -1;4 4 4;-1 -1 -1];
end
g=imfilter(df,w);
g=abs(g)./4;
[data index]=sort(g(:));
T=data(width*height-number+1);
for i=1:width
    for j=1:height
        if g(i,j)>=T
            h(i,j)=1;
        end
    end
end
figure;imshow(h);

%linedetect('line.bmp',0)