Hello Mat

 找回密码
 立即注册
查看: 3507|回复: 0

canny滤波器

[复制链接]

1298

主题

1524

帖子

114

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22653
发表于 2017-3-8 23:28:02 | 显示全部楼层 |阅读模式
  1. function canny_im = canny_fspecial(im,type)
  2.     if nargin < 2
  3.         type = 'canny';
  4.     end
  5.     if ~isa(im,'double')
  6.         im = double(im)/255;
  7.     end
  8.    switch type
  9.        case 'canny'  % canny filter

  10.     r=im(:,:,1);g=im(:,:,2);b=im(:,:,3);
  11.     % 平滑滤波器
  12.     filter= [2 4 5 4 2;
  13.              4 9 12 9 4;
  14.              5 12 15 12 5;
  15.              4 9 12 9 4;
  16.              2 4 5 4 2];
  17.     filter=filter/115;

  18.     im_conv= convn(im,filter);  % 平滑滤波后图像

  19.     % 计算梯度
  20.     gradXfilt=[-1 0 1;   % 卷积模板convolution mask
  21.                -2 0 2;
  22.                -1 0 1];
  23.     gradYfilt=[1  2   1; % 卷积模板 convolution mask
  24.                0  0   0;
  25.               -1  -2  -1];
  26.     GradX= convn(im_conv,gradXfilt);  % 卷积
  27.     GradY= convn(im_conv,gradYfilt);  % 卷积
  28.     absgrad=abs(GradX)+abs(GradY);
  29.    
  30.     % 计算梯度角
  31.     [a,b]=size(GradX);
  32.     theta=zeros([a b]);
  33.     for i=1:a
  34.           for j=1:b
  35.                 if(GradX(i,j)==0)
  36.                    theta(i,j)=atan(GradY(i,j)/1e-10);
  37.                 else
  38.                     theta(i,j)=atan(GradY(i,j)/GradX(i,j));
  39.                 end
  40.           end
  41.      end
  42.       theta=theta*(180/3.14);
  43.       for i=1:a
  44.           for j=1:b
  45.                 if(theta(i,j)<0)
  46.                     theta(i,j)= theta(i,j)-90;
  47.                     theta(i,j)=abs(theta(i,j));
  48.                 end
  49.           end
  50.      end
  51.       for i=1:a
  52.           for j=1:b
  53.               if ((0<theta(i,j))&&(theta(i,j)<22.5))||((157.5<theta(i,j))&&(theta(i,j)<181))
  54.                     theta(i,j)=0;
  55.               elseif (22.5<theta(i,j))&&(theta(i,j)<67.5)
  56.                      theta(i,j)=45;
  57.               elseif (67.5<theta(i,j))&&(theta(i,j)<112.5)  
  58.                       theta(i,j)=90;
  59.               elseif (112.5<theta(i,j))&&(theta(i,j)<157.5)
  60.                       theta(i,j)=135;
  61.               end
  62.           end
  63.       end

  64.     % 非极大值抑制
  65.     canny_im = padarray(absgrad, [1 1]);
  66. % A = [1 2; 3 4];
  67. % B = padarray(A,[3 2],'replicate','post')
  68. % B =
  69. %      1     2     2     2
  70. %      3     4     4     4
  71. %      3     4     4     4
  72. %      3     4     4     4
  73. %      3     4     4     4
  74.     [a,b]=size(theta);
  75.     for i=2:a-2
  76.         for j=2:b-2
  77.                if (theta(i,j)==135)
  78.                      if ((canny_im(i-1,j+1)>canny_im(i,j))||(canny_im(i+1,j-1)>canny_im(i,j)))
  79.                           canny_im(i,j)=0;
  80.                       end
  81.                elseif (theta(i,j)==45)   
  82.                       if ((canny_im(i+1,j+1)>canny_im(i,j))||(canny_im(i-1,j-1)>canny_im(i,j)))
  83.                            canny_im(i,j)=0;
  84.                       end
  85.                elseif (theta(i,j)==90)   
  86.                       if ((canny_im(i,j+1)>canny_im(i,j))||(canny_im(i,j-1)>canny_im(i,j)))
  87.                           canny_im(i,j)=0;
  88.                       end
  89.                elseif (theta(i,j)==0)   
  90.                       if ((canny_im(i+1,j)>canny_im(i,j))||(canny_im(i-1,j)>canny_im(i,j)))
  91.                           canny_im(i,j)=0;
  92.                       end
  93.                end
  94.         end
  95.     end

  96.    end
  97. end
复制代码


算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

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

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-5-19 13:59 , Processed in 0.225302 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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