function x = microclassifier(CI, row, col, image, blocksize)

matrix=image((row-1)*blocksize+1:row*blocksize, (col-1)*blocksize+1:col*blocksize);


% Background test
tolerance_bk = 0.1 * 255;

if (CI(row,col,1) == inf) | (abs((mean(mean(matrix)) - 255)) <= tolerance_bk)
   x = 3;
   return
end

% Set the row,col of neighboring blocks
adj_col = [col col col-1 col+1];
adj_row = [ row-1 row+1 row row];

% text positive - 1
% image positive - 2
type_relative = [];

% Set tolerance for bi-level (text) and mean-sd (picture) comparison
tolerance_text = 80;
tolerance_image = 50;

% Run through all 4 neighbours
for n = 1:4
   
if ((adj_row(n) > 0) & (adj_col(n) > 0) &(adj_col(n) < size(CI,2)) & (adj_row(n) < size(CI,1)))
   
   % If neighbor is text
   if (CI(adj_row(n),adj_col(n),2) == 2)
 	  if ((CI(row,col,5) ~= -1) & (CI(row,col,6) ~= -1) & ...
         (abs((CI(row,col,5) - CI(adj_row(n), adj_col(n), 5))) <= tolerance_text) & ...
         (abs((CI(row,col,6) - CI(adj_row(n), adj_col(n), 6)))  <= tolerance_text))
      
   	   type_relative = [type_relative 2];
      end
      
   % If neighbor is image   
 	elseif (CI(adj_row(n),adj_col(n),2) == 1)
    if (abs((CI(row,col,3)- CI(adj_row(n), adj_col(n),3))) <= tolerance_image)
       type_relative = [type_relative 1];
    end
    
   % Unclassified 
 	else
    type_relative = [type_relative 4];
 	end

else
   type_relative = [type_relative 0];
end

% With 4 neighbors, the block can be classified as either picture or text with respect
% to the context information of the four neighbors.

% An image is classified as a text if the context information of any of the 4 neighbors points to
% it being text

%Default unclassified
x = 4;

for u = 1:length(type_relative)
   if (type_relative(u) == 1)
      x = 1;
   end
end

for u = 1:length(type_relative)
   if (type_relative(u) == 2)
      x = 2;
   end
end

   

end

    
         
 
         
         
   




