2016-02-17 36 views
0

分割代码:分割,读字图像从右到左

% // 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); 

原图:word image

分割输出:saved output of segmentation

我已经做了分割为孤立字符做OCR。它运行良好, 但是,我希望分割的输出顺序不是从左到右,而是从右到左,就像我们读阿拉伯文字一样。

如果一切正常,enter image description here将被保存为word6.1.png,enter image description here保存为word6.2.png,其余保存为word6.3.png

我不知道该怎么做代码,使其从右向左阅读单词。

任何帮助将不胜感激。

+0

是否可以将输入从左向右翻转例如使用'fliplr',从左到右运行算法,然后再次翻转输出? – mikkola

回答

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增量。

+0

谢谢,但是,它使得输出只停留在一个文件图像中,分割结果应该保存在每个分段字符的不同文件图像中。 –