Search code examples
matlabcanny-operator

Canny edge detector is detecting the borders of the images


clear_all();

image_name = 'canny_test.png';

% no of  pixels discarded on border areas
discard_pixels = 10;

% read image and convert to grayscale
input_image = gray_imread(image_name);

% discard border area
input_image = discard_image_area(input_image, discard_pixels);

% create a binary image
binary_image = edge(input_image,'canny');    
imshow(binary_image);

Input

enter image description here

Expected Outcome

enter image description here

Actual Outcome

enter image description here

Here, we see that the borderlines of the image are being detected by the Canny Edge Detector which is not my expected outcome.

How can I achieve this?


Source Code

function [output_image] = discard_image_area( input_image, pixel_count)
    output_image = input_image;

    [height, width] = size(input_image);

    % discard top
    output_image(1:pixel_count, :) = 0;
    % discard bottom
    h = height - pixel_count;
    output_image(h:height, :) = 0;    
    % discard left
    output_image(:,1:pixel_count) = 0;
    % discard right
    output_image(:,(width-pixel_count):width) = 0;
end

function img = gray_imread( image_name )
    I = imread(image_name);

    if(is_rgb(I))
        img = rgb2gray(I);
    elseif (is_gray(I))
        img = I;
    end
end

Solution

  • Apply discard_image_area after applying the edge function. Otherwise the discarded area makes its boundary apparently. i.e. do this:

    image_name = 'canny_test.png';
    discard_pixels = 10;
    input_image =  rgb2gray(imread(image_name));  % there is no such this as gray_imread
    
    % create a binary image
    binary_image = edge(input_image,'canny');    
    
    % discard border area
    binary_image = discard_image_area(binary_image , discard_pixels);
    
    imshow(binary_image);
    

    Output:

    output