matlab 图像分割 hough 霍夫变换

客官°小女子只卖身不卖艺 2021-06-10 20:41 672阅读 0赞

在此之前一般要边缘检测。

(1)hough变换原理

平面坐标系上直线转换到极坐标系上一个点,平面直角坐标系上点在极坐标系上为一条直线。
这里写图片描述

(2)有关函数介绍

1. [H,theta,rho] = hough(BW,p,v)

H是变换到的hough矩阵。
theta和rho对应于矩阵每一列和每一行的ρ和θ值组成的向量。
p与v成对使用。p如果使用thetaresolution则v是θ轴方向上的单位区间的长度,可取(0,90)之间,默认为1;p如果使用rhoresolution 则v是ρ轴方向上的单位区间长度,可取(0,norm(size(BW))之间,默认为1。

2. peaks = houghpeaks(H,numpeak,p,v)

peaks是一个Q*2的矩阵,每行的两个元素分别是某一峰值的行列索引,Q为找到的峰值数目。
numpeak是寻找的峰值数目。
p和v成对使用。p如果使用threshold则v表示峰值的阈值,只有大于阈值的点才被认为可能是阈值,默认为0.5*max(H(:))。p如果是NHoodSize则表示每次检测出一个峰值后,v就指出该峰值周围需要清零的邻域信息,并以向量[M N] 形式输出。

3. lines = houghlines(BW,theta,rho,peaks,p,v)

lines是个结构数组,有point1(端点1),point2(端点2),theta,rho。
p和v成对使用。p如果使用fillgap则v表示同一幅图像中两条线的阈值,小于将会合并,默认20。p如果是minlength则v表示直线段最小长度阈值,默认40。
point1:两元素向量[r1, c1],指定了线段起点的行列坐标。
point2:两元素向量[r2, c2],指定了线段终点的行列坐标。
theta:与线相关的霍夫变换的以度计量的角度。
rho:与线相关的霍夫变换的ρ轴位置

(3).完整代码:

  1. RGB= imread('building.jpg');
  2. I = rgb2gray(RGB);
  3. BW = edge(I, 'canny');
  4. [H, T, R]=hough(BW, 'RhoResolution',0.5,'ThetaResolution',0.5);
  5. figure, imshow(imadjust(mat2gray(H)), 'XData', T, ...
  6. 'YData', R, 'InitialMagnification', 'fit');
  7. xlabel('\theta'), ylabel('\rho');
  8. axis on; axis normal; hold on;
  9. colormap(hot);
  10. peaks = houghpeaks(H, 15);
  11. figure, imshow(BW);
  12. hold on;
  13. lines = houghlines(BW, T, R, peaks, 'FillGap',25, 'MinLength',15);
  14. max_len = 0;
  15. for k=1:length(lines)
  16. xy = [lines(k).point1; lines(k).point2];
  17. plot(xy(:,1),xy(:,2),'LineWidth',3,'Color','b');
  18. plot(xy(1,1),xy(1,2),'x','LineWidth',3,'Color','yellow');
  19. plot(xy(2,1),xy(2,2),'x','LineWidth',3,'Color','red');
  20. len = norm(lines(k).point1 - lines(k).point2);
  21. if ( len > max_len)
  22. max_len = len;
  23. xy_long = xy;
  24. end
  25. end

取出图中最长两条线:

  1. I=imread('capture4.jpg');
  2. BW=im2bw(I);
  3. BW=edge(BW,'canny');
  4. [H,T,R] = hough(BW);
  5. imshow(H,[],'XData',T,'YData',R,...
  6. 'InitialMagnification','fit');
  7. xlabel('\theta'), ylabel('\rho');
  8. axis on, axis normal, hold on;
  9. P = houghpeaks(H,10,'threshold',ceil(0.3*max(H(:))));
  10. x = T(P(:,2)); y = R(P(:,1));
  11. plot(x,y,'s','color','white');
  12. lines = houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
  13. figure,imshow(BW), hold on;
  14. max_len = 0;
  15. for k = 1:length(lines)
  16. xy = [lines(k).point1; lines(k).point2]; %两点矩阵
  17. plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
  18. plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
  19. plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
  20. len = norm(lines(k).point1 - lines(k).point2); %两点距离
  21. Len(k)=len
  22. if ( len > max_len)
  23. max_len = len
  24. xy_long = xy
  25. end
  26. end
  27. plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','blue');
  28. [L1 Index1]=max(Len(:));
  29. Len(Index1)=0;
  30. [L2 Index2]=max(Len(:));
  31. x1=[lines(Index1).point1(1) lines(Index1).point2(1)];
  32. y1=[lines(Index1).point1(2) lines(Index1).point2(2)];
  33. x2=[lines(Index2).point1(1) lines(Index2).point2(1)];
  34. y2=[lines(Index2).point1(2) lines(Index2).point2(2)];
  35. K1=(lines(Index1).point1(2)-lines(Index1).point2(2))/(lines(Index1).point1(1)-lines(Index1).point2(1));
  36. K2=(lines(Index2).point1(2)-lines(Index2).point2(2))/(lines(Index2).point1(1)-lines(Index2).point2(1));
  37. hold on
  38. [m,n] = size(BW);
  39. BW1=zeros(m,n);
  40. b1=y1(1)-K1*x1(1);
  41. b2=y2(1)-K2*x2(1);
  42. for x=1:n
  43. for y=1:m
  44. if y==round(K1*x+b1)|y==round(K2*x+b2)
  45. BW1(y,x)=1;
  46. end
  47. end
  48. end
  49. for x=1:n
  50. for y=1:m
  51. if ceil(K1*x+b1)==ceil(K2*x+b2)
  52. y1=round(K1*x+b1)
  53. BW1(1:y1-1,:)=0;
  54. end
  55. end
  56. end
  57. figure,imshow(BW1);

发表评论

表情:
评论列表 (有 0 条评论,672人围观)

还没有评论,来说两句吧...

相关阅读

    相关 变换

    二维:图像处理中的霍夫变换,最开始用作直线检测 [霍夫变换(主要说明检测直线及圆的原理)][Link 1] [霍夫变换直线检测(Line Detection)原理及示例