细分代码:
% // Original Code by Soumyadeep Sinha
% Modified by Ana with several modification //
% Saving each single segmented character as one file
function [s] = seg (a)
myFolder = 'D:\1. Thesis FINISH!!!\Data set\trial';
% a = imread ('adv1.png');
% Binarization %
level = graythresh (a);
bw = im2bw (a, level);
% Complement %
b = imcomplement (bw);
% Morphological Operation - Dilation %
% se = strel('rectangle', [1 2]);
% r = imdilate(b, se);
r=padarray(b,[0 10]);
% % Morphological Operation - Dilation %
se = strel('rectangle', [1 2]);
i = imerode(r, se);
%VP
verticalProjection = sum(i, 1);
set(gcf, 'Name', 'Segmentation Trial', 'NumberTitle', 'Off')
subplot(2, 2, 1);imshow(i);
subplot(2,2,3);
plot(verticalProjection, 'b-');
grid on;
t = verticalProjection;
t(t==0) = inf;
mayukh=min(t)
% 0 where there is background, 1 where there are letters
letterLocations = verticalProjection > mayukh;
% Find Rising and falling edges
d = diff(letterLocations);
startingColumns = find(d>0);
endingColumns = find(d<0);
% Extract each region
y=1;
for k = 1 : length(startingColumns)
% Get sub image of just one character...
subImage = i(:, startingColumns(k):endingColumns(k));
% im = subImage;
s = subImage;
% figure, imshow (s);
% Normalization %
[p] = pad (s);
% Morphological Operation - Thinning %
im = bwmorph(p,'thin',Inf);
% Save %
[L,num] = bwlabel(im);
for z= 1 : num
bw= ismember( L, z);
% Construct filename for this particular image.
baseFileName = sprintf('word6a.%d.png', y);
y=y+1;
% Prepend the folder to make the full file name.
fullFileName = fullfile(myFolder, baseFileName);
% Do the write to disk.
imwrite(bw, fullFileName);
subplot(2,2,4);
pause(1);
imshow(bw);
end
% y=y+1;
end;
s = (im);
我已经完成了隔离字符的分段以进行OCR。它运行良好, 但是,我希望分段的输出顺序排列不是从左到右,而是从右到左排列,就像我们读阿拉伯语单词一样。
如果有效,将保存为word6.1.png,保存为word6.2.png,其余保存为word6.3.png
我不知道如何处理代码,让它从右到左阅读。
非常感谢任何帮助。
答案 0 :(得分:0)
如果我正确阅读了您的代码,%Save%
部分的以下更改应该可以解决问题:
% Save %
[L,num] = bwlabel(im);
for z= 1 : num
bw= ismember( L, z);
% Construct filename for this particular image.
baseFileName = sprintf('word6a.%d.png', num-z+1); %% <--- just make the numbering start backwards
y=y+1;
% Prepend the folder to make the full file name.
fullFileName = fullfile(myFolder, baseFileName);
% Do the write to disk.
imwrite(bw, fullFileName);
subplot(2,2,4);
pause(1);
imshow(bw);
end
此外,可能不再需要y
的增量。