分割字符图像的大小归一化
Size Normalization for segmented character images
我已经使用padarray进行了归一化(代码如下),但是下一个过程(特征提取)的结果还不够好。因为它不完全是segmented part features,还包括pad part。
-样本图像(分割字符)
我只需要对图像的分割字符进行归一化,将其居中并平方(应该是[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
修改了这段代码,还是不行。因此,我需要对此代码修改的建议或针对此案例的任何推荐方法。
任何帮助将不胜感激。谢谢。
不能 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;
结果:
这就是你想要的吗?
我已经使用padarray进行了归一化(代码如下),但是下一个过程(特征提取)的结果还不够好。因为它不完全是segmented part features,还包括pad part。
我只需要对图像的分割字符进行归一化,将其居中并平方(应该是[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
修改了这段代码,还是不行。因此,我需要对此代码修改的建议或针对此案例的任何推荐方法。
任何帮助将不胜感激。谢谢。
不能 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;
结果:
这就是你想要的吗?