Friday, June 17, 2016

MATLAB SOM Code - Self Organizing Maps Character recognition

SOM is a type of Artificial Neural Network , I've used this for character recognition along with character segmentation in Matlab 
Here goes the code for SOM , below code will train the network for Alphabets  

f=imread('009.jpg');
BW1 = roicolor(f(:,:,1),180,255);
BW2 = roicolor(f(:,:,2),180,255);
BW3 = roicolor(f(:,:,3),180,255);
b=~(BW1|BW2|BW3);
se1=[0 1 0 ; 1 1 1 ;0 1 0];
b=imdilate(b,se1);
s=size(b);
p=1;q=1;start=1;maxe(1)=0;
for i=1:s(1,2)
    count=0;

    for j=30:s(1,1)
        if(b(j,i)==1)
            count=count+1;
  
        end
        if(start==0)
            if(maxe(p)<count)
                maxe(p)=count;
                loc(p)=i-L(q-1);
            end
        end
        histo1(i)=count;
    end

    if(count>0 && start==1)
         
        
         L(q)=i;
          q=q+1;
          start=0;
     end
     if(count==0 && start==0)
         R(p)=i;
         p=p+1;
         start=1;
         maxe(p)=0;
     end
        
end

p=1;q=1;start=1;maxe1(1)=0;
for i=20:s(1,1)-20
    count=0;
    for j=10:s(1,2)-20
        if(b(i,j)==1)
            count=count+1;
        end
        if(start==0)
            if(maxe1(p)<count)
                maxe1(p)=count;
            end
        end
        histo2(i)=count;
    end
    if(count>0 && start==1)
         
        
         L1(q)=i;
          q=q+1;
          start=0;
     end
     if(count==0 && start==0)
         R1(p)=i;
         p=p+1;
         start=1;
         maxe1(p)=0;
     end
end

data(:,1)=double(reshape(imresize(b(L1(1):R1(1),L(1):R(1)),[25,25]),625,1));
data(:,2)=double(reshape(imresize(b(L1(1):R1(1),L(2):R(2)),[25,25]),625,1));
data(:,3)=double(reshape(imresize(b(L1(1):R1(1),L(3):R(3)),[25,25]),625,1));
data(:,4)=double(reshape(imresize(b(L1(1):R1(1),L(4):R(4)),[25,25]),625,1));

totalW = 100;
[I,J] = ind2sub([10, 10], 1:100);
N = size(data,2);
w = rand(625, totalW);
eta0 = 50;
etaN = eta0;
tau2 = 100;


sig0 = 1;
sigN = sig0;
tau1 = 10;

for po=1:100
    for j=1:N

      x = data(:,j);
       dist = sum( sqrt((w - repmat(x,1,totalW)).^2),1);
       [v ind] = min(dist);
        
      ri = [I(ind), J(ind)];
      dist = 1/(sqrt(2*pi)*sigN).* exp( sum(( ([I( : ), J( : )] - repmat(ri, totalW,1)) .^2) ,2)/(-2*sigN))*etaN;

      for rr = 1:100
           w(:,rr) = w(:,rr) + dist(rr).*( x - w(:,rr));
      end
     
    end
    etaN = eta0 * exp(-po/tau2);
    sigN = sig0*exp(-po/tau1); 
     
   
   if mod(po,100) == 1
        figure;
        axis off;
       hold on;
       for ab = 1:100
            
            subplot(10,10,ab);
            axis off;
            imagesc(reshape(w(:,ab),25,25));
            
         axis off;
       end
       hold off;
   end

end




   
        

2 comments:

  1. Attempted to access L(2); index out of bounds because numel(L)=1.

    Error in SOM_char (line 73)
    data(:,2)=double(reshape(imresize(b(L1(1):R(1),L(2):R(2)),[25,25]),625,1));

    i am facing this error can you solve it????

    ReplyDelete
  2. Undefined function 'R1' for input arguments of type 'double'.

    Error in SOM_char (line 72)
    data(:,1)=double(reshape(imresize(b(L1(1):R1(1),L(1):R(1)),[25,25]),625,1));

    why the error is ... can you solve it thanks in advance......

    ReplyDelete