假设我们要使用Matlab中的形态学扩张算子来放大二进制图像中的黑色区域。所需的输出必须如下所示,但是给定的代码会生成不同的图像!
bin = ones(10,10, 'uint8');
bin(3:8, 3:8) = 0;
bin([4 7], [4 7]) = 1;
nhood = [1 0 1;
0 0 0;
1 0 1];
dil = imdilate(bin, strel(nhood))
figure;
subplot(1,2,1)
imshow(255*bin, 'InitialMagnification', 'fit')
subplot(1,2,2)
imshow(255*dil, 'InitialMagnification', 'fit')
答案 0 :(得分:2)
在这种情况下,您的结构元素是倒置的,即[255,0,255; 0,0,0;当您将黑色区域设为前景时,将使用255、0、255]。
要获得视频中显示的结果,您将必须使用[0,1,0; 1,1,1; 0,1,0]作为结构元素。
注意:通常,在形态学操作中,您将白色区域作为前景,并使用结构元素修改前景。但是在这个video中,他使用黑色区域作为前景
bin = ones(10,10, 'uint8');
bin(3:8, 3:8) = 0;
bin([4 7], [4 7]) = 1;
nhood = [0 1 0;
1 1 1;
0 1 0];
erode = imerode(bin, strel(nhood));
dilate = imdilate(erode, strel(nhood));
figure;
subplot(2,2,1)
imshow(255*bin, 'InitialMagnification', 'fit')
subplot(2,2,2)
imshow(255*erode, 'InitialMagnification', 'fit')
title('after erosion')
subplot(2,2,3)
imshow(255*dilate, 'InitialMagnification', 'fit')
title('after dilation')