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

小白练手,敬请斧正。

Hough变换是一种图像处理技术,常用于在二维图像中寻找特定形状(如直线、圆等)的一维表示,尤其适合于检测直线。在MATLAB中,可以使用`imfindcircles`函数来进行直线检测,但它是针对边缘检测后的二值图像设计的。对于Hough变换检测直线,你可以使用`houghlines`函数。 以下是基本步骤: 1. **读取和预处理图像**:首先加载图像并转换成灰度图像,如果需要,可以进行滤波(例如高斯滤波)去除噪声。 ```matlab img = imread('your_image.jpg'); gray_img = rgb2gray(img); ``` 2. **边缘检测**:利用`edge`或`canny`函数提取图像的边缘信息。 ```matlab edges = edge(gray_img, 'Canny'); % 使用Canny算子 ``` 3. **进行Hough变换**:调用`houghlines`函数,传入边缘图作为输入,并设置参数,如角度范围和阈值。 ```matlab [H,theta,rho] = hough(edges); % Hough矩阵,theta角度,rho距离 ``` 4. **找出直线**:通过查找Hough矩阵中的极大值对(通常通过`accumarray`函数),找到潜在的直线,然后应用反变换得到实际的直线参数。 ```matlab peaks = houghpeaks(H, numLines, 'threshold', threshold_value); % numLines是直线的数量 lines = houghlines(edges, theta, rho, peaks); ``` 5. **显示结果**:最后可以在原始图像上绘制出检测到的直线。 ```matlab figure; imshow(img); hold on; for k = 1:length(lines) xy = [lines(k).point1; lines(k).point2]; % 获取两点坐标 plot(xy(:,1), xy(:,2), 'LineWidth', 2, 'Color', 'r'); % 绘制直线 end hold off; ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值