图像中的邻域半径

时间:2012-08-08 00:51:27

标签: matlab image-processing

我有一张图片(200x200),想要找到具有预定义半径的特定点的邻域位置。例如,半径为5时,我在一个点周围有25个点。 MATLAB可以做到吗?问题是关于图像的边缘,它并不总是25点,程序应该只找到该半径范围内的点。这些点可以从1(角落)到25(图像中心)变化

2 个答案:

答案 0 :(得分:6)

以下是一个例子:

%# sample grayscale image
img = imread('cameraman.tif');
[imgH,imgW,~] = size(img);

%# circle params
t = linspace(0, 2*pi, 50);   %# approximate circle with 50 points
r = 80;                      %# radius
c = [100 130];               %# center

%# get circular mask
BW = poly2mask(r*cos(t)+c(1), r*sin(t)+c(2), imgH, imgW);

%# show cropped image
imshow( immultiply(img,BW) )
axis on

screenshot

这将处理边缘情况就好了。使用POLY2MASK的优点是它以亚像素精度计算掩模(请阅读函数文档中的算法部分),前提是您使用足够的点来近似圆。

答案 1 :(得分:2)

根据评论中的讨论,我正在添加另一个解决方案。对于给定点,我们计算指定步数内的相邻点(如果愿意,则计算半径)。这显示了2D和3D情况。

2D矩阵

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

screenshot_2D_matrix


3D矩阵

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

screenshot_3D_matrix

HTH