从自然图像中提取文本

时间:2016-04-17 08:23:39

标签: matlab matlab-figure matlab-guide

执行此代码时遇到问题。我想从图像中提取文本,这是我的代码

i = imread('handicapped.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

我在第19行中有错误

Error using reshape
To RESHAPE the number of elements must not change.

Error in test11 (line 19)
Ibox = reshape(Ibox,[4 92]);
谁能帮助我?

1 个答案:

答案 0 :(得分:2)

您假设已找到始终 92个边界框。你收到一个错误,因为这显然并非如此。而不是将第二个维度指定为reshapeyou can pass an empty array,以便reshape找出合适的维度。

%// 4 Rows with numel(Ibox)/4 columns
Ibox = reshape(Ibox, 4, []);

你的for循环与92的假设相同,所以你也需要改变它

for cnt = 1:size(Ibox, 2)
    rectangle('position',Ibox(:,cnt),'edgecolor','r');
end