2016-04-17 50 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。 –