简体   繁体   English

如何在MATLAB中计算图像中的文本行数

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

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. 像本参考文献一样,有5行。

参考图片

How do I do this in MATLAB? 如何在MATLAB中执行此操作?

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. 首先,我将直接从StackOverflow中读取图像,但是您上传的图像实际上是RGB。 As such, I'll convert it to binary using im2bw , then invert the image like I talked about above. 这样,我将使用im2bw将其转换为二进制im2bw ,然后像上面提到的那样反转图像。 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: 接下来,我们创建使用水平线结构元素strel ,扩张与图像imdilate ,然后使用bwlabel计数得到的对象,因此线的总数量:

%// 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将包含对象的总数,并且我们看到它是预期的5:

>> num

num =

     5

If you have the Computer Vision System Toolbox, you can use the ocr function. 如果您拥有计算机视觉系统工具箱,则可以使用ocr功能。 It will not only give you the location of each word, it will also interpret the characters. 它不仅会给您每个单词的位置,还会解释字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM