一个班轮解决方案
用途:
mat(imdilate(mat==2,strel('disk',2)))=2;
结果
mat =
1 1 2 2 2 1 1 1
1 2 2 2 2 2 1 1
2 2 2 2 2 2 2 3
2 2 2 2 2 2 2 2
1 2 2 2 2 2 2 3
1 1 2 2 2 2 1 1
一步一步的解释
针对此问题的解决方案是基于在区域dilation操作,其中所述基质是等于2 这可以如下:
%initializes the input matrix
mat = [1,1,1,1,1,1,1,1 ; 1,1,1,1,1,1,1,1; 1,1,2,2,2,1,1,3 ; 1,1,2,2,2,2,1,3; 1,1,1,1,2,1,1,3 ; 1,1,1,1,1,1,1,1];
%initilizes a mask which represents the reion which we want to exapand
roiMask = mat==2;
%perform expansion to this mask by imdilate function
dilatedRoi = imdilate(mat==2,strel('disk',2));
%assigns the new value into the original matrix
mat(dilatedRoi) = 2;
注意,膨胀运算的特征在于结构化元素对象,它基本上是一个定义执行扩展方式的二进制矩阵。 在我的例子中,我使用MATLAB的strel功能,生成以下内容:
strel('disk',2)
ans =
0 0 1 0 0
0 1 1 1 0
1 1 1 1 1
0 1 1 1 0
0 0 1 0 0
您可能需要改变strel为了完全控制所需的扩张行为。