资源简介

在图像处理中,使用Sobel算子进行边缘提取的Matlab代码。

资源截图

代码片段和文件信息

%Function:Simple Edge Detection
%Data:3rd Oct.2008

clc;
clear all;
close all;

%%%%%%%%%%%%%%%%
%%%  Imread  %%%
%%%%%%%%%%%%%%%%
I0=imread(‘picturename.jpg‘);
I=rgb2gray(I0);
J0=double(I);
J_x=J0;
J_y=J0;
J=J0;
[MN]=size(I);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%  Sobel Operator Filtering  %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Sobel_x=(1/4)*[-1-2-1;000;121];
Sobel_y=(1/4)*[-101;-202;-101];
[W_SW_S]=size(Sobel_x);
Half_W_S=(W_S-1)/2;
%Filtering with Matrix Sobel_x to Get gx
for p=Half_W_S+1:M-Half_W_S   
    for q=Half_W_S+1:N-Half_W_S
        filter_sum=0;
        for k=-Half_W_S:Half_W_S
            for l=-Half_W_S:Half_W_S
                filter_sum=filter_sum+J0(p+kq+l)*Sobel_x(k+Half_W_S+1l+Half_W_S+1);
            end
        end
        J_x(pq)=filter_sum;
    end
end
%Filtering with Matrix Sobel_y to Get gy
for p=Half_W_S+1:M-Half_W_S
    for q=Half_W_S+1:N-Half_W_S
        filter_sum=0;
        for k=-Half_W_S:Half_W_S
            for l=-Half

评论

共有 条评论