Matlab:扩张运算符没有 return 所需的输出

Matlab: Dilation operator does not return the desired output

假设我们想在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')

结构元素和原图如下:

在这种情况下,您的结构元素是倒置的,即 [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')