clc;
close all;
clear;
workspace;
format short g;
format compact;
fontSize = 15;
fprintf('Beginning to run %s.m ...\n', mfilename);
grayImage = imread('cameraman.tif');
subplot(2, 1, 1);
imshow(grayImage);
edg = edgy(grayImage);
subplot(2, 1, 2);
imshow(edg, []);
fprintf('Done running %s.m.\n', mfilename);
function edg = edgy(originalImage)
[rows, columns, numberOfColorChannels] = size(originalImage);
if numberOfColorChannels == 3
originalImage = rgb2gray(originalImage);
end
C = double(originalImage);
Gx = zeros(rows, columns);
Gy = zeros(rows, columns);
for row = 1 : rows - 2
for col = 1 : columns - 2
Gx=((2*C(row+2,col+1)+C(row+2,col)+C(row+2,col+2))-(2*C(row,col+1)+C(row,col)+C(row,col+2)));
Gy=((2*C(row+1,col+2)+C(row,col+2)+C(row+2,col+2))-(2*C(row+1,col)+C(row,col)+C(row+2,col)));
C(row,col)=sqrt(Gx.^2+Gy.^2);
end
end
edg = C;
end
Best Answer