You are on page 1of 8

Fuzzy Logic Image Processing - MATLAB & Simulink Example 3/9/16, 18:38

Fuzzy Logic Image Processing
This example shows how to use Fuzzy Logic Toolbox software for image processing. Open This Example
Specifically, this example shows how to detect edges in an image.

An edge is a boundary between two uniform regions. You can detect an edge by comparing the intensity of
neighboring pixels. However, because uniform regions are not crisply defined, small intensity differences between
two neighboring pixels do not always represent an edge. Instead, the intensity difference might represent a shading
effect.

The fuzzy logic approach for image processing allows you to use membership functions to define the degree to
which a pixel belongs to an edge or a uniform region.

Import RGB Image and Convert to Grayscale
Import the image into MATLAB.

Irgb = imread('peppers.png');

Irgb is a 384 x 512 x 3 uint8 array. The three channels of Irgb (third array dimension) represent the red, green,
and blue intensities of the image.

Convert Irgb to grayscale so that you can work with a 2-D array instead of a 3-D array. Use the standard NTSC
conversion formula to calculate the effective luminance of each pixel.

Igray = 0.2989*Irgb(:,:,1)+0.5870*Irgb(:,:,2)+0.1140*Irgb(:,:,3);

figure; image(Igray,'CDataMapping','scaled'); colormap('gray');
title('Input Image in Grayscale')

http://www.mathworks.com/help/fuzzy/examples/fuzzy-logic-image-processing.html Page 1 of 8

mathworks. Convert Image to Double-Precision Data The Fuzzy Logic Toolbox software operates on double-precision numbers only. convert Igray.com/help/fuzzy/examples/fuzzy-logic-image-processing. classType = class(Igray).MATLAB & Simulink Example 3/9/16. Calculate the image gradient along the x-axis and y-axis. Obtain Image Gradient The fuzzy logic edge-detection algorithm for this example relies on the image gradient to locate breaks in uniform regions.Fuzzy Logic Image Processing . you can use the im2double function in the Image Processing Toolbox software to convert Igray to a scaled. I = I/scalingFactor. you can use the rgb2gray function in the Image Processing Toolbox software to convert Irgb to grayscale. Scale I so that its elements are in the [0 1] range. I = double(Igray). Alternatively. scalingFactor = double(intmax(classType)). 18:38 Alternatively. double-precision image.html Page 2 of 8 . http://www. to a double array. all elements of I are in that range too. So. a uint8 array. Because uint8 values are in the [0 2^8-1] range.

'CDataMapping'. colormap('gray').Gx. Ix = conv2(I. 18:38 Gx = [-1 1]. figure. image(Iy.'CDataMapping'.'same'). title('Ix'). Gy = Gx'.'same').MATLAB & Simulink Example 3/9/16.Gy.html Page 3 of 8 .Fuzzy Logic Image Processing . title('Iy').'scaled'). http://www. image(Ix. Iy = conv2(I. colormap('gray').'scaled').mathworks. figure.com/help/fuzzy/examples/fuzzy-logic-image-processing.

'input'. http://www.'gaussmf'. edgeFIS = addvar(edgeFIS.1.com/help/fuzzy/examples/fuzzy-logic-image-processing.[sx 0]).[-1 1]). you can use the imfilter.[sy 0]). edgeFIS = addmf(edgeFIS.[-1 1]). Define Fuzzy Inference System (FIS) for Edge Detection Create a Fuzzy Inference System (FIS) for edge detection. 18:38 Gx and Gy are simple gradient filters. Specify a zero-mean Gaussian membership function for each input.'input'.'Iy'. Ix and Iy. The gradient values are in the [-1 1] range. You can use other filters to obtain the image gradients. If the gradient value for a pixel is 0.'zero'. as the inputs of edgeFIS.'gaussmf'.mathworks. edgeFIS = addmf(edgeFIS. to obtain a matrix containing the x-axis gradients of I. or imgradient functions to obtain the image gradients. sy = 0.2.MATLAB & Simulink Example 3/9/16. edgeFIS.1.'input'.1. Specify the image gradients. You convolve I with Gx.Fuzzy Logic Image Processing . using the conv2 function. You can change the values of sx and sy to adjust the edge detector performance. see Convolution. imgradientxy. if you have the Image Processing Toolbox software. edgeFIS = addvar(edgeFIS. Alternatively. Similarly. Increasing the values makes the algorithm less sensitive to the edges in the image and decreases the intensity of the detected edges. sx and sy specify the standard deviation for the zero membership function for the Ix and Iy inputs. you convolve I with Gy to obtain the y-axis gradients of I.'zero'. For information about how you can filter an image using convolution. sx = 0. edgeFIS = newfis('edgeDetection').'Ix'. such as the Sobel operator or the Prewitt operator.html Page 4 of 8 .'input'. then it belongs to the zero membership function with a degree of 1.

'output'.1. 18:38 Specify the intensity of the edge-detected image as an output of edgeFIS.Fuzzy Logic Image Processing .'white'.1). plotmf(edgeFIS.1. title('Iout') Specify FIS Rules Add rules to make a pixel white if it belongs to a uniform region. white and black.'trimf'.1).[ba bb bc]). and bc to adjust the edge detector performance.2). title('Ix'). figure subplot(2. edgeFIS = addmf(edgeFIS.2.[wa wb wc]). edgeFIS = addmf(edgeFIS.'Iout'.[0 1]).com/help/fuzzy/examples/fuzzy-logic-image-processing. make the pixel black.2. http://www. and end of the triangles of the membership functions. title('Iy').2). subplot(2. for Iout. As you can with sx and sy. wb = 1.'input'.'output'. you can change the values of wa.mathworks.'output'. Otherwise. Specify the triangular membership functions. plotmf(edgeFIS.1). wa = 0. ba = 0. wc. bb = 0. bc = . subplot(2. edgeFIS = addvar(edgeFIS.'input'.'trimf'. plotmf(edgeFIS. bb. peak. ba. Plot the membership functions of the inputs/outputs of edgeFIS.2.[3 4]).7.html Page 5 of 8 . The triplets specify the start. These parameters influence the intensity of the detected edges.'output'. wb.1. wc = 1.MATLAB & Simulink Example 3/9/16.'black'.

html Page 6 of 8 . title('Edge Detection Using Fuzzy Logic') http://www. image(Ieval.1) Ieval(ii.edgeFIS).'CDataMapping'.'scaled').:) = evalfis([(Ix(ii.r2). showrule(edgeFIS) ans = 1.:)).r). r = char(r1. Ieval = zeros(size(I)).'scaled'). If (Ix is not zero) or (Iy is not zero) then (Iout is black) (1) Evaluate FIS Evaluate the output of the edge detector for each row of pixels in I using corresponding rows of Ix and Iy as inputs. r2 = 'If Ix is not zero or Iy is not zero then Iout is black'. image(I. colormap('gray'). If (Ix is zero) and (Iy is zero) then (Iout is white) (1) 2. end Plot Results figure. colormap('gray').(Iy(ii.:)).'CDataMapping'. 18:38 r1 = 'If Ix is zero and Iy is zero then Iout is white'.% Preallocate the output matrix for ii = 1:size(I.mathworks. edgeFIS = parsrule(edgeFIS.MATLAB & Simulink Example 3/9/16.com/help/fuzzy/examples/fuzzy-logic-image-processing. title('Original Grayscale Image') figure.Fuzzy Logic Image Processing .]'.

18:38 Summary http://www.html Page 7 of 8 .MATLAB & Simulink Example 3/9/16.com/help/fuzzy/examples/fuzzy-logic-image-processing.Fuzzy Logic Image Processing .mathworks.

You defined the gradient as zero using Gaussian membership functions for your FIS inputs.MATLAB & Simulink Example 3/9/16.Fuzzy Logic Image Processing .html Page 8 of 8 .com/help/fuzzy/examples/fuzzy-logic-image-processing. 18:38 You detected the edges in an image using a FIS. If the gradient for a pixel is not zero. http://www. comparing the gradient of every pixel in the x and y directions. then the pixel belongs to an edge (black).mathworks.