Ana Ain
Ana Ain

Reputation: 173

Size Normalization for segmented character images

I've already do a normalization using padarray (code as follows), but the result for the next process (feature extraction) isn't good enough. Because it's not exactly the segmented part features, but also including the pad part.

enter image description here -sample image (segmented characters)

I need to normalize only the segmented character of images, put it centered, and squared (it supposed to be [64 64]). It also should preserve the aspect ratio, without stretching or distorting the image, so the character image will be keep proportional.

% Normalization done using pad
function p = pad (im)
nrows = size(im,1);
ncols = size(im,2);

d = abs(ncols-nrows);    % difference between ncols and nrows:
if(mod(d,2) == 1)        % if difference is an odd number
    if (ncols > nrows)   % we add a row at the end
        im = [im; zeros(1, ncols)];
        nrows = nrows + 1;
    else                 % we add a col at the end
        im = [im zeros(nrows, 1)];
        ncols = ncols + 1;
    end
end

if ncols > nrows
    im = padarray(im, [(ncols-nrows)/2 0]);
else
    im = padarray(im, [0 (nrows-ncols)/2]);
end

im = imresize(im, [64 64]);

% figure, imshow (im);

p = (im);

% Here im is a 5x5 matix, not perfectly centered 
% because we added an odd number of columns: 3
% Original code by Sembei Norimaki, modified by Ana

Some modification of this code, still not work. So, I need a suggestion for this code modification or any recommended method for this case.

Any Help would be very appreciated. Thank You.

Upvotes: 0

Views: 289

Answers (1)

Tasos Papastylianou
Tasos Papastylianou

Reputation: 22235

Not 100% sure that's what you're after, but here goes:

Example: (assumes images 'alif.png', 'dod.png', 'ha.png', and 'uau.png' in the path).

%%%% in file 'processLetter.m' %%%%
function L = processLetter (L)
  %% Step 1 : Trim padding.
  tmp = find (L); % Get linear indices of nonzero elements
  [Row_subs, Col_subs] = ind2sub (size (L), tmp); % Convert to row / col subscripts
  L = L(min (Row_subs) : max (Row_subs), min (Col_subs) : max (Col_subs)); % trim

  %% Resize such that the largest dimension is scaled to 64 pixels
  Rows = size (L, 1); Cols = size (L, 2);
  if Rows > Cols; Resize_vec = [64, NaN]; 
  else            Resize_vec = [NaN, 64]; 
  end

  L = imresize (L, Resize_vec);
  Rows = size (L, 1); Cols = size (L, 2);

  %% Pad smallest dimension to 64 pixels
  if Rows > Cols; 
    LeftPad  = abs (floor ((64 - Cols) / 2 ));
    RightPad = abs (floor ((Cols - 64) / 2 ));
    L = padarray (L, [0, LeftPad ], 'pre' );
    L = padarray (L, [0, RightPad], 'post');
  else
    TopPad    = abs (floor ((64 - Rows) / 2 ));
    BottomPad = abs (floor ((Rows - 64) / 2 ));
    L = padarray (L, [TopPad,    0], 'pre' );
    L = padarray (L, [BottomPad, 0], 'post');
  end 

  L = mat2gray (L);
  L = L > 0.5;  % in case L was a 'double' matrix -- needed in Octave
end
%%%% end of file 'processLetter.m' %%%%

Then call with:

Alif = imread ('alif.png'); Dod  = imread ('dod.png'); 
Ha   = imread ('ha.png'  ); Uau  = imread ('uau.png');
Alif = double (Alif); Dod = double (Dod); Ha = double (Ha); Uau = double (Uau); % if using octave -- octave 'imresize' function throws an error if image is logical instead of double
subplot (2, 4, 1); imagesc (Alif); axis equal off; colormap gray;
subplot (2, 4, 2); imagesc (Dod ); axis equal off;
subplot (2, 4, 3); imagesc (Ha  ); axis equal off;
subplot (2, 4, 4); imagesc (Uau ); axis equal off;
subplot (2, 4, 5); imagesc (processLetter (Alif)); axis equal off;
subplot (2, 4, 6); imagesc (processLetter (Dod) ); axis equal off;
subplot (2, 4, 7); imagesc (processLetter (Ha)  ); axis equal off;
subplot (2, 4, 8); imagesc (processLetter (Uau) ); axis equal off;

Result:

enter image description here

Is this the kind of thing you were after?

Upvotes: 2

Related Questions