hough变换检测直线 matlab,Hough变换直线检测MatLab代码 | 学步园

function Img_hough = hough_s(Img, bw)

%该函数实现hough变换提取直线的功能。

%输入图像x,运行之后直接画出直线。

%选择进行Hough变换的图像行

%Img为原图像;bw为边缘图像

%%

[H,W,D]=size(Img);

Img_hough = Img;

if D==1

channel = Img_hough;

Img_hough = cat(3,channel, channel, channel);

end

[M,N]=size(bw);

%求出图像大小。

%%

dtheta=1;

drho=1;

md=ceil((N+round(sqrt(M^2+N^2)))/drho);

%确定网格的最大区域。

ma=ceil(180/dtheta);

numrhotheta=zeros(md,ma);

%产生计数矩阵。

coordrhotheta=cell(1,1);

% para=cell(1,3);

%cell数组相当于c语言中的指针,可动态的改变大小。

for i=1:md

for j=1:ma

coordrhotheta{i,j}=[];

end

end

%产生空网格。

ymin = 5;

ymax = M - 4;

for i=ymin:ymax

for j=1:N

if bw(i,j) == 1

for k=1:ma

rho=round((j*cos(dtheta*k*pi/180)+i*sin(dtheta*k*pi/180))/drho);

%根据直线的法线式表示,计算出平面上不同点的hough变换值。

rho=rho+ceil(N/drho);%可能的最大负值。

numrhotheta(rho+1,k)=numrhotheta(rho+1,k)+1;

%将hough变换值相应位置的计数值加1。

coordrhotheta{rho+1,k}=[coordrhotheta{rho+1,k};[i,j]];

%记录hough变换值相应位置对应的点的坐标。

end

end

end

end

%%

figure;imshow(Img);

hold on

num = 8;

for i=1 : num

[y1,col1]=max(numrhotheta);

[y2,col]=max(y1);

row=col1(col);

%求出hough变换值最大值的坐标。

numrhotheta(row,col)=0;

%为了避免重复计算,将计算过的点置0。

rhood=1;

chood=0;

top=max(row-rhood,1);

down=min(row+rhood,md);

left=max(col-chood,1);

right=min(col+chood,ma);

numrhotheta(top:down,left:right)=0;

%     nds=coordrhotheta{row,col};

nds = [];

for r = top : down

for c = left : right

nds = [nds; coordrhotheta{r, c}];

end

end

Img_hough=draw(Img_hough, nds);

end

imwrite(mat2gray(numrhotheta),'numrhotheta.bmp')

效果图如下:8条主要直线

Img_hough.JPG

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值