function [Cell,imout,CV,CV_ind]=CA(image,k,d,r)
%Cells - basic cells in cellular automata
%type - 1 edge
%type - 2 focus
%type - 3 noisy
%type - 4 uncertain
%V - Neighbourhood cells
[l,b,N]=size(image);
d=d*N; %if it is an RGB image
Z=l*b; %number of cells
thr=10;
% CV = initCV(image,k);
% Initialization of cells
%========================
for ii=1:Z
Cell{ii}.class=round(0.5+rand*(k));
%Selecting neighbourhood cells
Cell{ii}.V=[];
if ((ii-1)>0)&&(mod(ii-1,l)~=0)
Cell{ii}.V=[Cell{ii}.V ii-1];
end
if ((ii+1)<Z)&&(mod(ii+1,l)~=0)
Cell{ii}.V=[Cell{ii}.V ii+1];
end
if ((ii-l)>0)&&(mod(ii-l,l)~=0)
Cell{ii}.V=[Cell{ii}.V ii-l];
end
if ((ii+l)<Z)&&(mod(ii+l,l)~=0)
Cell{ii}.V=[Cell{ii}.V ii+l];
end
if r==8
if ((ii-1-l)>0)&&(mod(ii-1-l,l)~=0)
Cell{ii}.V=[Cell{ii}.V ii-1-l];
end
if ((ii+1-l)>0)&&(mod(ii+1-l,l)~=0)
Cell{ii}.V=[Cell{ii}.V ii+1-l];
end
if ((ii-1+l)<Z)&&(mod(ii-1+l,l)~=0)
Cell{ii}.V=[Cell{ii}.V ii-1+l];
end
if ((ii+1+l)<Z)&&(mod(ii+1+l,l)~=0)
Cell{ii}.V=[Cell{ii}.V ii+1+l];
end
end
Cell{ii}.type='uncertain';
Cell{ii}.G=0;
Cell{ii}.Q=[];
end
%Storing image value in cell
Cell_ind=1;
for ii=1:l
for jj=1:b
Cell{Cell_ind}.Value=image(ii,jj,:);
Cell_ind=Cell_ind+1;
end
end
%Generatingrandom colors for classification
clr=zeros(k,N);
for ii=1:k
clr(ii,:)=[round(rand*255) round(rand*255) round(rand*255)];
end
%MAIN LOOP
%==========
for iter=1:10
iter
%Calculate centroid value
[CV,CV_ind]=CVCalc(Cell,k,N);
%Create sample set
sample=zeros(1,Z);
for ii=1:Z
sample(ii)=ii;
end
while isempty(sample)~=1
%select a random sample
x=randsample(sample,1);
%Check first instance in iteration
if Cell{x}.G==0
%find first instance neighbours
Neigh=Cell{x}.V;
Rem=[];
for kk=1:length(Neigh)
if Cell{Neigh(kk)}.G==0
Rem=[Rem Neigh(kk)];
end
end
Neigh=Rem;
%Find minimal spectral distance with centroid
for jj=1:N
X(jj)= Cell{x}.Value(jj);
end
for ss=1:k
Ref_Dist(ss)=mean(abs(X-(CV(ss))));
end
[~, NearClass]=min(Ref_Dist);
Cell{x}.Class=NearClass;
if Ref_Dist<thr
Cell{x}.Q=[Cell{x}.Q Cell{x}.Class];
end
%Compare spectral distance and classify
for kk=1:length(Neigh)
XD=SVCalc(Cell{x},Cell{Neigh(kk)});
if Ref_Dist> XD
Cell{Neigh(kk)}.class =Cell{x}.class;
if XD<thr
Cell{Neigh(kk)}.Q=[Cell{Neigh(kk)}.Q Cell{Neigh(kk)}.class];
end
end
Cell{Neigh(kk)}.G=1; %prevent repetition
sample=sample(sample~=Neigh(kk));
end
%remove the entries from the sample set
sample=sample(sample~=x);
end
end
%Reset all the prevention values
for ii=1:Z
Cell{ii}.G=0;
end
end
for ii=1:Z
Cell{ii}.Q=unique(Cell{ii}.Q);
end
%Classify type
Cell=CellType(Cell);
%Form final image
imout = ImSynth(Cell,l,b,N,clr);
end
评论0