Image processing matlab中字符序列的提取
我想提取序列中的字符。例如,给定此图像: 以下是我编写的代码:Image processing matlab中字符序列的提取,image-processing,extraction,text-extraction,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)<=
[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
[L Ne]=bwlabel(二进制图像);
stats=regionprops(L,'BoundingBox');
cc=vertcat(stats(:).BoundingBox);
aa=cc(:,3);
bb=cc(:,4);
等等
图形
对于n=1:大小(统计,1)
如果(aa(n)/bb(n)>=0.2&&aa(n)/bb(n)regionprops
通过在列主顺序中查找blob进行操作。regionprops
不按行主顺序进行操作,这就是您要查找的。列主顺序源于MATLAB本身,因为按列主顺序操作是本机行为。此外,使用的逻辑>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
然后像以前一样,使用ind
变量并将其与for
循环一起使用。小注释。BinaryImage
应该反转,以便文本为白色,背景为黑色。您的原始图像已翻转。是的,我总是在任何处理之前反转二进制图像。这在您的co中并不明显这就是我留下那张小纸条的原因。谢谢。它起作用了。但是我不明白'ind=[1:2:numel(stats)2:2:numel(stats)];“…numel只需计算像素总数。它是如何将列主顺序更改为行主顺序的?请仔细阅读本文。如果您想要行主顺序,则必须按[1 3 5 2 4 6]
的顺序访问结构元素。该语句以这种方式生成索引。1:2:numel(stats)
创建一个向量,从1开始,最多创建跳过2个元素的元素。2:2:numel(统计)
是一样的,但我们从2开始。试着自己在MATLAB中查看向量,看看它是否有效。但是,让我更新帖子,以便您可以根据需要对其进行调整。这篇帖子目前假设有两行字符。当上行包含3个字母,下行包含3个字母时,效果很好数字。但我的某些图像的下行有4个数字。在这种情况下,它会先检测下行,然后检测上行。像和我的所有图像只有2行。此外,有时它会随机检测(不按顺序)。随机检测图像有一些方向。可能这会导致该问题。感谢您的努力!
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