Ana Ain
Ana Ain

Reputation: 173

How to save exact image of segmentation result (segmentation using vertical projection method)?

I've already modified a code for segmentation process. The code is as follow:

% Preprocessing + Segmentation (VP with secondary element)
% // Original Code of Vertical Projection for Segmentation by Soumyadeep Sinha //
% // Modified by Ana Ainul S. : [email protected], 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);

It gave a good result, but I have some trouble when I need to save it as a one file for each segmented image.

enter image description here

Segmented character in process.

It gives me a different result, when I save it.

enter image description here

The secondary element, that supposed to be unite with the main body of the character was being separated when I try to save it. I've tried to modified the code, but still didn't get the solution. I need to save exactly the same images that showed at the program.

Any help, would be very appreciated.

Thank you so much.

Upvotes: 1

Views: 451

Answers (1)

Shai
Shai

Reputation: 114876

You have two different processes for segmenting the characters in your code:
one is the loop for k = 1 : length(startingColumns) where you correctly segment by columns,
And a second different segmentation by connected components (bwlabel) on top of the previous one.

If I understand your needs correctly, you do not need the second bwlabel processing of each character.

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,
Good luck with your thesis ;)

Upvotes: 1

Related Questions