Matlab使用Prewitte算子进行边缘检测

来源:互联网 发布:装网络摄像头怎么安装 编辑:程序博客网 时间:2024/06/18 15:58

算子比较

比较常见的边缘检测算子有Roberts、Prewitt和Sobel。这里给出一些简单比较:

  • Roberts算子:边缘定位准,但是对噪声敏感。适用于边缘明显且噪声较少的图像分割。Roberts边缘检测算子是一种利用局部差分算子寻找边缘的算子,Robert算子图像处理后结果边缘不是很平滑。经分析,由于Robert算子通常会在图像边缘附近的区域内产生较宽的响应,故采用上述算子检测的边缘图像常需做细化处理,边缘定位的精度不是很高。
  • Prewitt算子:对噪声有抑制作用,抑制噪声的原理是通过像素平均,但是像素平均相当于对图像的低通滤波,所以Prewitt算子对边缘的定位不如Roberts算子。
  • Sobel算子:Sobel算子和Prewitt算子都是加权平均,但是Sobel算子认为,邻域的像素对当前像素产生的影响不是等价的,所以距离不同的像素具有不同的权值,对算子结果产生的影响也不同。一般来说,距离越远,产生的影响越小。
  • Isotropic Sobel算子:加权平均算子,权值反比于邻点与中心点的距离,当沿不同方向检测边缘时梯度幅度一致,就是通常所说的各向同性。

简单定义

边缘检测的三个算子

|f||Gx|+|Gy|

|f|>T大于阀值的时候判定此像素点为边缘。

代码实现:

在主文件中进行了手动给出阀值T 和 不给出阀值T,让函数自动迭代生成。
prewittee.m

function prewittee(FILENAME)%prewittee(FILENAME) takes a gray scale image with filename FILENAME.% Read the image.Im = im2double(imread(FILENAME));% Show the image in a new window.figure;imshow(Im, [min(min(Im)) max(max(Im))]);title('Original Image');disp('Original image is read and displayed successfully.');%% Generate the corresponding binary edge image of the given image Im.T = double(max(max(Im)))*0.2;direction = 'all';g = myprewittedge(Im,T,direction);% Show the image in a new window.figure;imshow(g, [0 1]);title('Binary Edge Image 1');disp('The corresponding binary edge image is computed and displayed successfully.');%% Generate the corresponding binary edge image of the given image Im% without specifying the thresholddirection = 'all';f = myprewittedge(Im,[],direction);% Show the image in a new window.figure;imshow(f, [0 1]);title('Binary Edge Image 2');disp('The corresponding binary edge image is computed and displayed successfully.');

myprewittedge.m

% myprewittedge computes a binary edge image from the given image.%%   g = myprewittedge(Im,T,direction) computes the binary edge image from the%   input image Im.%   % The function myprewittedge, with the format g=myprewittedge(Im,T,direction), % computes the binary edge image from the input image Im. This function takes % an intensity image Im as its input, and returns a binary image g of the % same size as Im (mxn), with 1's where the function finds edges in Im and 0's % elsewhere. This function finds edges using the Prewitt approximation to the % derivatives with the assumption that input image values outside the bounds % are zero and all calculations are done using double-precision floating % point. The function returns g with size mxn. The image g contains edges at % those points where the absolute filter response is above or equal to the % threshold T.%   %       Input parameters:%       Im = An intensity gray scale image.%       T = Threshold for generating the binary output image. If you do not%       specify T, or if T is empty ([ ]), myprewittedge(Im,[],direction) %       chooses the value automatically according to the Algorithm 1 (refer%       to the assignment descripton).%       direction = A string for specifying whether to look for%       'horizontal' edges, 'vertical' edges, positive 45 degree 'pos45'%       edges, negative 45 degree 'neg45' edges or 'all' edges.function g = myprewittedge(Im,T,direction)  if isempty(T)    T = (max(max(Im)) + min(min(Im))) / 2;    for i = 1:10    G1 = Im .* (Im > T);    G2 = Im .* (Im < T);    m1 = mean(G1(G1~=0));    m2 = mean(G2(G2~=0));    T = (m1 + m2) / 2;    end  end    [m, n] = size(Im);%   temp = Im;  prewittNum = 0;  for j = 2:m-1    for k = 2:n-1      prewittNum = abs(Im(j-1,k+1)-Im(j+1,k+1)+Im(j-1,k)-Im(j+1,k)+Im(j-1,k-1)-Im(j+1,k-1))+abs(Im(j-1,k+1)+Im(j,k+1)+Im(j+1,k+1)-Im(j-1,k-1)-Im(j,k-1)-Im(j+1,k-1));      if (prewittNum > T)        temp(j,k) = 255;        else        temp(j,k) = 0;        end     end   end   g = temp;

示例输入与输出

输入文件

源文件

手动给出阀值T

手动给出阀值T

自动生成阀值T

自动生成阀值T

阅读全文
0 0
原创粉丝点击