flipkart ads

MATLAB TUTORIAL SEVEN


Edge Detection Methods for images using Matlab.

Perwitt Operator based Edge Detection of given image

clc;
clear all;
close all;
aa = imread('Arjundiscontinuity.JPG');
a = double(aa);
[row col] = size(a);
w1 = [-1 0 1;-1 0 1;-1 0 1];
w2 = [-1 -1 -1;0 0 0;1 1 1];
for x=2:1:size(a,1)-1;
    for y=2:1:size(a,2)-1;
a1=w1(1)*a(x-1,y-1)+w1(2)*a(x-1,y)+w1(3)*a(x-1,y+1)+w1(4)*a(x,y-1)+w1(5)*a(x,y)+w1(6)*a(x,y+1)+w1(7)*a(x+1,y-1)+w1(8)*a(x+1,y)+w1(9)*a(x+1,y+1);
size(a1);
a2=w2(1)*a(x-1,y-1)+w2(2)*a(x-1,y)+w2(3)*a(x-1,y+1)+w2(4)*a(x,y-1)+w2(5)*a(x,y)+w2(6)*a(x,y+1)+w2(7)*a(x+1,y-1)+w2(8)*a(x+1,y)+w2(9)*a(x+1,y+1);
size(a2);
    a11(x,y) = (a1);
    a12(x,y) = (a2);
    a3(x,y) = ((a1+a2));
    size(a3);
    end
end
Thresh=200;

a3=max(a3,Thresh);
a3(a3==round(Thresh))=0;
B1=uint8(a3);

a11=max(a11,Thresh);
a11(a11==round(Thresh))=0;
Bx1=uint8(a11);

a12=max(a12,Thresh);
a12(a12==round(Thresh))=0;
By1=uint8(a12);

subplot(2,2,1)
imshow(Bx1); title('Perwitt X direction gradient')
subplot(2,2,2)
imshow(By1); title('Perwitt Y direction gradient');
subplot(2,2,3)
imshow(a3); title('Perwitt Gradient image');
subplot(2,2,4)
imshow(~a3); title('Edge Detected Image');





Sobel Operator based Edge Detection of given image

clear all;
close all;
aa = imread('Arjundiscontinuity.JPG');
a = double(aa);
[row col] = size(a);
w2 = [-1 -2 -1;0 0 0;1 2 1];
w1 = [-1 0 1;-2 0 2;-1 0 1];
for x=2:1:size(a,1)-1;
    for y=2:1:size(a,2)-1;
a1=w1(1)*a(x-1,y-1)+w1(2)*a(x-1,y)+w1(3)*a(x-1,y+1)+w1(4)*a(x,y-1)+w1(5)*a(x,y)+w1(6)*a(x,y+1)+w1(7)*a(x+1,y-1)+w1(8)*a(x+1,y)+w1(9)*a(x+1,y+1);
size(a1);
a2=w2(1)*a(x-1,y-1)+w2(2)*a(x-1,y)+w2(3)*a(x-1,y+1)+w2(4)*a(x,y-1)+w2(5)*a(x,y)+w2(6)*a(x,y+1)+w2(7)*a(x+1,y-1)+w2(8)*a(x+1,y)+w2(9)*a(x+1,y+1);
size(a2);
    a11(x,y) = (a1);
    a12(x,y) = (a2);
    a3(x,y) = ((a1+a2));
    size(a3);
    end
end
Thresh=200;

a3=max(a3,Thresh);
a3(a3==round(Thresh))=0;
B1=uint8(a3);

a11=max(a11,Thresh);
a11(a11==round(Thresh))=0;
Bx1=uint8(a11);

a12=max(a12,Thresh)
a12(a12==round(Thresh))=0;
By1=uint8(a12);

subplot(2,2,1)
imshow(aa); title('Original Image')
subplot(2,2,2)
imshow(Bx1); title('Sobel X direction gradient');
subplot(2,2,3)
imshow(By1); title('Sobel Y direction gradient');
subplot(2,2,4)
imshow(a3); title('Sobel Gradient image');








Canny operator Comparison with Perwitt & Soble Operators based
Edge Detection using inbuit matlab commands :


I1 = imread('discontinuity.JPG');
I = rgb2gray(I1);
A1 = edge(I,'sobel')
A2 = edge(I,'prewitt');
A3 = edge(I,'canny');
figure
imshow(I); title('Original Image');
figure
imshow(A1); title('Sobel');
figure
imshow(A2); title('Perwitt');
figure
imshow(A3); title('canny');





        

No comments:

Post a Comment

flipkart products