MATLAB: LOCAL THRESHOLD AND BOOLEAN FUNCTION BASED EDGE DETECTION

booleanedge detectionfunctionlocalthreshold

I have to write a matlab code to implement the above edge detection scheme.
Can someone please give me an idea how to start coding it? how should I proceed ?
Below is the brief theory.
Localization of edges is done by this method. The concept behind it is to threshold a gray level image using local mean to make a binary image. It recognizes nearly all-actual edges and edges due to noise. To remove noise edges another approach is used.
I would appreciate any help.

Best Answer

  • See my demo. Feel free to adapt it as needed.
    clc;
    clearvars;
    close all;
    workspace;
    fontSize = 16;
    % Read in a standard MATLAB gray scale demo image.

    folder = fullfile(matlabroot, '\toolbox\images\imdemos');
    button = menu('Use which demo image?', 'CameraMan', 'Moon', 'Eight', 'Coins', 'Pout');
    if button == 1
    baseFileName = 'cameraman.tif';
    elseif button == 2
    baseFileName = 'moon.tif';
    elseif button == 3
    baseFileName = 'eight.tif';
    elseif button == 4
    baseFileName = 'coins.png';
    else
    baseFileName = 'pout.tif';
    end
    % Read in a standard MATLAB gray scale demo image.
    folder = fullfile(matlabroot, '\toolbox\images\imdemos');
    % Get the full filename, with path prepended.
    fullFileName = fullfile(folder, baseFileName);
    % Check if file exists.
    if ~exist(fullFileName, 'file')
    % File doesn't exist -- didn't find it there. Check the search path for it.
    fullFileName = baseFileName; % No path this time.
    if ~exist(fullFileName, 'file')
    % Still didn't find it. Alert user.
    errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
    uiwait(warndlg(errorMessage));
    return;
    end
    end
    grayImage = imread(fullFileName);
    % Get the dimensions of the image.
    % numberOfColorBands should be = 1.
    [rows, columns, numberOfColorBands] = size(grayImage);
    % Display the original gray scale image.
    subplot(2, 2, 1);
    imshow(grayImage, []);
    axis on;
    title('Original Grayscale Image', 'FontSize', fontSize);
    % Enlarge figure to full screen.
    set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
    % Give a name to the title bar.
    set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
    % Get the local mean and subtract the original from it
    % This is the Laplacian
    edgeImage = conv2(double(grayImage), [-1, -1, -1; -1, 8, -1; -1, -1, -1]/8, 'same');
    % Display the image.

    subplot(2, 2, 2);
    imshow(edgeImage, []);
    title('Edge Image', 'FontSize', fontSize);
    % Let's compute and display the histogram.
    [pixelCount, grayLevels] = hist(edgeImage(:), 100);
    subplot(2, 2, 3);
    bar(grayLevels, pixelCount);
    grid on;
    title('Histogram of edge image', 'FontSize', fontSize);
    xlim([0 grayLevels(end)]); % Scale x axis manually.
    % Threshold the image.
    binaryImage = edgeImage > 10;
    % Display the image.
    subplot(2, 2, 4);
    imshow(binaryImage, []);
    title('Binary Image', 'FontSize', fontSize);