【发布时间】:2017-01-19 19:20:19
【问题描述】:
代码:
img = imread ('G:\Stuff\RP\Database\0001_4.jpg');
%imshow(img);
bin_img = imcomplement(im2bw(img, 0.8)); %Binarizing
%figure;
%imshow(bin_img);
bin_img = bwareaopen(bin_img, 50); %for removing dots and commas
%%%%%%%% Line Segmentation %%%%%%%%
dbw_img = imdilate(bin_img, strel('line', 100, 0));%Dilating
[L, N]=bwlabel(dbw_img); %finding connected components
bbox = regionprops(L, 'BoundingBox');
lineSlopeMatrix=[N 0];
for i=1:N %must run for all the lines in an image
bBox=bbox(i).BoundingBox;
x=bBox(1)+0.5;
y=bBox(2)+0.5;
w=bBox(3);
h=bBox(4);
linePatch=bin_img(y:y+h,:); %Extracting line
figure,imshow(linePatch) % Prints lines
words_img = imdilate(linePatch, strel('line', 40, 0));%Dilating
[R, C]=bwlabel(words_img); %finding connected components i.e. Words
bounding = regionprops(R, 'BoundingBox');
for j=1:C %nmust run for the words in a line
bdBox=bounding(j).BoundingBox;
xAxis=bdBox(1)+0.5;
yAxis=bdBox(2)+0.5;
width=bdBox(3);
height=bdBox(4);
% [row col]=size(linePatch)
% yAxis,yAxis+height,xAxis,xAxis+width
Patch=linePatch(yAxis:yAxis+height,xAxis:xAxis+width);
%Extracting Patch of Words
figure,imshow(Patch) %Prints words
Patch=[];
end
linePatch=[];
end
问题:我首先从输入图像中分割出一行,然后从该行中提取单词。我的算法将单词从第一行正确分割出来(来自输入图像,附在这篇文章中),然后它还会从第二行分割第一个单词,而不是给我以下错误:
??? Index exceeds matrix dimensions.
Error in ==> test_words_lines at 33
Patch=linePatch(yAxis:yAxis+height,xAxis:xAxis+width);
我很容易理解错误,检查了矩阵的尺寸,它们看起来很好,或者我在那里找不到问题..
请看附上的图片: Segmentation of Words from Line 1// Segmentation of Words from Line 2
请告诉我此错误的正确原因并提出修复建议。 谢谢:)
【问题讨论】:
-
尝试使用调试器并在
Patch=linePatch(yAxis:yAxis+height,xAxis:xAxis+width)行停止。检查xAxis, yAxis, etc.的值。它们是否大于您的矩阵大小linePatch? -
height,width,yAxis和linePatch的大小? -
@bushmills 我添加了条件
i==2 && j==2的断点,发现xAxis的值是280,yAxis104,width是45,height是5.. 而linePatch是 108*2465。现在,yAxis+height=104+5=109 大于 no of rows,108.. 但这没有意义..size(linePatch)=108*2465不是表示 108 行和 2465 列吗?和xAxis和yAxis值,表示像素数?在此先感谢:) -
@bushmills 大小为
linePatch= 108 * 2465,height=5 ,width=45,yAxis= 104
标签: matlab image-processing matrix text-segmentation