Saving Segmentation Result Automatically - Matlab

2019-03-02 05:19发布

Complete Segmentation code:

% Preprocessing + Segmentation
 % // Original Code of Segmentation by Soumyadeep Sinha with several modification by Ana//
 % Saving each  single segmented character as one file 
    function [s] = seg (a)

myFolder = 'D:\1. Thesis FINISH!!!\Simulasi I\Segmented Images';
% a = imread ('adv1.png');

% Binarization %
level = graythresh (a);
b = im2bw (a, level);

% Complement %
c = imcomplement (b);

% Morphological Operation - Dilation %
se = strel ('square', 1);
% se = strel('rectangle', [1 2]);
r = imerode(c, se); 

i=padarray(r,[0 10]);
% i=padarray(c,[0 10]);

% Morphological Operation - Dilation %
% se = strel('rectangle', [1 2]);
% se = strel ('square', 1);
% 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] =  normalization (s); 

%  se = strel ('square', 1);
%  se = strel('rectangle', [2 1]);
%  im = imdilate(p, se); 

   % 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('data.%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);
  • I have loaded images into matlab workspace to do a character segmentation process for word images. Ex: data(1).png, data(2).png, and so on.
  • The segmentation process, will give multiple image as output for every segmented character. Word images contain various amount of character, so the output also will vary. For example, Output of segmented result for image = data(1).png become data(1)_1.png, data(1)_2.png, data(1)_3.png, and data(2).png become data(2)_1.png, data(2)_2.png and so on.

Word images

enter image description here

Lately, I was did it manually, but the data set will be bigger and it so wasting time to run segmentation for one by one images. Is there any suggestion, how should I do to make it simple and more effective? Get the result for every segmented character (in sequence).

    % Save %
    [L,num] = bwlabel(im);
    for z= 1 : num
    bw= ismember( L, z);
    % Construct filename for this particular image.
    % Change basefilename for each word images %
    baseFileName = sprintf('data (1).%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

after using this code, it create a good result, but just for one data, the next data will replace the recent data. So, lately, for every word image, I run the segmentation process one by one and change this part to get an appropriate result. Change sprintf('data (1).%d.png', y) to become sprintf('data (2).%d.png', y); and so on.

% Change basefilename for each word images %
    baseFileName = sprintf('data (1).%d.png', y);
    y=y+1;

The result that I hope. I hope, I can get it automatically. result

Any help will be very appreciated.

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-02 05:43

Since Arabic is my native language I will help you in this. Let me start by making clear : that some Arabic letters contains non-connected regions. Because of this using image processing techniques will not be enough. Few years ago I designed a system that takes advantage of the idea: regions related to the same letter are either above or below the letter. The steps :

  1. Convert the image to binary.
  2. Complement the image. The texts is white and the background is black
  3. Process the image to divide it into multiple rows. This can be done by performing projection onto the vertical axis. Valleys will be related to spaces between lines.
  4. Each line will be proceeds desperately : Regions detection. All regions above or below each other will be segmented together

If the text is written by people " hand written" the problem will become more complex. Then you need a machine learning solution to verify the segmented regions.

查看更多
登录 后发表回答