I have an array of linear indices and for each linear index, I want to find the linear indices of the neighboring pixels in a radius of let's say 5-pixels
. I found the following code which does the job for a 8-connected neighborhood
. But, how to implement it to find the linear indices of 120 neighbors
for a 5-pixel
neighborhood.
%# 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);