Thursday 17 March 2016

Image Processing Using Matlab: Sobel Filter Without using Inbuilt Function

Problem:

Implement SobelImage(image) to compute edge magnitude and orientation information. SobelImage should display the magnitude and orientation of the edges in an image. Please use ”rgb2gray” command in matlab to convert color image into a gray scale image and then run the filter on that.


Main Function:

clear
close all
input = imread('LadyBug.jpg');
%input=double(input);
input1=rgb2gray(input);
figure,imshow(input1),title('original');
[im1,im2]=SobelImage(input1);
%figure(1)
%imshow(uint8(input)), title('original');
figure(3)
imshow(im1), title('magnitude');
figure(4);
imshow(im2), title('direction');

Sobel Filter Function:

function[magnitude,direction]=SobelImage(im2)
temp1=(im2);
temp=double(temp1);
direction=zeros(size(temp));
for i = 2:size(temp, 1)-2
  for j = 2:size(temp, 2)-2
         % x-direction:
         Gx=((2*temp(i+2,j+1)+temp(i+2,j)+temp(i+2,j+2))-(2*temp(i,j+1)+temp(i,j)+temp(i,j+2)));
         %y-direction:
         Gy=((2*temp(i+1,j+2)+temp(i,j+2)+temp(i+2,j+2))-(2*temp(i+1,j)+temp(i,j)+temp(i+2,j)));
         temp1(i,j)=sqrt(Gx^2+Gy^2); %magnitude of the edge
         direction(i,j)=atan(Gy/Gx); %direction of the edge
  end
end
magnitude=temp1;
direction=double(direction);






0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home