matlab - Voxel neighborhood indexing - Detecting "out of bounds" in 26 neighbor access with linear indexing -
Well, I do not know how to describe my problem with the title, I hope I have one right . / P>
I have a matrix ( M
in the example below) which is a 3D image, in this case, by 11x11x11 voxels (I have made it Logical just for ease, and the size is just an example too.)
In my code, I need to reach 26 voxels of some voxels, and for that I use some fancy linear sequencing :
The problem is that if the "range" of dot
m
is in, then some values are exited He is tried, and the error will occur.
To solve this problem, one good way would be to make it around M
to form it +2 size in each dimension, and with zero, but I really want to avoid changing M
, because my code is very complex, for example.
I can not do any double search of this ng, I got some stuck here. Any suggestions
Edit :. @Dan works to answer, however I would like to see if there is a possible solution using this linear sequencing method
% Example Data M = Round (Randon ( 11,11,11)) ~ = 0; A good way to store 26 neighboring indices for the best handing S = size (M); N = length (s); [C1 {1: n}] = ndgrid (1: 3); C2 (1: n) = {2}; Neighborhood 26 = Sub-Ind (S, C1 {}} - Sub-Ind (S, C2 {:}); Point = [5 1 6]; This work will work until the point is in the range (in this example) neighbors = M (all 2 ind (s, point (1), point (2), point (3)) + neighborhood 26)
Is this linear indexing material necessary? Because it is very easy to control boundary conditions because you use the subscript indexing and minutes
and max
as follows:
p = [5, 1, 6]; Minimum (P (2) + 1 (maximum (1, p (1) -1), end (), max (P) (1) + 1, end), max (1, p (2) -1) (1, P (3) -1): Minimum (P (3) + 1, end)) Get rid of% point it yourself (i.e. center) Neighbors = neighborhood ([1: 13, 15: end])
In this way you can easily simplify if you want a broad neighborhood:
p = [5, 1, 6]; N = 2; Max (1, P (2) -N): Minimum (P (2) + N, End), Max (1, P (3) -N)): Minimum (P (3) + N, End) % // Get rid of it yourself (center means) middle = CIL (number (neigbourhood) / 2); Neighbors = neighborhood ([1: middle -2, mid + 1: end])
Or if you prefer to keep the size of the cube, then it may be:
neighbors = neighborhood; Neighbors (Middle) = Nain;
If you want to use them in your code multiple times, then it is best to redo as an M-file function, which only gives index:
function ind = matching nbr (m, p, n) m = zero (size (m)); Minimum (P (2) + N, End), Maximum (1, P (1) -N): Minimum (P (1) + N, End), Max (1, P (2) (1, p (3) -n): minimum (p (3) + n, end)) = 1; M (P (1), P (2), P (3)) = 0; Ind = Search (M); End
Comments
Post a Comment