如何保存分割结果的精确图像(使用垂直投影法进行分割)?

时间:2016-07-14 03:46:08

标签: matlab image-processing computer-vision image-segmentation text-segmentation

我已经修改了用于细分过程的代码。代码如下:

% Preprocessing + Segmentation (VP with secondary element)
% // Original Code of Vertical Projection for Segmentation by Soumyadeep Sinha //
% // Modified by Ana Ainul S. : anaainul@gmail.com, Last modified : 14/07/16 //
% Saving each  single segmented character as one file 

function [ss] = segment (a)
myFolder = 'D:\1. Thesis FINISH!!!\Data set';

%% Binarization %%
level = graythresh (a);
b = im2bw (a, level);
%% Complement %
c = imcomplement (b);
i=padarray(c,[0 10]);

% Vertical Projecttion for Character Segmentation
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)); 
  s = subImage;
  figure, imshow (s);

  % Save each segmented characters %
  [L,num] = bwlabel(s);
  for z = 1 : num
     bw= ismember(L, z);
     % Construct filename for this particular image.
     baseFileName = sprintf('data1.%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);
   end
end;
ss = (s);

它给出了一个很好的结果,但是当我需要将它保存为每个分段图像的一个文件时,我遇到了一些麻烦。

enter image description here

正在处理的分段字符。

当我保存它时,它会给我一个不同的结果。

enter image description here

当我尝试保存它时,应该与角色主体结合的次要元素被分开。我试图修改代码,但仍然没有得到解决方案。 我需要保存与程序中显示的完全相同的图像。

任何帮助,都将非常感激。

非常感谢你。

1 个答案:

答案 0 :(得分:1)

您可以使用两种不同的流程来细分代码中的字符:
一个是循环for k = 1 : length(startingColumns),您可以按列正确分段,
并且通过连接组件(bwlabel)进行第二次不同分段。

如果我理解您的需求,则不需要对每个字符进行第二次bwlabel处理。

for k = 1 : length(startingColumns)
    % Get sub image of just one character...
    subImage = i(:, startingColumns(k):endingColumns(k)); 
    s = subImage;
    figure, imshow (s);
    imwrite( s, fullfile( baseFolder, sprintf('data.%d.png', k ) ) );
end

PS,
祝你的论文好运;)