Hough变换提取直线——Matlab实现

Hough变换提取直线——Matlab实现

%用Hough变换提取直线
%在对图形进行直线检测之前,需要对图形进行边缘检测、二值化处理,用拉普拉斯算法或canny提取边缘
%Hough变换思想
%1.在ρ、θ的极值范围内对其分别进行 m,n 等分,设一个二维数组的下标与 ρi 、 θj 的取值 对应;
%2.对图像上的所有边缘点作 Hough 变换,求每个点在 θj (j=01,…,n)Hough 变换后的 ρi,判断( ρi 、 θj )与哪个数组元素对应,则让该数组元素值加 1%3.比较数组元素值的大小,最大值所对应的( ρi 、 θj )就是这些共线点对应的直线方程的参数。 
close all;clear;clc;
a=imread('fw6.tif');
figure;
a=a(:,:,1);
subplot(221);
imshow(a); 
bw1=LapuLas(a);%调用自己编写的拉普拉斯算法进行边缘检测
subplot(222);
imshow(bw1);
%bw1=edge(a,'canny',0.2);
%imshow(bw1);
subplot (223);
[H,theta,rho]=naiveHough(bw1);%调用自己编写的Hough函数,H为Hough变换矩阵,theta和rho为霍夫变换的角度和半径
imshow(H,[],'XData',theta,'YData',rho, 'InitialMagnification','fit');%画出Hough空间
xlabel('\theta (degrees)'), ylabel('\rho');
axis on, axis square, hold on;
P = houghpeaks(H,6);%提取6个极值点
x = theta(P(:,2));
y = rho(P(:,1));
plot(x,y,'s','color','red');%标出极值点
title('Hough空间');
lines = houghlines(bw1,theta,rho,P);%提取线段
subplot(224)
imshow(a),hold on;%为了在原图上画出直线段
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
 plot(xy(:,1),xy(:,2),'LineWidth',1,'Color','green');%画出线段
plot(xy(1,1),xy(1,2),'o','LineWidth',1,'Color','yellow');%起点
plot(xy(2,1),xy(2,2),'o','LineWidth',1,'Color','red');%终点
end
%Hough变换函数
function [ Hough, theta_range, rho_range ] = naiveHough(I)
[rows, cols] = size(I);
theta_maximum = 90;
rho_maximum = floor(sqrt(rows^2 + cols^2)) - 1;
theta_range = -theta_maximum:theta_maximum - 1;
rho_range = -rho_maximum:rho_maximum;
Hough = zeros(length(rho_range), length(theta_range));
for row = 1:rows
    for col = 1:cols
        if I(row, col) > 0 %only find: pixel > 0
            x = col - 1;
            y = row - 1;
            for theta = theta_range
                rho = round((x * cosd(theta)) + (y * sind(theta)));  
                rho_index = rho + rho_maximum + 1;
                theta_index = theta + theta_maximum + 1;
                Hough(rho_index, theta_index) = Hough(rho_index, theta_index) + 1;
            end
        end
    end
end
end
%拉普拉斯边缘提取
function [p] = LapuLas(e)
r=e;
[m,n]=size(e);
%图像二值化后,拉普拉斯算法提取边缘
dd=sum(sum(e)/(m*n));
for x=1:m
    for y=1:n
        if(r(x,y)>=dd)
            r(x,y)=255;
        else
            r(x,y)=0;
        end
    end
end
subplot(2,2,3),imshow(r);title('二值化后图像');
p=zeros(m-1,n-1);
for ii=2:m-1
    for jj=2:n-1
        p(ii,jj)=r(ii,jj+1)+r(ii,jj-1)+r(ii+1,jj)+r(ii-1,jj)-4*(r(ii,jj));
    end
end
p=uint8(p);
end

小白练手,敬请斧正。

  • 6
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值