% BRODATZ_MAKE Computes feature description of Brodatz textures.
%
% Procedure of computing feature description:
% - Each texture image is divided into patches.
% - The patches are histogram equalized and number of gray-lavels 
%   is reduced. 
% - The co-occurrence matrices are computed from the normalized patches.
% - The entries of the co-occurence matrix are reshaped column
%   feature vector.
%
% Resulting data:
%  X [dim x num_data] Feature description of Brodatz textures.
%  y [1 x num_data] Labels identifying the textures (1,2,...,13).
%  P [(Patch_Size^2) x num_data] Patches of size [Patch_Size x Patch_Size] 
%    represented as column vectors. Use imshow(reshape(patch(:,i),h,w)) 
%    to display the i-th patch.
%   
% Control paramaters:
%  The control constants are defined at the begining of this file.
%
%  Max_Levels [1x1] Number of gray-lavels after patch normalization. 
%  Patch_Size [1x1] Defines size of patches to which the texture is 
%   divided.
%  Patch_X [1 x num_x] X-coordinate of left-top point of the patches.
%  Patch_Y [1 x num_y] Y-coordinate of left-top point of the patches.
%  File_Names [string array] File names of the texture images.
%

% Modifications:
% 25-aug-2004, VF

Max_Levels = 8;
Num_Dist = 2;
Patch_Size = 32;
Patch_X = [1:20:(512-32)];
Patch_Y = [1:10:(256-32)];  % training set 
%Patch_Y = 255+[1:10:(256-32)];  % testing set

File_Names = {'../data/1.1.01.tiff', '../data/1.1.02.tiff', ...
      '../data/1.1.03.tiff', '../data/1.1.04.tiff', ...
      '../data/1.1.05.tiff', '../data/1.1.06.tiff',...
      '../data/1.1.07.tiff', '../data/1.1.08.tiff', ...
      '../data/1.1.09.tiff', '../data/1.1.10.tiff', ...
      '../data/1.1.11.tiff', '../data/1.1.12.tiff',...
      '../data/1.1.13.tiff'};

X = [];  % features
y = [];  % labels
P = [];  % images

% For each texture image
for j=1:length(File_Names),
  
  img = imread(File_Names{j});
  fprintf('File: %s, size %dx%d', File_Names{j},size(img,2),size(img,1));

  patches = uint8(zeros(Patch_Size^2,length(Patch_X)*length(Patch_Y)));
  cnt = 0;
  for col=Patch_X,
    for row=Patch_Y,
      cnt = cnt + 1;
      
      win = img(col:col+Patch_Size-1,row:row+Patch_Size-1);
      patches(:,cnt) = win(:);
    end      
  end
    
  fprintf(', num_patches=%d\n', size(patches,2));
  
  features = zeros(4*Num_Dist*Max_Levels^2, size(patches,2));
  for i=1:size(patches,2),
    fprintf('.');

    % take a patch 
    win = reshape(patches(:,i),Patch_Size,Patch_Size);
    
    % normalize patch
    win = imgnorm(win,Max_Levels);

    % compute co-occurrence matrices and use them as features
    com = cooccurrence(win,Max_Levels,Num_Dist);
    com = com(:);

    % store features
    if isempty(features), features = zeros(length(com),size(patches,2)); end
    features(:,i) = com;
  
  end
  fprintf('\n');
  
  X = [X, features];
  y = [y, j*ones(1,size(features,2))];
  P = [P, patches];
end

save brodatz_trn X y P Max_Levels Patch_Size Patch_X Patch_Y File_Names;

%EOF
