Canny 边缘检测器正在检测图像的边界

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);

输入

预期结果

实际结果

在这里,我们看到图像的边界线正在被 Canny 边缘检测器检测到,这不是我预期的结果。

如何实现?


源代码

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

答案很简单,您的函数 discard_image_area 将图像边界附近的图像值更改为 0。

因此它在图像值和 0 之间创建 Step。
这正是 Canny 边缘检测器所寻找的。

应用该功能后显示图像,您可以轻松看到它。

只是不要使用那个功能。

应用edge函数后应用discard_image_area。否则,丢弃的区域显然会成为其边界。即这样做:

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);

输出: