Voxel Index Index - Out of bounds detection in 26 neighboring linear indexed accesses

Well, I don’t know how to describe my problem with the name, I hope that the one I have is correct.

I have a matrix ( Min the example below), which is a three-dimensional image composed in this case by 11x11x11 voxels (I made this logical only for convenience, and the size is also an example).

In my code, I need to contact 26 neighbors of some voxels, and for this I use some linear indexing fantasies found at: http://www.mathworks.com/matlabcentral/answers/86900-how-to-find-all-neighbours -of-an-element-in-n-dimensional-matrix

The problem is that if it pointis in the "boundary" value M, some of the boundary values ​​are accessed, and this will lead to an error.

To solve this problem, a good approach would be to create a border around M, making it +2 in each dimension and filling it with zeros, however I really would like to avoid the changeM , since my code is rather complicated than the one in the example.

I can't find a way to do this, I'm a little stuck here. Any suggestion?

EDIT: @ The answer works, however I would like to see if there is a possible solution using this linear indexing method.

% Example data
M=round(randn(11,11,11))~=0;

% Fancy way of storing 26 neigh indices for good accesing 
s=size(M);
N=length(s);
[c1{1:N}]=ndgrid(1:3);
c2(1:N)={2};
neigh26=sub2ind(s,c1{:}) - sub2ind(s,c2{:});

point=[5 1 6];

% This will work unless the point is in the boundary (like in this example)
neighbours=M(sub2ind(s,point(1),point(2),point(3))+neigh26) 
+4
2

? , min max :

p = [5, 1, 6];

neighbourhood = M(max(1,p(1)-1)):min(p(1)+1,end),
                  max(1,p(2)-1)):min(p(2)+1,end),
                  max(1,p(3)-1)):min(p(3)+1,end))

%// Get rid of the point it self (i.e. the center)
neighbours = neighbourhood([1:13, 15:end])

, , :

p = [5, 1, 6];
n = 2;
neighbourhood = M(max(1,p(1)-n)):min(p(1)+n,end),
                  max(1,p(2)-n)):min(p(2)+n,end),
                  max(1,p(3)-n)):min(p(3)+n,end))

%// Get rid of the point it self (i.e. the center)
mid = ceil(numel(neigbourhood)/2);
neighbours = neighbourhood([1:mid-2, mid+1:end])

, :

neighbours = neighbourhood;
neighbours(mid) = NaN;

, m , :

function ind = getNeighbours(M,p,n)
    M = zeros(size(M));
    M(max(1,p(1)-n)):min(p(1)+n,end), max(1,p(2)-n)):min(p(2)+n,end), max(1,p(3)-n)):min(p(3)+n,end)) = 1;
    M(p(1), p(2), p(3)) = 0;
    ind = find(M);
end
+3

: left-right, up-down, one more on each sides of the third dimension NaNs. 3x3x3, NaNs , , .

%// Initializations
sz_ext = size(M)+2; %// Get size of padded/extended input 3D array
M_ext = NaN(sz_ext); %// Initialize extended array
M_ext(2:end-1,2:end-1,2:end-1) = M; %// Insert values from M into it

%// Important stuff here : Calculate linear offset indices within one 3D slice
%// then for neighboring 3D slices too
offset2D = bsxfun(@plus,[-1:1]',[-1:1]*sz_ext(1)); %//'
offset3D = bsxfun(@plus,offset2D,permute([-1:1]*sz_ext(1)*sz_ext(2),[1 3 2]));

%// Get linear indices for all points
points_linear_idx = sub2ind(size(M_ext),point(:,1)+1,point(:,2)+1,point(:,3)+1);
%// Linear indices for all neighboring elements for all points; index into M_ext
neigh26 = M_ext(bsxfun(@plus,offset3D,permute(points_linear_idx,[4 3 2 1])))

: , 4th 27 ( ) 3x3x3. , neigh26 3x3x3xN, N - .

: M Point -

M=rand(11,11,11);
point = [
    1 1 4;
    1 7 1]

- -

neigh26(:,:,1,1) =
       NaN       NaN       NaN
       NaN    0.5859    0.4917
       NaN    0.6733    0.6688
neigh26(:,:,2,1) =
       NaN       NaN       NaN
       NaN    0.0663    0.5544
       NaN    0.3440    0.3664
neigh26(:,:,3,1) =
       NaN       NaN       NaN
       NaN    0.3555    0.1257
       NaN    0.4424    0.9577
neigh26(:,:,1,2) =
   NaN   NaN   NaN
   NaN   NaN   NaN
   NaN   NaN   NaN
neigh26(:,:,2,2) =
       NaN       NaN       NaN
    0.7708    0.3712    0.2866
    0.7088    0.3743    0.2326
neigh26(:,:,3,2) =
       NaN       NaN       NaN
    0.4938    0.5051    0.9416
    0.1966    0.0213    0.8036
+1

All Articles