问题
I have some images which have single lines or multiple lines of text. I want to calculate the number of lines. Like in a this reference, there is 5 lines.
How do I do this in MATLAB?
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/33706254/how-to-calculate-the-number-of-lines-of-text-in-an-image-in-matlab