模糊逻辑图像处理
此示例显示如何使用模糊逻辑进行图像处理。具体地,该示例示出了如何检测图像中的边缘。
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.
图像处理的模糊逻辑方法允许您使用隶属函数来定义像素所属的程度或均匀区域。
一世mport RGB Image and Convert to Grayscale
导入图像。
一世rgb = imread('peppers.png');
一世rgb
是384 x 512 x 3uint8.
大批。三个渠道一世rgb
(third array dimension) represent the red, green, and blue intensities of the image.
转变一世rgb
to grayscale so that you can work with a 2-D array instead of a 3-D array. To do so, use thergb2gray
功能。
IGRAY = RGB2GRAY(IRGB);图图像(IGRAI,'cdatamapping'那'缩放'的)colormap('灰色的'的)title('灰度中的输入图像'的)
将图像转换为双精度数据
这evalfis.
function for evaluating fuzzy inference systems supports only single-precision and double-precision data. Therefore, convertIgay.
to adouble
array using theim2double.
功能。
i = im2double(igray);
Obtain Image Gradient
该示例的模糊逻辑边缘检测算法依赖于图像梯度以定位均匀区域中的断裂。计算图像梯度X-axis andy-轴。
Gx
andGy
are simple gradient filters. To obtain a matrix containing theX-AXIS梯度一世
那you convolve一世
和Gx
using theConv2.
功能。梯度值位于[-1 1]范围内。同样,获得y-AXIS梯度一世
,卷起一世
和Gy
。
Gx = [-1 1]; Gy = Gx'; Ix = conv2(I,Gx,'same');iy = conv2(我,gy,'same');
绘制图像渐变。
图图像(ix,'cdatamapping'那'缩放'的)colormap('灰色的'的)title('Ix'的)
图图像(IY,'cdatamapping'那'缩放'的)colormap('灰色的'的)title('Iy'的)
您可以使用其他过滤器获取图像渐变,例如Sobel运算符或ProWitt运算符。有关如何使用卷积过滤图像的信息,请参阅空间域中的图像过滤是什么?(Image Processing Toolbox)
Alternatively, if you have the Image Processing Toolbox software, you can use theImfilter.
(Image Processing Toolbox)那imgradientxy.
(Image Processing Toolbox), 或者imgradient.
(Image Processing Toolbox)函数以获取图像渐变。
Define Fuzzy Inference System (FIS) for Edge Detection
Create a fuzzy inference system (FIS) for edge detection,edgefis.
。
edgefis.= mamfis('姓名'那'edgedetection');
指定图像渐变,一世X
and一世y
,作为输入的输入edgefis.
。
edgefis.= addInput(edgeFIS,[-1 1],'姓名'那'Ix');edgefis.= addInput(edgeFIS,[-1 1],'姓名'那'Iy');
Specify a zero-mean Gaussian membership function for each input. If the gradient value for a pixel is0.
那then it belongs to the zero membership function with a degree of1
。
sx = 0.1;sy = 0.1;EdgeFIS = AddMF(边缘FIS,'Ix'那“gaussmf”,[sx 0],'姓名'那'zero');EdgeFIS = AddMF(边缘FIS,'Iy'那“gaussmf”,[sy 0],'姓名'那'zero');
SX.
andSY.
specify the standard deviation for the zero membership function for the一世X
and一世y
输入。要调整边缘检测器性能,可以更改值SX.
andSY.
。增加值使算法对图像中的边缘的敏感性不太敏感,并降低检测到的边缘的强度。
Specify the intensity of the edge-detected image as an output ofedgefis.
。
edgefis = addoutput(边缘文件,[0 1],'姓名'那'iout');
指定三角形成员函数,白色和黑色Iout.
。
wa = 0.1;WB = 1;WC = 1;ba = 0;bb = 0;BC = 0.7;EdgeFIS = AddMF(边缘FIS,'iout'那'trimf'那[wa wb wc],'姓名'那'白色的');EdgeFIS = AddMF(边缘FIS,'iout'那'trimf',[bb bc],'姓名'那'黑色的');
As you can withSX.
andSY.
,您可以更改值哇
那WB.
那厕所
那BA.
那BB.
那and公元前
to adjust the edge detector performance. The triplets specify the start, peak, and end of the triangles of the membership functions. These parameters influence the intensity of the detected edges.
Plot the membership functions of the inputs and outputs ofedgefis.
。
figure subplot(2,2,1) plotmf(edgeFIS,'输入',1)标题('Ix'的)subplot(2,2,2) plotmf(edgeFIS,'输入'那2) title('Iy')子图(2,2,[3 4])plotmf(边缘fis,'输出',1)标题('iout'的)
指定FIS规则
如果它属于均匀区域和黑色,则添加规则以制作像素白色。当两个方向上的图像梯度为零时,像素在均匀区域中。如果任一方向具有非零梯度,则像素处于边缘。
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(边缘fis,[R1 R2]);edgefis.rules.
ANS = 1x2具有属性的Fisrule数组iout = black(1)“
Evaluate FIS
为每行像素进行评估边缘检测器的输出一世
using corresponding rows of一世X
and一世y
作为输入。
一世eval = zeros(size(I));为了II = 1:IEVAL(II,:) = EVALFIS(EDGEFIS,[(IX(II,:))]');结尾
绘图结果
绘制原始灰度图像。
figure image(I,'cdatamapping'那'缩放'的)colormap('灰色的'的)title('原始灰度图像'的)
绘制检测到的边缘。
figure image(Ieval,'cdatamapping'那'缩放'的)colormap('灰色的'的)title('Edge Detection Using Fuzzy Logic'的)