分段字符图像的大小标准化

时间:2016-07-22 17:22:59

标签: matlab normalization preprocessor image-segmentation

我已经使用padarray进行了规范化(代码如下),但下一个进程(特征提取)的结果不够好。因为它不完全是分段部件的功能,还包括垫部件。

enter image description here - 样本图像(分段字符)

我只需要对图像的分段字符进行标准化,将其置于中心位置,并将其平方(它应该是[64 64])。它还应该保留纵横比,而不会拉伸或扭曲图像,因此字符图像将保持成比例。

% 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

对此代码进行了一些修改,仍然无效。因此,我需要对此代码修改或此案例的任何推荐方法提出建议。

非常感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

不是100%肯定这就是你所追求的,但这里是:

示例:(在路径中假设图像'alif.png','dod.png','ha.png'和'uau.png'。)

%%%% 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' %%%%

然后致电:

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;

结果:

enter image description here

这是你追求的那种东西吗?