0

我有一个线性索引数组,对于每个线性索引,我想找到半径为 的相邻像素的线性索引5-pixels。我发现以下代码可以为8-connected neighborhood. 但是,如何实现它来找到邻域的线性120 neighbors索引5-pixel

%# target_array: array where pixels are marked
%# idx: linear index of a marked pixel
[M,N] = size(target_array)

neighbor_offsets=[-M-1 -M -M+1 1 M+1 M M-1 -1];

neighbors = bsxfun(@plus, idx, neighbor_offsets);
4

2 回答 2

1

您提到的代码可以找到像素周围的线性索引,只要该像素不太靠近目标数组的边界。

在您的情况下,我建议您单独遍历像素和find邻域:

[M,N] = size(target_array);

SE = strel('disk',5,inf);

%# the linear indices are stored in idxList
nIndices = length(idxList);

neighbors = cell(nIndices);

for ii = 1:nIndices
    idx = idxList(ii);
    bw = false(M,N);
    bw(idx) = true;
    bw = imdilate(bw,SE);

    %# mask the center
    bw(idx) = false;

    neighbors{ii} = find(bw);
end

如果你知道没有一个邻域重叠或接触,你可以简化上面的:

bw = false(M,N);
bw(idxList = true;
bw = imdilate(bw,SE);
bw(idxList) = false;
cc = bwconncomp(bw);
neighbors = cc.PixelIdxList;
于 2013-04-29T11:40:42.280 回答
0

你可以用meshgridsub2ind

假设一个目标数组称为T和索引点(mn),其中[m, n] = ind2sub(size(T), ind);

[X, Y] = meshgrid(m-5:m+5, n-5:n+5);
I = sub2ind(size(T), X(:), Y(:));

但是如果你担心边缘(你应该是),那么使用 min 和 max 如下:

[M, N] = size(T);
[X, Y] = meshgrid(max(1,m-5):min(M,m+5), max(1,n-5):min(N,n+5));
I = sub2ind(size(T), X(:), Y(:));

另请注意,这将包括中心点,但这很容易通过使用找到它的线性索引来删除sub2ind(size(T), m, n);

于 2013-04-29T11:42:54.820 回答