Image Edge Detection — A Simple Approach
By: Pravish K Viswan
Tags: R Programming, Edge Detection, Image Processing

Hey.. meaning to create a story here for while. This is a program created as part of Georgia Tech MS program to perform simple image detection. Posting the code below.

clear all;

clc;

%Input image

img = imread (‘horse1.jpg’);

%Show input image

figure, imshow(img),title(‘horse1.jpg’);

img = rgb2gray(img);

img = double (img);

%Value for Thresholding

T_Low = 0.075;

T_High = 0.099;



%Gaussian Filter Coefficient

B = [1, 4, 7, 10, 7, 4, 1; 4, 12, 26, 33, 26, 12, 4;7, 26, 55, 71, 55, 26, 7;10, 33, 71, 91, 71, 33, 10;7, 26, 55, 71, 55, 26, 7;4, 12, 26, 33, 26, 12, 4;1, 4, 7, 10, 7, 4, 1 ];

B = 1/1115.* B;

%Convolution of image by Gaussian Coefficient

A=conv2(img, B, ‘same’);



xgrad=img;

ygrad=img;

[rows,cols] = size(img);

%calculating gradient image in X-direction

for i=1:rows-1

for j=1:cols-1

xgrad(i,j) = abs(img(i+1,j)-img(i,j)+img(i+1,j+1)-img(i,j+1));

ygrad(i,j) = abs(img(i,j+1)-img(i,j)+img(i+1,j+1)-img(i+1,j));

end

end



xgrad=0.5 * xgrad;

ygrad=0.5 * ygrad;

figure, imshow(uint8(img)),title(‘gray image’);

figure, imshow(uint8(xgrad)), title(‘X-gradient image’);

figure, imshow(uint8(ygrad)), title(‘Y-gradient image’);



gtheta = atan2 (xgrad, ygrad);



xphi=zeros (rows, cols);

yphi=zeros (rows, cols);

gtheta2=zeros (rows, cols);

xphi2=zeros (rows, cols);

yphi2=zeros (rows, cols);



for i=2:rows-1

for j=2:cols-1

if (xgrad(i,j) > xgrad(i-1,j) && xgrad(i,j) > xgrad(i+1,j))

xphi(i,j)=xgrad(i,j);

end;



if (ygrad(i,j) > ygrad(i,j-1) && ygrad(i,j) > ygrad(i,j+1))

yphi(i,j)=ygrad(i,j);

end;

end;

end;



figure, imshow(xphi), title(‘xphi1 nonmaximal suppression.’);

figure, imshow(yphi), title(‘yphi1 nonmaximal suppression.’);

figure, imshow(gtheta), title(‘gtheta1 nonmaximal suppression.’);



for i=2:rows-1

for j = 2:cols-1

if(gtheta(i,j) > — pi/8 && gtheta(i,j) <= pi/8)

gtheta2(i,j)=0;

if (xgrad(i,j) > xgrad(i,j-1) && xgrad(i,j) > xgrad(i,j+1))

xphi2(i,j)=xgrad(i,j);

end;

if (ygrad(i,j) > ygrad(i,j-1) && ygrad(i,j) > ygrad(i,j+1))

yphi2(i,j)=ygrad(i,j);

end;

elseif(gtheta(i,j) > pi/8 && gtheta(i,j) <= (3/8) * pi)

gtheta2(i,j)=pi/4;

if (xgrad(i,j) > xgrad(i+1,j-1) && xgrad(i,j) > xgrad(i-1,j+1))

xphi2(i,j)=xgrad(i,j);

end;

if (ygrad(i,j) > ygrad(i+1,j-1) && ygrad(i,j) > ygrad(i-1,j+1))

yphi2(i,j)=ygrad(i,j);

end;

elseif(gtheta(i,j) > — (3/8) * pi && gtheta(i,j) <= — pi/8)

gtheta2(i,j) = -pi/4;

if (xgrad(i,j) > xgrad(i-1,j-1) && xgrad(i,j) > xgrad(i+1,j+1))

xphi2(i,j)=xgrad(i,j);

end;

if (ygrad(i,j) > ygrad(i-1,j-1) && ygrad(i,j) > ygrad(i+1,j+1))

yphi2(i,j)=ygrad(i,j);

end;

elseif((gtheta(i,j) > (3/8) * pi && gtheta(i,j) <= pi/2) || (gtheta(i,j) >= — pi/2 && gtheta(i,j) <= — (3/8) * pi))

gtheta2(i,j) =pi/2;

if (xgrad(i,j) > xgrad(i-1,j) && xgrad(i,j) > xgrad(i+1,j))

xphi2(i,j)=xgrad(i,j);

end;

if (ygrad(i,j) > ygrad(i-1,j) && ygrad(i,j) > ygrad(i+1,j))

yphi2(i,j)=ygrad(i,j);

end;

end;

end;

end;



figure, imshow(xphi2), title(‘xphi 2 nonmaximal suppression.’);

figure, imshow(yphi2), title(‘yphi 2 nonmaximal suppression.’);

figure, imshow(gtheta2),title(‘gtheta 2 nonmaximal suppression.’);



T_Low = T_Low * max(max(xphi2));

T_High = T_High * max(max(xphi2));



xE = zeros (rows, cols);

yE = zeros (rows, cols);

E = zeros (rows, cols);

count1 = 1;



while count1 ~= 0

count1 = 0;

for i = 1 : rows

for j = 1 : cols

if (xphi2(i,j) >= T_High && xE(i,j) == 0)

xE(i,j) = 1;

count1=count1+1;

elseif (xphi2(i,j) >= T_Low && xE(i,j) ==0)

if(xE(i+1,j)==1 || xE(i-1,j)==1 || xE(i,j+1)==1 || xE(i,j-1)==1 || xE(i-1, j-1)==1 || xE(i-1, j+1)==1 || xE(i+1, j+1)==1 || xE(i+1, j-1)==1)

xE(i,j) = 1;

count1=count1+1;

end;

end;

end;

end;

end;

count1 = 1;



while count1 ~= 0

count1 = 0;

for i = 1 : rows

for j = 1 : cols

if (yphi2(i,j) >= T_High && yE(i,j) == 0)

yE(i,j) = 1;

count1=count1+1;

elseif (yphi2(i,j) >= T_Low && yE(i,j) ==0)

if(yE(i+1,j)==1 || yE(i-1,j)==1 || yE(i,j+1)==1 || yE(i,j-1)==1 || yE(i-1, j-1)==1 || yE(i-1, j+1)==1 || yE(i+1, j+1)==1 || yE(i+1, j-1)==1)

yE(i,j) = 1;

count1=count1+1;

end;

end;

end;

end;

end;



E = (xE.²) + (yE.²);

E = sqrt(E);



figure, imshow(xE),title(‘x of final image’);

figure, imshow(yE),title(‘y of final image’);

figure, imshow(E),title(‘E final image’);