我目前正在使用Matlab学习图像处理。我要做的是在下图中找到所有字母 a 并删除所有其余字母。
首先,我将图像转换为二进制图像。然后我尝试使用中值滤波器去除噪声。在某些边界中存在一些间隙,我通过打开图像来填充。 但后来我陷入困境,我在互联网上找到了一些东西(我并不完全理解),我可以从中选择所有a的间隙。
接下来我该怎么做?
我的代码如下:
text = imread('http://i.stack.imgur.com/N4nCm.png');
text = mat2gray(text);
% find threshold and chance to binary image
border = graythresh(text);
textbw = im2bw(text, border);
imshow(textbw);
% remove noise with median filter
textfilt = medfilt2(textbw);
% remove small holes in the border of the a
se = strel('disk', 4);
texter = imopen(textfilt, se);
% find holes
s = regionprops(texter, 'BoundingBox');
bb = round(reshape([s.BoundingBox], 4, []).');
% show original image with holes
imshow(textbw);
for idx = 1 : numel(s)
rectangle('Position', bb(idx,:), 'edgecolor', 'red');
end
答案 0 :(得分:9)
这是一个很好的解决问题。这是一种你可以使用的方法,但我承认它绝不是完美的,也可能不那么健壮。希望它会给你一些想法......
我所做的基本上是用中值滤镜过滤图像(就像你做的那样)并使用bwareaopen
删除小元素。然后我调用regionprops
来获取一堆属性,其中最重要的是area
和eccentricity
。这个想法是所有字母“a”应该有一个类似的偏心,因此一旦我们知道一个字母的怪癖,我们就可以找到其他大致相同的字母。你可以使用额外的属性使代码更加健壮,这些属性可以使字母从其他字母中脱颖而出;也许比例为MajorAxisLength/MinorAxisLength
。我会把那部分留给你:)
因此,在这种情况下选择字母的最简单方法是选择面积最大的对象,即图像中心的大 a 。一旦我们有了它的偏心率,我们就可以应用一些阈值并仅选择那些使用regionprops
找到的具有类似偏心率的物体。之前应用的中值滤波器和bwareaopen
调用在这里很重要,因为右边4个方框中的噪声类型会因为没有被移除而使事情变得复杂,因为一些随机点可能会出现偏心现象类似于我们亲爱的字母“a”。
话虽如此,这是评论的代码。请注意,我将text
变量的名称更改为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
输出,质心为红点,边界框为黄色矩形:
这很有趣!希望我能以某种方式提供帮助。您可能需要调整其他字母的代码,或者如果图像中有其他圆形对象,但我想这是一个开始。