使用形态学运算在Matlab中进行数字识别

时间:2015-03-26 12:57:24

标签: matlab

我有一个png格式的数字'6'的图像,我想用形态学操作确定茎相对于blob的位置。我使用下面的代码检测到了6的blob。现在,我不知道如何检测数字'6'的词干。我尝试使用霍夫变换和边缘检测算法,但它没有帮助。

以下是我检测blob的代码:

img=imread('six.png');    
img=rgb2gray(img);    
figure,imshow(img);   
i1=im2bw(img);    
st=strel('square',20);    
imdilate(i1,st);    
figure,imshow(i1);    
i2=imfill(i1,'holes');    
figure,imshow(i2);    
i1=imsubtract(i2,i1);    
B = bwboundaries(i1);    
figure,imshow(i1)    
i2=i2-i1;    
figure,imshow(i2);    
text(10,10,strcat('\color{green}Objects Found:',num2str(length(B))))    
hold on    

for k = 1:length(B)    
    boundary = B{k};    
    plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 0.2)

end

if eq(num2str(length(B)),'1')        
    h=msgbox('the number is 6');    
else    
    h=msgbox('unknown number');
end

这是原始的六张图片和我当前的输出

Input six

Output six

1 个答案:

答案 0 :(得分:1)

如果您想坚持形态学操作,您可以简单地找到最接近您已检测到的孔的像素并将其移除。

我从你做的相同的形态学操作开始,并添加额外的步骤去除检测到的孔的距离阈值内的像素。

img=imread('six.png');    
img=im2bw(img);    
figure,imshow(img);   

filled_img=imfill(img,'holes');    
figure; imshow(filled_img);

filled_boundary= bwmorph(filled_img,'remove');
figure
imshow(filled_boundary)

hole = ~img & filled_img;
figure; imshow(hole);

hole_boundary = bwmorph(hole, 'remove');
figure; imshow(hole_boundary);

%Remove points on the boundary that are close to the hole
[hole_x, hole_y] = find(hole_boundary);
[fill_x, fill_y] = find(filled_boundary);
D = pdist2([hole_x, hole_y], [fill_x, fill_y]);
[distance, ~] = min(D, [], 1);

distance_threshold = 10;
top_edges = filled_boundary;
top_edges(fill_x(distance<distance_threshold), fill_y(distance<distance_threshold)) = 0;
figure; imshow(top_edges);

这是我的输出图像的样子

Top of six