%-------------------------------------------------------------------------------
% Author:   Isaac Gerg, isaacgerg@psu.edu
% Date:     October 2003
% 
% 
% IMDETECTEDGES ( Image, Method, Threshold )
% 
% 
% Image       Grayscale image on which to perform edge detection.
% 
% Method      Method of edge detection.
%               Methods available are:
%                 'prewitt'
%                 'roberts'
%                 'sobel'
%                 'log' - Laplacian of Gausian
%                 'canny'
% 
% Threshold   Threshold value for edge creation. Relative threshold value.
%             Thresholding is done at maxValue(Image)*Threshold.
% 
% This function approximates the magnitude of the gradient as: 
%                |G| = |G_x| + |G_y|
% 
% 
%-------------------------------------------------------------------------------

function imgProcessedImage = imdetectedges(imgImage, strMethod, iThreshold)

%-------------------------------------------------------------------------------
% Validate input parameters.
if iThreshold < 0
    error('Illeagal threshold value specified');
    return
end

% Create masks.
iRobertsX = [1  0;
             0 -1];
iRobertsY = [0 -1;
             1  0];
iSobelX = [-1 0 1;
           -2 0 2;
           -1 0 1];
iSobelY = [1  2  1;
           0  0  0;
          -1 -2 -1];
iPrewittX = [ -1 0 1;
              -1 0 1;
              -1 0 1];
iPrewittY = [ 1  1  1;
              0  0  0;
             -1 -1 -1];
iLog = [ 1  1  1;
         1 -8  1;
         1  1  1];
% Canny edge detection will be implemented via Matlab function.

switch lower(strMethod)
    case 'roberts'
    	imgX = conv2(imgImage, iRobertsX);
    	imgY = conv2(imgImage, iRobertsY);
        imgMagnitude = abs(imgX) + abs(imgY);
        iThreshold = max(max(imgMagnitude))*iThreshold;
        iSize = size(imgImage);
        for X = 1:iSize(1),
            for Y = 1:iSize(2),
                if imgMagnitude(X,Y) >= iThreshold;
                    imgMagnitude(X,Y) = 255;
                else
                    imgMagnitude(X,Y) = 0;
                end
            end
    	end
        disp('Roberts edge detection.');
        imgProcessedImage = imgMagnitude;
    case 'sobel'
    	imgX = conv2(imgImage, iSobelX);
    	imgY = conv2(imgImage, iSobelY);
        imgMagnitude = abs(imgX) + abs(imgY);
        iThreshold = max(max(imgMagnitude))*iThreshold;
        iSize = size(imgImage);
        for X = 1:iSize(1),
            for Y = 1:iSize(2),
                if imgMagnitude(X,Y) >= iThreshold;
                    imgMagnitude(X,Y) = 255;
                else
                    imgMagnitude(X,Y) = 0;
                end
            end
    	end
        disp('Sobel edge detection.');
        imgProcessedImage = imgMagnitude;
    case 'prewitt'
    	imgX = conv2(imgImage, iPrewittX);
    	imgY = conv2(imgImage, iPrewittY);
        imgMagnitude = abs(imgX) + abs(imgY);
        iThreshold = max(max(imgMagnitude))*iThreshold;
        iSize = size(imgImage);
        for X = 1:iSize(1),
            for Y = 1:iSize(2),
                if imgMagnitude(X,Y) >= iThreshold;
                    imgMagnitude(X,Y) = 255;
                else
                    imgMagnitude(X,Y) = 0;
                end
            end
    	end
        disp('Prewitt edge detection.');
        imgProcessedImage = imgMagnitude;
    case 'log'
    	imgImage = conv2(imgImage, iLog);
        imgMagnitude = abs(imgImage);
        iThreshold = max(max(imgMagnitude))*iThreshold;
        iSize = size(imgImage);
        for X = 1:iSize(1),
            for Y = 1:iSize(2),
                if imgMagnitude(X,Y) >= iThreshold;
                    imgMagnitude(X,Y) = 255;
                else
                    imgMagnitude(X,Y) = 0;
                end
            end
    	end
        disp('LoG edge detection.');
        imgProcessedImage = imgMagnitude;
    case 'canny'
        disp('Canny edge detection.');
        imgProcessedImage = edge(imgImage, 'canny');
    otherwise
        disp('Unknown edge detection method!');
        imgProcessedImage = imgImage;
end
%-------------------------------------------------------------------------------