【发布时间】:2014-12-21 10:46:30
【问题描述】:
我有这张图片(附上),我想分割数量列和价格列,但我不知道怎么做?!以及使用什么功能来帮助我做到这一点?!
任何帮助将不胜感激。谢谢
【问题讨论】:
-
您真的要创建两个新图像还是要从图像中提取数据?
标签: matlab image-processing computer-vision image-segmentation
我有这张图片(附上),我想分割数量列和价格列,但我不知道怎么做?!以及使用什么功能来帮助我做到这一点?!
任何帮助将不胜感激。谢谢
【问题讨论】:
标签: matlab image-processing computer-vision image-segmentation
利用输入图像的规则结构,您可以执行以下操作:
img = imread('http://i.stack.imgur.com/SuKT2.jpg'); %//read image
bw = sum(img,3) < 10; %//convert to binary mask
获取沿行的像素总和显示文本行(正和由空格分隔)
lines = bwlabel( sum( bw,2) > 1 ); %// label each line
lbw = bsxfun( @times, single(bw), lines ); %// label each line in the mask
现在我们可以忽略前 8 行(页眉)和最后两行页脚
sbw = lbw > 8 & lbw < max(lines)- 2; %// select only the relevant lines
我们可以将文本分成几列,假设至少有 15 像素的足够大的间隙
col = bwlabel( imfilter( single(sum(sbw,1) < 1), ones(1,15)/15, 'symmetric', 'same' ) < .9 );
计算两列选中行的边界框
st = regionprops( bsxfun(@times, sbw, col ), 'BoundingBox' );
可视化生成的边界框
figure;imshow( img, 'border','tight' );hold on;
rectangle('Position', st(1).BoundingBox, 'EdgeColor','r','LineWidth',2);
rectangle('Position', st(2).BoundingBox, 'EdgeColor','r','LineWidth',2);
结果
给定边界框,可以直接裁剪
col1 = imcrop( img, st(1).BoundingBox ); %// crop the description of the products
col2 = imcrop( img, st(2).BoundingBox ); %// crop the prices
【讨论】:
bw 和lines,然后检查bsxfun 相乘的结果——这个操作基本上给每个文本行一个不同的标签。 (2)我随意选择了阈值10,还不如4一样好。
s what i did ( see the whole code in debug mode ) but icant 把它理解为一个逻辑。如果您向我解释一下,我将不胜感激
lines 是一维(非 2D)向量,用于计算每个图像行的“文本”像素数。文本行之间的图像行的“文本”像素为零,因此它们的lines 值为零。为了标记对应于同一文本行的所有图像行,我使用了bwlabel 命令。您可以figure;plot(sum(bw,2)>1);hold on;plot(lines); 查看每个图像行如何与不同的文本行相关联。为了将此一维表示转换为所有文本像素的标签,我使用bsxfun 将它与掩码bw 的每一列相乘,并得到一个掩码,其中每个文本行都有不同的标签。