How can I find 26 neighbors of point with coordinates of (i,j,k)?

8 views (last 30 days)
I have a stack of images in a 3D matrix.
How can I find 26 neighbors of a specific voxel with coordinates of (i,j,k) in Matlab?
Your help is appreciated.

Accepted Answer

Image Analyst
Image Analyst on 3 Nov 2016
To get a 27 element cube around the voxel, do this:
neighbors = matrix3d(i-1:i+1, j-1:j+1, k-1:k+1);
Then turn it into a column vector like this:
neighbors = neighbors(:);
Then delete the i,j,k element to get the final 26 neighbors, by deleting the middle element of the column vector:
neighbors[14] = []; % Now we're done! We have a list of 26 voxel values.
  7 Comments
Image Analyst
Image Analyst on 15 Mar 2017
What does "identify" mean to you? You have the indexes of a voxel and its neighbors, so what else do you need to "identify" them?
Sandhiya Prakash
Sandhiya Prakash on 15 Mar 2017
Yes sir.I am having indexes of voxel and its neighbours,so totally i will have 27 scalar values.Now leaving the actual voxel consider only the neighbours(26) and identify its neighbouring voxel values.How to perform this?

Sign in to comment.

More Answers (2)

KSSV
KSSV on 3 Nov 2016
doc knnsearch

Walter Roberson
Walter Roberson on 15 Mar 2017
Edited: Walter Roberson on 15 Mar 2017
function [idx3d, valid, linidx] = neighbour26(i, j, k, Your3DMatrix)
%returns the indices of the 26 3-D neighbors of location i, j, k within Your3DMatrix
%In the first output, indices are returned as a 26 x 3 matrix of row, column, page triples.
%Since on the border areas neighbours might be outside of the matrix, the
%second output is a vector indicating which of the rows are valid.
%The third output is the linear indices -- useful for extracting content
[rows, cols, pages] = size(Your3DMatrix);
[R, C, P] = ndgrid(R-1:R+1, C-1:C+1, P-1:P+1);
idx3d = [R(:), C(:), P(:)];
idx3d(14,:) = []; %remove center
valid = idx(:,1) >= 1 & idx(:,2) >= 1 & idx(:,3) >= 1 & idx(:,1) <= rows & idx(:,2) <= cols & idx(:,3) <= pages;
linidx = nan(size(valid));
linidx(valid) = sub2ind( [rows, cols, pages], idx3(valid,1), idx3d(valid,2), idx3d(valid,3) );

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!