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');
Once the image has been read, convert that image into grayscale.
Igray = rgb2gray(Irgb);figureimage(Igray,'CDataMapping','scaled')colormap('gray')title('Input Image in Grayscale')
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);
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');
To plot the image about Ix
gradient values.
figureimage(Ix,'CDataMapping','scaled')colormap('gray')title('Ix')
To plot the image about Iy
gradient values.
figureimage(Iy,'CDataMapping','scaled')colormap('gray')title('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');
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');
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');
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');
Once you have specified the Ix
, Iy
, and Iout
, the plot of all these can be generated to see the region.
figuresubplot(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')
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
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
This plots the original grayscaled image.
figureimage(I,'CDataMapping','scaled')colormap('gray')title('Original Grayscale Image')
This plots the image displaying the edged detected.
figureimage(Ieval,'CDataMapping','scaled')colormap('gray')title('Edge Detection Using Fuzzy Logic')
Free Resources