Edge detection using fuzzy logic

An essential component of image processing and computer vision is edge detection. It seeks to locate the divisions between various areas or items in an image. The edges signify transitions from one zone to another and represent substantial variations in intensity or color.

By utilizing the capacity of fuzzy sets and fuzzy rules to accommodate ambiguous or unclear information, fuzzy logic can be utilized for edge detection. In contrast to conventional edge detection methods, fuzzy edge detection algorithms offer a mechanism to model and capture subtle variations in intensity or color.

Import RGB image and convert it to grayscale.

Irgb = imread('fruits.png');
Reading the image
Orignal RGB image
Orignal RGB image

Once the image has been read, convert that image into grayscale.

Igray = rgb2gray(Irgb);
figure
image(Igray,'CDataMapping','scaled')
colormap('gray')
title('Input Image in Grayscale')
Converting the image to grayscale
Grayscaled image
Grayscaled image

Only single-precision and double-precision data are supported by the evalfis function for assessing fuzzy inference systems. Use the im2double method to transform Igray into a double array.

I = im2double(Igray);
Converting image to double precision

The fuzzy logic edge detection technique uses the picture gradient in this example to find breaks in uniform regions. Calculate the gradient of the image along the x- and y-axes.

Simple gradient filters are Gx and Gy. You convolve I with Gx using the conv2 function to get a matrix containing the x-axis gradients of I. The [-1 1] range corresponds to the gradient values. Convolute I with Gy to obtain the y-axis gradients of I similarly.

Gx = [-1 1];
Gy = Gx';
Ix = conv2(I,Gx,'same');
Iy = conv2(I,Gy,'same');
Obtaining gradiant image

To plot the image about Ix gradient values.

figure
image(Ix,'CDataMapping','scaled')
colormap('gray')
title('Ix')
Plotting image gradient with Ix
Image gradient with Ix
Image gradient with Ix

To plot the image about Iy gradient values.

figure
image(Iy,'CDataMapping','scaled')
colormap('gray')
title('Iy')
Plotting image gradient wih Iy
Image gradient with Iy
Image gradient with Iy

Other filters can also be used to obtain image gradients.

Next, we create the fuzzy inference system (FIS) named edgeFIS. We specify the Ix, Iy, and image gradients as input to edgeFIS.

edgeFIS = mamfis('Name','edgeDetection');
edgeFIS = addInput(edgeFIS,[-1 1],'Name','Ix');
edgeFIS = addInput(edgeFIS,[-1 1],'Name','Iy');
Defining Fuzzy Inference System (FIS) for edge detection

Once the FIS has been created, we specify the membership function. The membership function we're using here is the zero-mean Gaussian membership function.

sx = 0.1;
sy = 0.1;
edgeFIS = addMF(edgeFIS,'Ix','gaussmf',[sx 0],'Name','zero');
edgeFIS = addMF(edgeFIS,'Iy','gaussmf',[sy 0],'Name','zero');
Specifying a zero mean guassian membership function

The zero membership functions sx and sy parameters define its standard deviation for the Ix and Iy inputs. You can alter the values of sx and sy to modify the performance of the edge detector. Raising the settings makes the algorithm less sensitive to picture edges and reduces the intensity of identified edges.

Now, specify the intensity of the edge detected as an output of edgeFIS.

edgeFIS = addOutput(edgeFIS,[0 1],'Name','Iout');
Specifying intensity of edge detection

The triangular membership function is specified. They are specified for black and white for Iout.

wa = 0.1;
wb = 1;
wc = 1;
ba = 0;
bb = 0;
bc = 0.7;
edgeFIS = addMF(edgeFIS,'Iout','trimf',[wa wb wc],'Name','white');
edgeFIS = addMF(edgeFIS,'Iout','trimf',[ba bb bc],'Name','black');
Triangular membership function

Once you have specified the Ix, Iy, and Iout, the plot of all these can be generated to see the region.

figure
subplot(2,2,1)
plotmf(edgeFIS,'input',1)
title('Ix')
subplot(2,2,2)
plotmf(edgeFIS,'input',2)
title('Iy')
subplot(2,2,[3 4])
plotmf(edgeFIS,'output',1)
title('Iout')
Plot of membership fuctions

Create rules that will turn a pixel black if it doesn't belong in a uniform zone and white otherwise. When the gradient in the image is 0 in both directions, a pixel is said to be in a uniform area. The pixel is on an edge if each direction has a gradient that is not zero.

r1 = "If Ix is zero and Iy is zero then Iout is white";
r2 = "If Ix is not zero or Iy is not zero then Iout is black";
edgeFIS = addRule(edgeFIS,[r1 r2]);
edgeFIS.Rules
Specifying FIS rules

Lastly, you evaluate the FIS. For each row of pixels in I, using the corresponding rows of Ix and Iy as inputs, evaluate the edge detector's output.

Ieval = zeros(size(I));
for ii = 1:size(I,1)
Ieval(ii,:) = evalfis(edgeFIS,[(Ix(ii,:));(Iy(ii,:))]');
end
FIS evaluation

This plots the original grayscaled image.

figure
image(I,'CDataMapping','scaled')
colormap('gray')
title('Original Grayscale Image')
Orignal grayscaled image

This plots the image displaying the edged detected.

figure
image(Ieval,'CDataMapping','scaled')
colormap('gray')
title('Edge Detection Using Fuzzy Logic')
Image with edge detected

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved