如何使用Matlab提取图像中的文本区域?

时间:2013-03-29 08:32:41

标签: matlab image-processing edge-detection

我正在编写一个matlab程序,用于检测自然场景图像中的文本。我正在接受this video lecture and paper的帮助。我理解了前几步并进行了边缘检测(我在下面添加图像)。现在我想从最终图像中提取(分离出)文本。我该怎么做?

我的代码:

i = imread('f:\new.jpg');
i1 = rgb2gray(i);
imshow(i1);

i2 = edge(i1,'canny',0.3);
imshow(i2);

se = strel('square',2);
i3 = imdilate(i2,se);
imshow(i3);

i4 = imfill(i3,'holes');
imshow(i4);

[Ilabel num] = bwlabel(i4);
disp(num);
Iprops = regionprops(Ilabel);
Ibox = [Iprops.BoundingBox];
Ibox = reshape(Ibox,[4 92]);
imshow(i);

hold on;
for cnt = 1:92
    rectangle('position',Ibox(:,cnt),'edgecolor','r');
end

最终图片: enter image description here

1 个答案:

答案 0 :(得分:1)

裁剪出部分图像是从矩阵中获取子矩阵,知道它的边界索引。例如,如果子矩阵从y1行延伸到y2行,从x1列延伸到x2列(包括所有边界),您将获得子矩阵:

submat = mainmat(y1:y2,x1:x2);

在提取子矩阵之前,你必须得到每个字母的边界坐标。如果您有一个字母的yx坐标;无论是图像区域的所有坐标,还是区域边缘的坐标,都可以得到边界坐标:

% x-coordinates of the region are in the vector x
x1 = min( x(:) );
x2 = max( x(:) );
% y-coordinates of the region are in the vector y
y1 = min( y(:) );
y2 = max( y(:) );

对所有字母执行上述步骤后,您可以逐个分隔字母,但如果您想要分隔连接的字母区域,则必须检查字母边界矩形的重叠。