Code:
img = imread ('G:\Stuff\RP\Database\0001_4.jpg');
%imshow(img);
bin_img = imcomplement(im2bw(img, 0.8)); %Binarizing
%figure;
%imshow(bin_img);
bin_img = bwareaopen(bin_img, 50); %for removing dots and commas
%%%%%%%% Line Segmentation %%%%%%%%
dbw_img = imdilate(bin_img, strel('line', 100, 0));%Dilating
[L, N]=bwlabel(dbw_img); %finding connected components
bbox = regionprops(L, 'BoundingBox');
lineSlopeMatrix=[N 0];
for i=1:N %must run for all the lines in an image
bBox=bbox(i).BoundingBox;
x=bBox(1)+0.5;
y=bBox(2)+0.5;
w=bBox(3);
h=bBox(4);
linePatch=bin_img(y:y+h,:); %Extracting line
figure,imshow(linePatch) % Prints lines
words_img = imdilate(linePatch, strel('line', 40, 0));%Dilating
[R, C]=bwlabel(words_img); %finding connected components i.e. Words
bounding = regionprops(R, 'BoundingBox');
for j=1:C %nmust run for the words in a line
bdBox=bounding(j).BoundingBox;
xAxis=bdBox(1)+0.5;
yAxis=bdBox(2)+0.5;
width=bdBox(3);
height=bdBox(4);
% [row col]=size(linePatch)
% yAxis,yAxis+height,xAxis,xAxis+width
Patch=linePatch(yAxis:yAxis+height,xAxis:xAxis+width);
%Extracting Patch of Words
figure,imshow(Patch) %Prints words
Patch=[];
end
linePatch=[];
end
Issue: I am first segmenting a line from input image then extracting the words out of that line. My algo segments the words correctly out of first line (from the input image, attached with this post), then it also segments the first word from second line than it gives me the following error:
??? Index exceeds matrix dimensions.
Error in ==> test_words_lines at 33
Patch=linePatch(yAxis:yAxis+height,xAxis:xAxis+width);
I have understood the error easily, checked the dimensions of the matrix, they seem fine or maybe I can't find the problem there..
Do see the pictures attached: Segmentation of Words from Line 1// Segmentation of Words from Line 2
Please tell me the right cause of this error and suggest a fix. Thanks :)