boo*_*ans 5 matlab image-processing
我目前正在使用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
Run Code Online (Sandbox Code Playgroud)
这是一个很好的解决问题.这是一种你可以使用的方法,但我承认它绝不是完美的,也可能不那么健壮.希望它能带给你创意......
我所做的基本上是使用中值滤波器(就像你做的那样)过滤图像并使用删除小元素bwareaopen.然后我打电话regionprops来获得一堆属性,其中最重要的是area和eccentricity.这个想法是所有字母"a"应该有一个类似的偏心,因此一旦我们知道一个字母的怪癖,我们就可以找到其他大致相同的字母.你可以使用额外的属性使代码更加健壮,这些属性可以使字母从其他字母中脱颖而出; 也许是比例MajorAxisLength/MinorAxisLength.我会把那部分留给你:)
所以,挑在这种情况下,信的最简单的方法是选择面积最大,这是大的对象一个在图片的中心.一旦我们有了它的偏心率,我们就可以应用一些阈值并只选择那些使用regionprops具有相似偏心率的物体.中值滤波器和bwareaopen之前应用的调用在这里很重要,因为右边4个方框中的噪声类型会使事情变得复杂,如果它们没有被移除,因为一些随机点可能具有类似于我们亲爱的字母的偏心率"一个".
话虽如此,这是评论的代码.请注意,我改变了你的名字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
Run Code Online (Sandbox Code Playgroud)
这里大"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
Run Code Online (Sandbox Code Playgroud)
输出,质心为红点,边界框为黄色矩形:

那很有趣!希望我能以某种方式提供帮助.您可能需要调整其他字母的代码,或者如果图像中有其他圆形对象,但我想这是一个开始.
| 归档时间: |
|
| 查看次数: |
2334 次 |
| 最近记录: |