为整个单词而不是图像中的每个字符绘制边界框-MATLAB [英] Draw bounding boxes for entire word instead of each character in an image - MATLAB

查看:94
本文介绍了为整个单词而不是图像中的每个字符绘制边界框-MATLAB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MATLAB代码,可以在每个字母周围绘制边框.

I have a MATLAB code that draws bounding boxes around each letter.

我想在每个单词而不是每个字符周围画这些框.

I would like to draw these boxes around each word, instead of each character.

我曾经想到过

  • 读取单词之间每个空格的大小,并以此为基础,将每个单词分开.
  • 将相邻的矩形分组为更大的矩形,这对我基本上会做同样的事情.

这将如何完成?

这是到目前为止的图像:

Here is the image so far:

http://imgur.com/iDF5VD4

到目前为止,这是我的代码:

Here is my code so far:

%CLEAR EVERYTHING
clear all;
close all;

%SET FOLDER AND FILE LOCATION
folder = 'H:\Miscellaneous\Work\Project';
baseFileName = 'lorem-ipsum.jpg';
fullFile = fullfile(folder, baseFileName);

%CONVERT TO GRAYSCALE
normal = imread(fullFile);
gray = rgb2gray(normal);

%CONVERT TO BINARY IMAGE
binary = im2bw(gray);

%INVERT IMAGE
binary = ~binary;

%FILL HOLES
ifill=imfill(binary,'holes');
figure,imshow(ifill)

%COUNT LETTER IN TEXT
[Ilabel num]=bwlabel(ifill);
disp(num)

%CALCULATE REGION PROPERTIES
Iprops=regionprops(Ilabel);

%SET BOX PROPERTIES INTO VARIABLE
Ibox=[Iprops.BoundingBox];

%RESHAPE 1-D ARRAY
Ibox=reshape(Ibox,[4 num]);

%DRAW BOUNDING BOXES FOR EACH LETTER
for cnt=1:num
    rectangle('position',Ibox(:,cnt),'edgecolor','r');
end
hold off

推荐答案

我认为您有个好主意.您可以将信箱分组为单词,然后计算每组的边界框.

I think you've got a good idea. You group letter boxes into words, then compute the bounding box of each group.

在您的特定示例中,您可以通过形态学闭合非常快地完成此操作.我在这里不解释如何计算单词间距,您只需要字符间距,它是所使用字体的参数.我将此参数称为Sp.在您的图像上,Sp大约为4像素.

In your particular example, you can do this very fast with morphological closing. I don't explain here how to compute the word spacing, you only need the character spacing which is a parameter of the used font. I call this parameter Sp. On your image, Sp seems to be approximately 4 pixels.

因此,首先获取您的二进制映像;请注意,在此处填充孔非常有趣.使用形态学封闭,您可以直接处理字母,而无需使用其边界框.

So, first take your binary image; note that filling the holes is very interesting here. With morphological closing, you can work directly on the letters, no need to work with their bounding boxes.

binclosed = imclosed(binary, strel('rectangle',[2 ceil(Sp/2)]));

在这里,我用一个高度为2的矩形关闭,以捕捉例如"i"的点.

Here I close with a rectangle of height 2 in order to, for example, catch the dots of 'i').

然后,您可以标记已连接的组件并像对待字符一样绘制它们的边界框.

Then you can label the connected components and draw their bounding boxes as you have done for characters.

[Ilabel,num] = bwlabel(binclosed);
Iprops = regionprops(Ilabel, 'BoundingBox');
Ibox = reshape([Iprops.BoundingBox],[4 num]);
for cnt=1:num
    rectangle('position',Ibox(:,cnt),'edgecolor','r');
end

这篇关于为整个单词而不是图像中的每个字符绘制边界框-MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆