% APRENDIZAJE AUTOMÁTICO (G. Pajares y J. M. de la Cruz, Eds.)
% Editorial: RA-MA
% Capítulo 11: Razonamiento Basado en Casos
% Autores: Ángela Ribeiro, Xavier P. Burgos Artizzu y Jesús M. Conesa Muñoz

% Segmentación, método S2
% Analysis of natural images processing for the extraction of agricultural
% elements. Image Vision Comput. 28(1): 138-149 (2010)
function im = S2(A,R,G,B,light,shadows)
 [f,c,x]=size(A);
 im = ones(f,c);
 mini = 255;
 maxi = 0;
 
 A=A*255;
 
 im = im*6;
 
 r = A(:,:,1);
 g = A(:,:,2);
 b = A(:,:,3);
 
 r2 = zeros(f,c);
 g2 = zeros(f,c);
 b2 = zeros(f,c);
 
 r2(r>g)=r2(r>g)+1;
 r2(r>b)=r2(r>b)+1;
 
 g2(g>r)=g2(g>r)+1;
 g2(g>b)=g2(g>b)+1;
 
 b2(b>g)=b2(b>g)+1;
 b2(b>r)=b2(b>r)+1;
 
 im(r2==R)=im(r2==R)-1;
 im(g2==G)=im(g2==G)-1;
 im(b2==B)=im(b2==B)-1;
 
 maxi = max(r,g);
 maxi = max(maxi,b);
 
 mini = min(r,g);
 mini = min(mini,b);
 
 im(mini<=light)=im(mini<=light)-1;
 im(maxi>=shadows)=im(maxi>=shadows)-1;
 
 im(im~=1)=0;