% 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

% Eliminación de líneas de cultivo, método E2
% Analysis of natural images processing for the extraction of agricultural
% elements. Image Vision Comput. 28(1): 138-149 (2010)
function [aux,colI2,colD1,aux2] = E2(A,tam,totIni)
  [f,c] = size(A)
  aux = A;
  colD1 = 1;
  colI2 = 2;
  
  D1 = 1;
  I2 = 1;
  
  aux2 = zeros(f,c,3);
  aux2(:,:,1) = A;
  aux2(:,:,2) = A;
  aux2(:,:,3) = A;
    
  v = sum(aux(:,:));
  maximo = max(v);
  ptoIni = find(v==maximo);
  ptosI = find(ptoIni<c/2);
  if(length(ptosI)>0)
      ptoIni = ptoIni(1,length(ptosI));
  else
      ptoIni = ptoIni(1,1);
  end;
  bien = true;
  
  if(ptoIni>=c/2)
      [aux,D1,aux2] = fCIzq(aux,aux2,ptoIni,tam,totIni);
      colD1 = ptoIni;  
      aux(:,floor(colD1):c)=0;
      bien = false;
  else
      [aux,I2,aux2] = fCDer(aux,aux2,ptoIni,tam,totIni);
      colI2 = ptoIni;
      aux(:,1:ceil(colI2))=0;
  end;
  aux2(:,ptoIni,1)=255;
  aux2(:,ptoIni,2)=255;
  aux2(:,ptoIni,3)=0;
   
  v = sum(aux(:,1:c));
  maximo = max(v);
  ptoIni = find(v==maximo);
  [fi,ci] = size(ptoIni);
  ptosI = find(ptoIni<c/2);
  ptosD = find(ptoIni>=c/2);
  malvamos = false;
  
  if(bien)
    if(length(ptosD)>0)
      ptoIni = ptoIni(1,ptosD(1,1));
    else
      ptoIni = ptoIni(1,ci);
    end;    
  else
    if(length(ptosI)>0)
      ptoIni = ptoIni(1,ptosI(length(ptosI)));
    else %va a procesar 2 veces la misma linea de cultivo
      ptoIni = ptoIni(1,1);
    end;
  end;
  
  if(ptoIni>=c/2)
      [aux,D1,aux2] = fCIzq(aux,aux2,ptoIni,tam,totIni);
      colD1 = ptoIni;
      aux(:,floor(colD1):c)=0;
  else
      [aux,I2,aux2] = fCDer(aux,aux2,ptoIni,tam,totIni);
      colI2 = ptoIni;
      aux(:,1:ceil(colI2))=0;
  end;
  
  if(D1>I2)
        colD1 = D1;
        colI2 = I2;
  end;