这是一个很好的解决问题.这是一种你可以使用的方法,但我承认它绝不是完美的,也可能不那么健壮.希望它能带给你创意……
我所做的基本上是用中值滤波器过滤图像(就像你做的那样)并使用bwareaopen删除小元素.然后我打电话给regionprops来获得一堆属性,其中最重要的是区域和偏心.这个想法是所有字母“a”应该有一个类似的偏心,因此一旦我们知道一个字母的怪癖,我们就可以找到其他大致相同的字母.你可以使用额外的属性使代码更加健壮,这些属性可以使字母从其他字母中脱颖而出;也许比例为MajorAxisLength / MinorAxisLength.我会把那部分留给你:)
因此,在这种情况下选择字母的最简单方法是选择面积最大的对象,即图像中心的大对象.一旦我们有了它的偏心率,我们可以应用一些阈值并仅选择那些使用具有类似偏心率的regionprops找到的对象.之前应用的中值滤波器和对bwareaopen的调用在这里很重要,因为右边4个方框中的噪声可能会使事情变得复杂,如果它们没有被删除,因为一些随机点可能有一个类似于我们亲爱的怪癖字母“a”.
话虽如此,这是评论的代码.请注意,我将文本变量的名称更改为textIm,因为text是Matlab函数.
clc
clear
close all
textIm = imread('http://i.stack.imgur.com/N4nCm.png');
%// find threshold and change to binary image
border = graythresh(textIm);
%// =========== NEW \ ===========
%// NOTICE the use of ~im2bw(...)
textbw = ~im2bw(textIm, border);
%// remove noise with median filter
%// =========== NEW \ ===========
textfilt = medfilt2(textbw,[7 7]);
textfilt = bwareaopen(textfilt,8);
%// =========== NEW \ ===========
%// Use an absurdely large line structuring element oriented at 25 degrees
%// to make the a's stand out
se = strel('line', 20 ,25);
textfilt = imclose(textfilt, se);
%// Get a couple properties. Note the "Eccentricity"
S = regionprops(textfilt, 'Area','Eccentricity','Centroid','BoundingBox');
All_areas = vertcat(S.Area);
%// Find the largest element (i.e. the big a). We will use it to get its
%// eccentricity and fetch other a's.
[MaxArea, MaxAreaIdx] = (max(All_areas(:)));
%// Get eccentricity of largest letter.
RefEcc = S(MaxAreaIdx).Eccentricity
这里大“a”的偏心率是0.6654.偏心率为0表示圆,偏心率为1表示直线.
%// Just concatenate everything. Easier to work with.
All_Ecc = vertcat(S.Eccentricity);
All_Centroids = vertcat(S.Centroid);
All_BB = vertcat(S.BoundingBox)
%// Find elements that have the approximate eccentricity of the large a
%// found earlier. You can be more/less stringent and add more conditions here.
PotA = find(All_Ecc > RefEcc*.8 & All_Ecc < RefEcc*1.2)
%// Display output with centroids and bounding boxes.
imshow(textIm)
hold on
scatter(All_Centroids(PotA,1),All_Centroids(PotA,2),60,'r','filled');
for k = 1:numel(PotA)
rectangle('Position',All_BB(PotA(k),:),'EdgeColor','y','LineWidth',2)
end
输出,质心为红点,边界框为黄色矩形:
那很有趣!希望我能以某种方式提供帮助.您可能需要调整其他字母的代码,或者如果图像中有其他圆形对象,但我想这是一个开始.