Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 找到中心像素周围半径内所有邻居的线性索引?_Matlab_Indexing_Computer Vision - Fatal编程技术网

Matlab 找到中心像素周围半径内所有邻居的线性索引?

Matlab 找到中心像素周围半径内所有邻居的线性索引?,matlab,indexing,computer-vision,Matlab,Indexing,Computer Vision,我有一个线性索引数组,对于每个线性索引,我想找到半径为5像素的相邻像素的线性索引。我找到了下面的代码,它为一个8-连接的邻域执行此任务。但是,对于5像素邻域,如何实现它来找到120个邻域的线性指数呢 %# target_array: array where pixels are marked %# idx: linear index of a marked pixel [M,N] = size(target_array) neighbor_offsets=[-M-1 -M -M+1 1 M+1

我有一个线性索引数组,对于每个线性索引,我想找到半径为
5像素的相邻像素的线性索引。我找到了下面的代码,它为一个
8-连接的邻域
执行此任务。但是,对于
5像素
邻域,如何实现它来找到
120个邻域
的线性指数呢

%# target_array: array where pixels are marked
%# idx: linear index of a marked pixel
[M,N] = size(target_array)

neighbor_offsets=[-M-1 -M -M+1 1 M+1 M M-1 -1];

neighbors = bsxfun(@plus, idx, neighbor_offsets);

您提到的代码查找像素周围的线性索引,只要该像素不太接近目标阵列的边界

在您的情况下,我建议您逐个循环像素,然后
查找邻域:

[M,N] = size(target_array);

SE = strel('disk',5,inf);

%# the linear indices are stored in idxList
nIndices = length(idxList);

neighbors = cell(nIndices);

for ii = 1:nIndices
    idx = idxList(ii);
    bw = false(M,N);
    bw(idx) = true;
    bw = imdilate(bw,SE);

    %# mask the center
    bw(idx) = false;

    neighbors{ii} = find(bw);
end
如果您知道没有任何邻域重叠或接触,则可以简化上述步骤:

bw = false(M,N);
bw(idxList = true;
bw = imdilate(bw,SE);
bw(idxList) = false;
cc = bwconncomp(bw);
neighbors = cc.PixelIdxList;

您可以使用
meshgrid
sub2ind

假设一个名为
T
的目标数组和索引点(
m
n
),其中
[m,n]=ind2sub(大小(T),ind)

但如果您担心边缘(您应该这样做),请按如下所示使用“最小值”和“最大值”:

[M, N] = size(T);
[X, Y] = meshgrid(max(1,m-5):min(M,m+5), max(1,n-5):min(N,n+5));
I = sub2ind(size(T), X(:), Y(:));

还要注意,这将包括中心点,但:;通过使用
sub2ind(size(T),m,n)查找它的线性索引,很容易删除它

你能在你的代码中定义
M
idx
吗?@Dan:我已经编辑了这个问题来定义
M
idx
,谢谢你的帮助。我在idx数组中有超过90k个条目需要处理。这种for-loop方法非常昂贵。是的,邻里之间确实有重叠。是否有任何无循环的有效方法可以在不到一秒钟的时间内处理整个idx列表?感谢您的代码!我试图优化一个需要花费大量时间计算的函数,它是基于循环邻域的。更准确地说,我有不同的圆形邻域,在每个邻域中,我想计算它们下面像素值的平均值、标准值和中值。这是在狗的比例表示的上下文中完成的。圆形过滤器的大小由特定比例的相关半径(
scalespace\u radii
)给出:欢迎对如何改进计算提出任何建议@Tin请将此作为新问题发布谢谢Dan!我已经把它作为一个新问题发布了:-)
[M, N] = size(T);
[X, Y] = meshgrid(max(1,m-5):min(M,m+5), max(1,n-5):min(N,n+5));
I = sub2ind(size(T), X(:), Y(:));