Hello Mat

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

Sobel算子

[复制链接]

1296

主题

1522

帖子

112

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22643
发表于 2017-2-5 11:32:51 | 显示全部楼层 |阅读模式
Sobel算子是把图像中的每个像素的上下左右四领域的灰度值加权差,在边缘处达到极值从而检测边缘。
图像中每个像素点都与下面两个核做卷积,一个核对垂直边缘影响最大,而另一个核对水平边缘影响最大,两个卷积的最大值作为这个像素点的输出值。
Sobel算法能够产生较好的检测效果,而且对噪声具有平滑抑制作用,但是得到的边缘较粗,可能出现伪边缘,该算法根据具体工况合理设计。
Sobel算法程序如下:
  1. %% Sobel
  2. clc,clear,close all                    % 清屏、清工作区、关闭窗口
  3. warning off                         % 消除警告
  4. feature jit off                        % 加速代码执行
  5. I=imread('1.jpg');  % 读入图像
  6. r=I(:,:,1);g=I(:,:,2);b=I(:,:,3);
  7. nI=size(r);
  8. im = single(I) / 255;

  9.         yfilter = fspecial('sobel'); % sobel
  10.         xfilter = yfilter';
  11.        
  12.         rx = imfilter(im(:,:,1), xfilter);
  13.         gx = imfilter(im(:,:,2), xfilter);
  14.         bx = imfilter(im(:,:,3), xfilter);
  15.        
  16.         ry = imfilter(im(:,:,1), yfilter);
  17.         gy = imfilter(im(:,:,2), yfilter);
  18.         by = imfilter(im(:,:,3), yfilter);
  19.        
  20.         Jx = rx.^2 + gx.^2 + bx.^2;
  21.         Jy = ry.^2 + gy.^2 + by.^2;
  22.         Jxy = rx.*ry + gx.*gy + bx.*by;

  23.         D = sqrt(abs(Jx.^2 - 2*Jx.*Jy + Jy.^2 + 4*Jxy.^2)); % 2x2 matrix J'*J的第一个特征值
  24.         e1 = (Jx + Jy + D) / 2;
  25.         %  e2 = (Jx + Jy - D) / 2;                                   %第二个特征值

  26. edge_magnitude = sqrt(e1);
  27. edge_orientation = atan2(-Jxy, e1 - Jy);
  28. % figure,
  29. % subplot(121),imshow(edge_magnitude)                  % 梯度
  30. % subplot(122),imshow(edge_orientation)                  % 方向

  31. sob=edge(edge_magnitude,'sobel',0.29);
  32. % sob=bwareaopen(sob,100);                                  % 剔除小块
  33. % figure,imshow(y),title('Sobel Edge Detection')

  34. % 3*3 sobel
  35. f=edge_magnitude;
  36. sx=[-1 0 1;-2 0 2;-1 0 1];                         % 卷积模板convolution mask
  37. sy=[-1 -2 -1;0 0 0;1 2 1];                         % 卷积模板convolution mask
  38. for x=2:1:nI(1,1)-1
  39.     for y=2:1:nI(1,2)-1
  40.         mod=[f(x-1,y-1),2*f(x-1,y),f(x-1,y+1);
  41.             f(x,y-1),2*f(x,y),f(x,y+1);
  42.             f(x+1,y-1),2*f(x+1,y),f(x+1,y+1)];
  43.         mod=double(mod);
  44.         fsx=sx.*mod;
  45.         fsy=sy.*mod;
  46.         ftemp(x,y)=sqrt((sum(fsx(:)))^2+(sum(fsy(:)))^2);
  47.     end
  48. end
  49. fs=im2bw(ftemp); % fs=im2uint8(ftemp);
  50. fs=bwareaopen(fs,500);
  51. % figure,imshow(fs);title('Sobel Edge Detection')

  52. subplot(131),imshow(edge_magnitude),title('edge magnitude')
  53. subplot(132),imshow(sob),title('edge magnitude extraction')
  54. subplot(133),imshow(fs);title('sobel Edge Detection')
复制代码



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

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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