0

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 :)

Junaid
  • 941
  • 2
  • 14
  • 38
  • try to use the debugger and stop at line `Patch=linePatch(yAxis:yAxis+height,xAxis:xAxis+width)`. check the values of `xAxis, yAxis, etc.`. are they larger than the size of your matrix `linePatch`? – bushmills Sep 12 '16 at 12:22
  • `height`, `width`, `yAxis` and size of `linePatch`? – bushmills Sep 12 '16 at 13:21
  • @bushmills I have added the break-point with condition `i==2 && j==2` and found out that the Value of `xAxis` is 280, `yAxis` 104, `width` is 45, `height` is 5.. while the `linePatch` is 108*2465. Now, `yAxis+height`=104+5=109 which is greater than no of rows,108.. but that doesn't make sense.. Doesn't `size(linePatch)=108*2465` indicate 108 rows and 2465 columns? and the `xAxis` and `yAxis` values, indicate the pixel number? Thanks in advance :) – Junaid Sep 12 '16 at 13:25
  • @bushmills size of `linePatch`= 108 * 2465, `height` =5 , `width`=45, `yAxis`= 104 – Junaid Sep 12 '16 at 13:29

1 Answers1

0

See the following documentation for function regionprops, you are using:

http://de.mathworks.com/help/images/ref/regionprops.html#inputarg_properties

here you can read in the description of property Bounding Box, that the return values (your xAxis an yAxis) specify the upper-left corner of the bounding box in the form [x y z ...]. So adding the height to get the bounding box is wrong. You'll have to subtract the height.

bushmills
  • 673
  • 5
  • 17
  • I know what region props does. I have used the same method (addition of height and yAxis) before.. Still, I have, just tried subtraction and it returns blank images and after a while the same error.. – Junaid Sep 12 '16 at 13:57
  • please show us the code line, where you tried subtraction. Does it look like this: `Patch=linePatch(yAxis-height:yAxis,xAxis:xAxis+width);` Otherwise it would be hard, unless we can reproduce your problem. – bushmills Sep 12 '16 at 14:27