How to calculate the number of lines of text in an image in MATLAB

大憨熊 提交于 2019-12-06 16:33:56

I'm assuming you have the image processing toolbox installed, or this won't work. In addition, this assumes that each line of text has sufficient space from the other lines.

You can approach this with morphology. First, take the image and invert it so that it's white text on black background. Once you do this, use a horizontal line structure element that is the size of the width of the image, and use morphological dilation. This in effect will take letters from each line and join them together so that all characters belonging to the same line belong to one object. Once you do this, you count how many total lines there are.

First, I'll read in your image directly from StackOverflow, but the image you uploaded is actually RGB. As such, I'll convert it to binary using im2bw, then invert the image like I talked about above. The morphology logic I'm talking about assumes that objects are white on black background, which is why the inversion is needed.

Next, we create the horizontal line structuring element using strel, dilate the image with imdilate, then use bwlabel to count the total number of resulting objects and thus lines:

%// Read in image, convert to black and white and invert
im = ~im2bw(imread('http://s16.postimg.org/ih7ai6r5h/Para3.jpg'));

%// Create horizontal line structuring element
se = strel('line', size(im,2), 0);

%// Dilate the image with this structuring element
out = imdilate(im, se);

%// Count the total number of objects
[~,num] = bwlabel(out);

As a reference, this is what the processed image looks like before counting the lines:

Remember, the text is white on black background. num will contain the total number of objects, and we see that it's 5 as expected:

>> num

num =

     5

If you have the Computer Vision System Toolbox, you can use the ocr function. It will not only give you the location of each word, it will also interpret the characters.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!