【问题标题】:How to detect text region from a document image?如何从文档图像中检测文本区域?
【发布时间】:2015-01-13 08:25:24
【问题描述】:

我有一张文档图片,可能是报纸或杂志。例如,扫描的报纸。我想删除所有/大部分文本并将图像保留在文档中。任何人都知道如何检测文档中的文本区域?下面是一个例子。提前致谢!

示例图片:https://www.mathworks.com/matlabcentral/answers/uploaded_files/21044/6ce011abjw1elr8moiof7j20jg0w9jyt.jpg

【问题讨论】:

    标签: image-processing machine-learning computer-vision pattern-recognition


    【解决方案1】:

    通常的对象识别模式在这里可以工作 - 阈值、检测区域、过滤区域,然后对剩余区域执行您需要的操作。

    在这里设置阈值很容易。背景是纯白色(或可以过滤为纯白色),因此反转灰度图像中高于 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);
    

    正如您在下面链接的图片中看到的那样,它可以满足您的需求。除了图片和标头之外的所有内容都被删除。好消息是,如果您在远离首页的报纸上工作,这对于彩色和灰度图像来说效果很好。

    结果:

    http://imgur.com/vEmpavY,dd172fr#1

    【讨论】:

    • 感谢您的回答!我
    • 感谢您的回答!这是相当令人印象深刻的!!!!正如你所说,它适用于纯白色背景。如果背景和文字不是黑白的怎么办?例如本例(rmeassets.com/images/rmemedia/1-2_Newspaper-Ads-color_1.jpg),背景颜色为绿色,文本为白色。您的信息和代码将不胜感激。我碰巧有一个案例你的代码失败了。我可以给你发电子邮件,你看看吗?由于图片受版权保护,所以我没有在这里发布。我可以知道你的电子邮件吗?非常感谢! :)
    • 无论如何要处理其他背景颜色?谢谢!
    • 我的代码适用于第二张图片,但会删除第一张图片中的部分图片。可能需要更多的形态过滤才能使其工作。但基本上将移动到上面的新图像中的区域创建无文本图像可以首先作为子图像进行探索,看看它们内部是否有任何文本。在正确的方向设置阈值并用背景替换“文本”像素会在将文本的子图像放入空白图像之前消除它。
    • 你能提供这个过程的示例代码吗?非常感谢!真的很感激!! [我的邮箱:leefiong.lee#gmail.com]
    猜你喜欢
    • 2014-12-23
    • 2019-10-06
    • 2015-10-01
    • 1970-01-01
    • 2014-08-14
    • 2015-05-21
    • 2018-07-13
    • 1970-01-01
    • 2018-07-22
    相关资源
    最近更新 更多