Here is the code :
%{ Fuzzy Dilation and Erosion Copyright 2010 Vahid Rafiei . This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. %} % Settings len=512; useNoise=0; strel=1; %% Define v if strel==1 x=linspace(-10,10,len); v=2-(x).^2; v(v<0)=0; end v=v/max(v); plot(v,'--'); %% define mu x=linspace(-4,4,len); mu=3-x.^4+4*x.^2; mu(mu<0)=0; mu=mu/max(mu); if(useNoise) mu=mu+0.1*(rand(size(mu))-0.5); end hold on plot(mu,'-','Color','red') legend({'V','mu'}) % mu and v is ready %% Definition 1 % Dilation nalpha=10; alph=linspace(0.0001,1,nalpha); deltaAlpha=1/nalpha; Dmu=zeros(size(mu)); tic for a = alph valpha=v>=a; for x=1:numel(mu) mask=zeros(size(mu)); mask(x)=1; mask=convn(mask,valpha,'same'); Dmu(x)=Dmu(x)+max(mask.*mu)*deltaAlpha; end end toc figure(2) plot(mu,'Color', 'red'); hold on plot(Dmu,'Color','blue'); legend({'\mu','D\mu'}) title('Dilation (Def. 1)'); % Erosion Emu=zeros(size(mu)); tic for a = alph valpha=v>=a; for x=1:numel(mu) mask=zeros(size(mu)); mask(x)=1; mask=convn(mask,valpha,'same'); mask(mask==0)=NaN; Emu(x)=Emu(x)+min(mask.*mu)*deltaAlpha; end end toc figure(3) plot(mu,'Color', 'red'); hold on plot(Emu,'Color','blue'); legend({'\mu','E\mu'}) title('Erosion (Def. 1)');
Cheers ,