【问题标题】:Find linear indices of all the neighbors in a radius around the central pixel? [duplicate]找到中心像素周围半径内所有邻居的线性索引? [复制]
【发布时间】:2013-04-29 10:23:02
【问题描述】:

我有一个线性索引数组,对于每个线性索引,我想找到半径为5-pixels 的相邻像素的线性索引。我找到了以下代码,它为8-connected neighborhood 完成了这项工作。但是,如何实现它以找到120 neighbors 的线性索引,用于5-pixel 邻域。

%# 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);

【问题讨论】:

  • 你能在你的代码中定义Midx吗?
  • @Dan:我已经编辑了问题以定义Midx

标签: matlab indexing computer-vision


【解决方案1】:

您提到的代码会找到像素周围的线性索引,只要该像素不太靠近目标数组的边界即可。

在你的情况下,我建议你逐个循环遍历像素,find 邻域:

[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;

【讨论】:

  • 感谢您的帮助。我在 idx 数组中有超过 90k 个条目要处理。这种for循环方法非常昂贵。是的,社区确实重叠。是否有任何无循环的有效方法可以在不到一秒的时间内处理整个 idx 列表?
【解决方案2】:

您可以使用 meshgridsub2ind 来实现

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

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

但如果你担心边缘(你应该是),那么使用 min 和 max 如下:

[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);找到它的线性索引来删除它

【讨论】:

  • 感谢您的代码丹!我正在尝试优化一个需要花费大量时间来计算的函数,它基于循环邻域。更准确地说,我有不同的圆形邻域,在每个邻域内部,我想计算它们底层像素值的平均值、标准差和中值。这是在 DoG 比例表示的上下文中完成的。圆形过滤器的大小由与特定比例关联的半径给出 (scalespace_radii):pastebin.com/WkS5ShMf 欢迎任何有关如何改进计算的建议!
  • @Tin 请将此作为新问题发布
  • 感谢丹!我已经把它作为一个新问题发布了:-) stackoverflow.com/questions/21724404/…
猜你喜欢
  • 1970-01-01
  • 2018-08-19
  • 1970-01-01
  • 2013-04-08
  • 2020-03-05
  • 1970-01-01
  • 2023-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多