How to solve "Index exceeds matrix dimensions" error in segmentation process (matlab)

1 view (last 30 days)
Hello everyone,
I have segmentation code as follows. I've tried to segment all 300 word images, into characters images using vertical projection method. But I get "Index exceeds matrix dimensions" error, for k.
for z = 1: 300
name1 = strcat ('data (',int2str(z),').png');
name2 = strcat ('D:\1. Thesis FINISH!!!\Data set\0 Isolated Dataset\Advertising Bold 14\source\', name1);
a = imread (name2);
myFolder = 'D:\1. Thesis FINISH!!!\Data set\0 Segmented Character\coba';
%%Binarization %%
level = graythresh (a);
b = im2bw (a, level);
%%Complement %%
c = imcomplement (b);
%%PadArray %%
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;
% *Defining the threshold to determine baseline area* %
threshold=max(verticalProjection)/3;
% threshold=min(verticalProjection)/3;
% threshold = 5; % Threshold >0 used to detect the baseline of cursive characters
thresholdedProjection=verticalProjection > threshold;
count=0;
startingColumnsIndex=0;
for j =1:length(thresholdedProjection)
if thresholdedProjection(j)
if(count>0)
startingColumnsIndex=startingColumnsIndex+1;
startingColumns(startingColumnsIndex)= j-floor(count/2);
count=0;
end
else
count=count+1;
end
end
endingColumns=[startingColumns(2:end)-1 j-floor(count/2)];
% *Extract each region, result of segmentation process* %
y=1;
for k = 1 : length(startingColumns)
% Get sub image of just one character
subImage = i(:, startingColumns(k):endingColumns(k));
% im = subImage;
s = subImage;
Because I thought it's about the dimension, so i've tried to exchange
subImage = i(:, startingColumns(k):endingColumns(k));
become
subImage = i(startingColumns(k):endingColumns(k),:);
But nothing change. I have no Idea, what's wrong, which part I should modified to get it segmented correctly?
Any help and explanation would be very appreciated. Thank You so much.

Accepted Answer

Walter Roberson
Walter Roberson on 1 Aug 2016
You need to initialize startingColumn to [] for each image.
  1 Comment
ana ain
ana ain on 1 Aug 2016
Thank you sir. I've add startingColumns = []; That was good, it segmenting all the images. But, I have a trouble again, to save all the result of the segmentation (segmented character). It just save the last segmented character in every images. Is there any wrong code again? Could you please help me again.
for z = 1: 300
name1 = strcat ('data (',int2str(z),').png');
name2 = strcat ('D:\1. Thesis FINISH!!!\source\', name1);
a = imread (name2);
myFolder = 'D:\1. Thesis FINISH!!!\coba';
%%Binarization %%
level = graythresh (a);
b = im2bw (a, level);
%%Complement %%
c = imcomplement (b);
%%PadArray %%
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;
% *Defining the threshold to determine baseline area* %
threshold=max(verticalProjection)/3;
% threshold = 0;
% threshold=min(verticalProjection)/3;
% threshold = 5; % Threshold >0 used for cursive characters
thresholdedProjection=verticalProjection > threshold;
count=0;
startingColumns = [];
startingColumnsIndex=0;
for j =1:length(thresholdedProjection)
if thresholdedProjection(j)
if(count>0)
startingColumnsIndex=startingColumnsIndex+1;
startingColumns(startingColumnsIndex)= j-floor(count/2);
count=0;
end
else
count=count+1;
end
end
endingColumns=[startingColumns(2:end)-1 j-floor(count/2)];
% *Extract each region, result of segmentation process*
for k = 1 : length(startingColumns)
% Get sub image of just one character
subImage = i(:, startingColumns(k):endingColumns(k));
% im = subImage;
s = subImage;
% Normalization using algorithm 2 %
p = normalization2 (s);
subplot(2,2,2);
imagesc (p);
axis equal off;
pause (1);
% figure,
imshow (p);
% Morphological Operation - Thinning %
t = bwmorph(p,'thin',Inf);
end
% savename = char (strcat (myFolder, name1));
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(t, fullFileName);
% imwrite (t, fullfile (myFolder, sprintf ('data.%d.png', y)));
% y = y+1;
end;
It just save one character for every words images. even though every images contain more than three characters. I've set a loop to save it sequentially, but i think, there's some lack in the code. I need help and explanation.
Thanks in advance.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!