image-processing - 在序列matlab中提取字符

标签 image-processing extraction text-extraction

我想按顺序提取字符。例如,给定这张图片:

这是我写的代码:

[L Ne]=bwlabel(BinaryImage);
stats=regionprops(L,'BoundingBox');
cc=vertcat(stats(:).BoundingBox);
aa=cc(:,3);
bb=cc(:,4);
hold on
figure
for n=1:size(stats,1)
    if (aa(n)/bb(n) >= 0.2 && aa(n)/bb(n)<= 1.25)
        [r,c] = find(L==n);
        n1=BinaryImage(min(r):max(r),min(c):max(c));
        imshow(~n1);
        pause(0.5)
    end
    hold off
end

我应该为正确的顺序进行哪些更改?

最佳答案

regionprops通过在 中查找 blob 来操作专栏命令。 regionprops不按行优先顺序运行,这正是您要查找的。列优先顺序源自 MATLAB 本身,因为以列优先顺序操作是 native 行为。此外,您的逻辑使用 find / bwlabel也以列优先格式运行,因此在尝试以行优先格式显示字符时,您必须牢记这两件事。

因此,一个简单的方法是修改您的 for循环,这样您就可以按行而不是按列访问结构。对于您的示例图像,描述的字符顺序如下所示:

 1   3   5
 2   4   6

您需要按以下顺序访问该结构:[1 3 5 2 4 6] .因此,您将更改您的 for循环访问这个新数组,你可以像这样创建这个新数组:
ind = [1:2:numel(stats) 2:2:numel(stats)];

一旦你这样做了,只需修改你的 for循环访问 ind 中的值反而。为了使您的代码完全可重现,我将直接从 StackOverflow 和 读取您的图像。反转 图像作为文本是黑色的。文本需要为白色才能使 blob 分析成功:
%// Added
clear all; close all;
BinaryImage = ~im2bw(imread('http://s4.postimg.org/lmz6uukct/plate.jpg'));

[L Ne]=bwlabel(BinaryImage);
stats=regionprops(L,'BoundingBox');
cc=vertcat(stats(:).BoundingBox);
aa=cc(:,3);
bb=cc(:,4);
figure;

ind = [1:2:numel(stats) 2:2:numel(stats)]; %// Change
for n = ind %// Change
    if (aa(n)/bb(n) >= 0.2 && aa(n)/bb(n)<= 1.25)
        [r,c] = find(L==n);
        n1=BinaryImage(min(r):max(r),min(c):max(c));
        imshow(~n1);
        pause(0.5)
    end
end

警告

上面的代码假设只有两行字符。如果您有更多,那么很明显指定的索引将不起作用。

如果您希望它适用于多行,那么我要写的这个逻辑假设文本是水平的而不是一个角度。简而言之,您会一直循环直到用完结构,并且在循环开始时,您将搜索具有最小 (x,y) 的 blob。我们未处理的 blob 左上角的坐标。一旦你找到这个,你就搜索所有 y在此源的某个阈值内的坐标 y坐标,你会捕获这些位置的索引。你会重复这个直到你用完结构。

像这样的东西:
thresh = 5; %// Declare tolerance

cc=vertcat(stats(:).BoundingBox);
topleft = cc(:,1:2);

ind = []; %// Initialize list of indices
processed = false(numel(stats),1); %// Figure out those blobs that have been processed
while any(~processed) %// While there is at least one blob to look at...
    %// Determine the blob that has the smallest y/row coordinate that's  
    %// unprocessed
    cc_proc = topleft(~processed,:);
    ys = min(cc_proc(:,2));

    %// Find all blobs along the same row that are +/-thresh rows from
    %// the source row
    loc = find(abs(topleft(:,2)-ys) <= thresh & ~processed);

    %// Add to list and mark them off
    ind = [ind; loc];
    processed(loc) = true;
end

ind = ind.'; %// Ensure it's a row

然后你会使用 ind变量并将其与 for 一起使用就像以前一样循环。

关于image-processing - 在序列matlab中提取字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32208436/

相关文章:

html - 如何根据容器而不是屏幕尺寸提供图像尺寸

opencv - 最佳方法 : how to detect objects that have few features, 但成群结队

c# - 从 GOLD-Parser CGT-File 中提取关键词

python - PDFminer:提取带有字体信息的文本

java - 在 java 中获取 URL 参数并从该 URL 中提取特定文本

php - 如何从字符串中提取 PHP 中的标题标签?

OpenCV - 对齐图像堆栈 - 不同的相机

matlab - 如何对当前像素的 8 个相邻像素值求和。

python - 如何使用 lxml、XPath 和 Python 从网页中提取链接?

iphone - libarchive 解压到指定文件夹?