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

8 次查看(过去 30 天)
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.

采纳的回答

Image Analyst
Image Analyst 2016-11-3
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 个评论
Image Analyst
Image Analyst 2017-3-15
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 2017-3-15
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?

请先登录,再进行评论。

更多回答(2 个)

KSSV
KSSV 2016-11-3
doc knnsearch

Walter Roberson
Walter Roberson 2017-3-15
编辑:Walter Roberson 2017-3-15
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!

Translated by