2016-04-17 41 views
1

执行此代码时出现问题。我想从图像中提取文本,这是我的代码从自然图像中提取文字

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]); 

任何人都可以帮我一个错误???

+0

开始。为什么重塑使用,为什么这些维度? – nkjt

回答

2

您假设有总是找到92个边界框。你遇到了错误,因为这显然并非总是如此。而不是将第二个维度指定为reshape,you can pass an empty array,以便reshape将计算出适当的维度。

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

您的循环使得92相同的假设,所以你需要通过理解错误更改以及

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

非常感谢您使用这种方法从图像中进行文本检测,但效果不佳请帮助我提出任何好的方法,我在等待您的回复:) –

+0

@NomanMalik为什么不使用MATLAB的'ocr' ? – Suever

+0

因为我想用我的论文中的任何先进技术来帮助我。我在做MS。但是如果你建议ocr比这个更好,那么我会用ocr。 –