且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-12-09 20:45:58

我假设你有图像处理安装工具箱,或者这不起作用。此外,这假设每行文本与其他行有足够的空间。



您可以使用形态学来处理此问题。首先,拍摄图像并将其反转,使其成为黑色背景上的白色文本。执行此操作后,请使用水平线结构元素,该元素的大小与图像的宽度相同,并使用形态膨胀。这实际上将从每一行中取出字母并将它们连接在一起,以便属于同一行的所有字符都属于一个对象。执行此操作后,您将计算总行数。



首先,我将直接从***读取您的图像,但您上传的图像实际上是RGB 。因此,我将使用



请记住,黑色背景上的文字为白色。 num 将包含对象的总数,我们看到它是预期的5:

 >> num 

num =

5


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?

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 ***, 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