Edge detection with a given direction using Matlab

E

The aim of this article is to detect the edges with a given direction in an image. To that end create a function [ E ] = oriented_edges( I, thr, a, da ) that takes as input a double grayscale image Ι, a threshold value thr, a direction a, and an angle da. The output of the function is a binary image Ε where the pixels that meet the following requirements should have the value 1:

  • The pixel intensity gradient is higher than thr.
  • The gradient direction (in rad) is inside the interval: (a-da,a+da)

For the gradient calculation we use the imfilter with an appropriate filter (e.g. Sobel).

function [ E ] = oriented_edges(  I, thr, a, da )
 if size(I,3)==3;I=rgb2gray(I);end
    Gx = imfilter(I,fspecial('sobel')','replicate');
    Gy = imfilter(I,fspecial('sobel'),'replicate');
    Gdir = atan2(Gy,Gx)*180/pi;
    E = sqrt(abs(Gx).^2 + abs(Gy).^2);     
    for i=1:size(E,1)
        for j=1:size(E,2)
            if (Gdir(i,j)<(a-da) || Gdir(i,j)>(a+da))
                E(i,j) = 0;
            end
        end
    end
   E=im2bw(E,thr);
end

The following matlab code snippet is an example of the previous function:

clc;clear all;close all;
I1 = im2double(imread('shapes.png'));

figure
E=oriented_edges(I1,0.5,0,0);
subplot(1,2,1),imshow(E),title('t=0.5 | a=0 | da=0');
E=oriented_edges(I1,0.5,180,0);
subplot(1,2,2),imshow(E),title('t=0.5 | a=180 | da=0');
figure
E=oriented_edges(I1,0.5,90,0);
subplot(1,2,1),imshow(E),title('t=0.5 | a=90 | da=0');
E=oriented_edges(I1,0.5,-90,0);
subplot(1,2,2),imshow(E),title('t=0.5 | a=-90 | da=0');
figure
E=oriented_edges(I1,0.5,90,45);
subplot(1,2,1),imshow(E),title('t=0.5 | a=90 | da=45');
E=oriented_edges(I1,0.5,90,90);
subplot(1,2,2),imshow(E),title('t=0.5 | a=90 | da=90');

Using the following image :

The results should be:

Disclaimer: The present content may not be used for training artificial intelligence or machine learning algorithms. All other uses, including search, entertainment, and commercial use, are permitted.

Categories

Tags