I have an image (200x200) and want to find the neighborhood locations in a specific point with a predefined radius. For example, with the radius of 5, I have 25 points around a point. Does MATLAB can do it? The problem is about the edge of image which it does not always 25 points and the program should just find the points that are within that radius. These points can be varied from 1 (corner) to 25 (center of image)
Following the discussion in the comments, I am adding another solution. For a given point, we compute the neighboring points within a specified number of steps (radius if you will). This is shown for both the 2D and the 3D case.
siz = [10 15]; %# matrix size
p = [5 10]; %# 2D point location
%# neighboring points
k = 2; %# radius size
[sx,sy] = ndgrid(-k:k,-k:k); %# steps to get to neighbors
xy = bsxfun(@plus, p, [sx(:) sy(:)]); %# add shift
xy = bsxfun(@min, max(xy,1), siz); %# clamp coordinates within range
xy = unique(xy,'rows'); %# remove duplicates
xy(ismember(xy,p,'rows'),:) = []; %# remove point itself
%# show solution
figure
line(p(1), p(2), 'Color','r', ...
'LineStyle','none', 'Marker','.', 'MarkerSize',50)
line(xy(:,1), xy(:,2), 'Color','b', ...
'LineStyle','none', 'Marker','.', 'MarkerSize',20)
grid on, box on, axis equal
axis([1 siz(1) 1 siz(2)])
xlabel x, ylabel y

siz = [10 15 8]; %# matrix size
p = [5 10 4]; %# 3D point location
%# neighboring points
k = 2; %# radius size
[sx,sy,sz] = ndgrid(-k:k,-k:k,-k:k); %# steps to get to neighbors
xyz = bsxfun(@plus, p, [sx(:) sy(:) sz(:)]); %# add shift
xyz = bsxfun(@min, max(xyz,1), siz); %# clamp coordinates within range
xyz = unique(xyz,'rows'); %# remove duplicates
xyz(ismember(xyz,p,'rows'),:) = []; %# remove point itself
%# show solution
figure
line(p(1), p(2), p(3), 'Color','r', ...
'LineStyle','none', 'Marker','.', 'MarkerSize',50)
line(xyz(:,1), xyz(:,2), xyz(:,3), 'Color','b', ...
'LineStyle','none', 'Marker','.', 'MarkerSize',20)
view(3), grid on, box on, axis equal
axis([1 siz(1) 1 siz(2) 1 siz(3)])
xlabel x, ylabel y, zlabel z

HTH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With