Discuz! Board

用户名  找回密码
 立即注册
帖子
热搜: 活动 交友 discuz
查看: 1840|回复: 6

实验二:图像霍夫变换

[复制链接]

391

主题

1222

帖子

3902

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3902
发表于 2023-3-20 20:23:15 | 显示全部楼层 |阅读模式
  1. 霍夫变换
复制代码
回复

举报

391

主题

1222

帖子

3902

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3902
 楼主| 发表于 2023-3-24 21:56:40 | 显示全部楼层
回复

举报

391

主题

1222

帖子

3902

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3902
 楼主| 发表于 2023-3-25 06:51:38 | 显示全部楼层
  1. %Matlab 2012以上支持
复制代码
回复

举报

391

主题

1222

帖子

3902

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3902
 楼主| 发表于 2023-3-27 20:30:58 | 显示全部楼层
  1. function [h, theta, rho] = hough(I_bw)
  2. % h: 霍夫变换矩阵
  3. %theta:角度的取值范围  -90:90
  4. %rho  :-  图像对角线长度:+图像对角线长度
  5. theta = -90:90;
  6. [M,N] = size(I_bw);
  7. D = round(sqrt(M^2 + N^2)/2);
  8. rho = -D:D;
  9. h = zeros(2*D+1,181);
  10. for i = 1:M
  11.     for j = 1:N
  12.         if I_bw(i,j) == 1
  13.             % rho = x*cos(theta) + y*sin(theta)
  14.             %从原图像的行列值换算到xy坐标
  15.             x = -N/2 + j;
  16.             y = M/2 - i;
  17.             %从原图像矩阵映射到霍夫坐标系
  18.             rho = round(x*cos(theta/180*pi) + y*sin(theta/180*pi));
  19.             %从霍夫坐标系到霍夫矩阵
  20.             i_h = D - rho;
  21.             j_h = 91 + theta;
  22.             for ii = 1:181
  23.                h(i_h(ii),j_h(ii)) = h(i_h(ii),j_h(ii))+1;
  24.             end
  25.         end
  26.     end
  27. end
复制代码
回复

举报

391

主题

1222

帖子

3902

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3902
 楼主| 发表于 2023-3-27 20:53:38 | 显示全部楼层
Matlab7主函数
  1. <font style="background-color: rgb(255, 255, 255);">clc
  2. clear all%读图显示
  3. I = imread('line.jpg');
  4. subplot(2,2,1); imshow(I);
  5. %灰度化
  6. I_gray = rgb2gray(I);
  7. subplot(2,2,2); imshow(I_gray);
  8. %二值化
  9. I_bw = I_gray<200;
  10. subplot(2,2,3);imshow(I_bw,[]);
  11. %hough变换
  12. [H,theta,rho] = hough1(I_bw);
  13. %画图
  14. [m_H,n_H] = find(H == max(max(H)));
  15. % rho = x cos(theta)+ ysin(theta);
  16. % y =(rho-x* cos(theta))/sin(theta);
  17. [m,n] = size(I_bw);
  18. x = 0: n;
  19. y = (rho(m_H) - x* cos(theta(n_H)/180*pi))/sin(theta(n_H)/180*pi);
  20. subplot(2,2,4); imshow(I_gray);
  21. hold on ;
  22. plot (x,y,'-r');
  23. axis([0,n,0,m]);</font>
复制代码
回复

举报

391

主题

1222

帖子

3902

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3902
 楼主| 发表于 2023-3-28 13:47:12 | 显示全部楼层
Matlab7 霍夫变换重写
  1. function [h, theta, rho] = hough1(I_bw)
  2. %图像坐标系的定义:左上角为原点,横x纵y
  3. %pho-theta坐标系的定义: 中心
  4. %霍夫矩阵
  5. % h: 霍夫变换矩阵
  6. %theta:角度的取值范围  -90:90
  7. %rho  :-  图像对角线长度:+图像对角线长度
  8. theta = -90:89;
  9. [M,N] = size(I_bw);
  10. D = round(sqrt(M^2 + N^2));
  11. rho = -D+1:D-1;
  12. h = zeros(2*D-1,180);
  13. for i = 1:M
  14.     for j = 1:N
  15.         if I_bw(i,j) == 1
  16.             % rho = x*cos(theta) + y*sin(theta)
  17.             %从原图像的行列值换算到xy坐标
  18.             x =  j;
  19.             y =  i;
  20.             %从原图像矩阵映射到霍夫坐标系
  21.             rho0 = round(x*cos(theta/180*pi) + y*sin(theta/180*pi));
  22.             %从霍夫坐标系到霍夫矩阵
  23.             i_h = D + rho0;
  24.             j_h = 91 + theta;
  25.             for ii = 1:180
  26.                h(i_h(ii),j_h(ii)) = h(i_h(ii),j_h(ii))+1;
  27.             end
  28.         end
  29.     end
  30. end
复制代码
回复

举报

391

主题

1222

帖子

3902

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3902
 楼主| 发表于 2024-4-16 21:11:51 | 显示全部楼层
clc;clear all;
I = imread('abc.png');
I_gray = rgb2gray(I);
I_bw = I_gray<50;
subplot(1,2,1);imshow(I_bw);
%扫描xy坐标系的每一个点,对应着rho-theta坐标系一条曲线
[m,n] = size(I_bw);
n_theta = 100;
step_theta = pi/n_theta;
thetas = -pi/2:step_theta:pi/2;
step_rho = 5;
N = n_theta+1;
M = 2*(m+n)+1;
I_hough = zeros(M,N);
for i = 1:m
    i
    for j = 1:n
        %对于每个theta都有一个rho值
        for theta  = thetas
            i_theta = int8(theta/step_theta);   
            rho  = j*cos(theta)+i*sin(theta);
            i_rho = round(rho/step_rho);
            J = i_theta + n_theta/2+1;
            I = -i_rho + (M+1)/2;
            I_hough(I,J) = I_hough(I,J) + 1;            
        end
    end
end
subplot(1,2,2);imshow(imresize(uint8(I_hough),[(m+n+1)*5,404]));
回复

举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-4-10 21:53 , Processed in 0.039429 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表