更新时间:2023-02-26 16:55:18
您可以使用2D卷积构建一个掩码,根据该值选择值掩码,然后将它们减少到唯一值:
You can build a mask with a 2D convolution, select the values according to that mask, and then reduce them to unique values:
% // Data:
A = [ 1 1 1 1 1 1
1 2 2 3 3 3
4 4 2 2 3 3
4 4 2 2 2 3
4 4 4 4 3 3
5 5 5 5 5 5 ];
value = 2;
adj = [0 1 0; 1 0 1; 0 1 0]; %// define adjacency. [1 1 1;1 0 1;1 1 1] to include diagonals
%// Let's go
mask = conv2(double(A==value), adj, 'same')>0; %// pixels adjacent to those equal to `value`
result = unique(A(mask));
在示例中,这会产生
result =
1
2
3
4
请注意,结果包括 2
,因为某些值 2
的像素相邻具有该值的像素。
Note that the result includes 2
because some pixels with value 2
have adjacent pixels with that value.