【发布时间】:2015-01-13 08:25:24
【问题描述】:
我有一张文档图片,可能是报纸或杂志。例如,扫描的报纸。我想删除所有/大部分文本并将图像保留在文档中。任何人都知道如何检测文档中的文本区域?下面是一个例子。提前致谢!
【问题讨论】:
标签: image-processing machine-learning computer-vision pattern-recognition
我有一张文档图片,可能是报纸或杂志。例如,扫描的报纸。我想删除所有/大部分文本并将图像保留在文档中。任何人都知道如何检测文档中的文本区域?下面是一个例子。提前致谢!
【问题讨论】:
标签: image-processing machine-learning computer-vision pattern-recognition
通常的对象识别模式在这里可以工作 - 阈值、检测区域、过滤区域,然后对剩余区域执行您需要的操作。
在这里设置阈值很容易。背景是纯白色(或可以过滤为纯白色),因此反转灰度图像中高于 0 的任何内容都是文本或图像。然后可以在这个阈值二值图像中检测区域。
为了过滤区域,我们只需要确定是什么使文本与图片不同。文本区域会变小,因为每个字母都是它自己的区域。相比之下,图片是大区域。使用适当的阈值按区域区域过滤将拉出所有图片并删除所有文本,假设所有图片都没有页面上任何地方的单个字母大小。如果它们是,则可以使用其他过滤标准(饱和度、色调变化,...)。
根据区域和饱和度标准过滤区域后,可以通过将原始图像中落在过滤区域边界框内的像素插入到新图像中来创建新图像。
MATLAB 实现:
%%%%%%%%%%%%
% Set these values depending on your input image
img = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/21044/6ce011abjw1elr8moiof7j20jg0w9jyt.jpg');
MinArea = 2000; % Minimum area to consider, in pixels
%%%%%%%%%
% End User inputs
gsImg = 255 - rgb2gray(img); % convert to grayscale (and invert 'cause that's how I think)
threshImg = gsImg > graythresh(gsImg)*max(gsImg(:)); % Threshold automatically
% Detect regions, using the saturation in place of 'intensity'
regs = regionprops(threshImg, 'BoundingBox', 'Area');
% Process regions to conform to area and saturation thresholds
regKeep = false(length(regs), 1);
for k = 1:length(regs)
regKeep(k) = (regs(k).Area > MinArea);
end
regs(~regKeep) = []; % Delete those regions that don't pass qualifications for image
% Make a new blank image to hold the passed regions
newImg = 255*ones(size(img), 'uint8');
for k = 1:length(regs)
boxHere = regs(k).BoundingBox; % Pull out bounding box for current region
boxHere([1 2]) = floor(boxHere([1 2])); % Round starting points down to next integer
boxHere([3 4]) = ceil(boxHere([3 4])); % Round ranges up to next integer
% Insert pixels within bounding box from original image into the new
% image
newImg(boxHere(2):(boxHere(2)+boxHere(4)), ...
boxHere(1):(boxHere(1)+boxHere(3)), :) = img(boxHere(2):(boxHere(2)+boxHere(4)), ...
boxHere(1):(boxHere(1)+boxHere(3)), :);
end
% Display
figure()
image(newImg);
正如您在下面链接的图片中看到的那样,它可以满足您的需求。除了图片和标头之外的所有内容都被删除。好消息是,如果您在远离首页的报纸上工作,这对于彩色和灰度图像来说效果很好。
结果:
【讨论】: