我有一张二进制图片。我在图像中有几个单像素。单个像素是白色(1),并且它们的所有邻域都是黑色(0)。例如,下面的图像显示单个像素(位于中心)和两个像素(位于左下角):
0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
1 1 0 0 0
如何在Matlab中删除具有形态学操作的单个像素?
答案 0 :(得分:1)
与previous question一样,您可以使用bwboundaries
:
如果P
是二进制图像,则为:
B = bwboundaries(P,8);
for k = 1:numel(B)
if size(B{k})<=2
P(B{k}(1,1),B{k}(1,2)) = 0;
end
end
因此,上面的示例P
变为:
P =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 0 0 0
答案 1 :(得分:1)
我使用带有conv2
的2D卷积:
M = [0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
1 1 0 0 0]
C = [0 1 0
1 1 1
0 1 0]; % The matrice that is going to check if a `1` is alone or not.
%if you also want to consider the neibhbors on the diagonal choose:
%C = ones(3);
R = M.*conv2(M,C,'same')>1 %Check for the neighbors.
<强> RESULT 强>
R =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 0 0 0