快速找到像素的邻居(Fast way to find the neighboor of pixel)

我编程的任务是找到图像D中给定像素x的邻居,其公式为:

显示满足与像素x的距离的像素y的公式为1,则它们是像素x邻居。 这是我的matlab代码。 但是,它仍然需要很长时间才能找到。 你能建议一个更快的方法吗? 非常感谢

%-- Find the neighborhood of one pixel % x is pixel coordinate % nrow, ncol is size of image function N = find_neighbor(x,nrow,ncol) i = x(1); j = x(2); I1 = i+1; if (I1 > nrow) I1 = nrow; end I2 = i-1; if (I2 < 1) I2 = 1; end J1 = j+1; if (J1 > ncol) J1 = ncol; end J2 = j-1; if (J2 < 1) J2 = 1; end N = [I1, I2, i, i; j, j, J1, J2];

例如: ncol=128; nrow=128; x =[30;110] ncol=128; nrow=128; x =[30;110] ncol=128; nrow=128; x =[30;110]然后输出

N =31 29 30 30; 110 110 111 109]

用于循环调用函数

x=[30 31 32 33; 110 123 122 124] for i=1:length(x) N = find_neighbor(x(:,i),nrow,ncol); end

I am programming for task that finds the neighbor of a given pixel x in image Dthat can formula as:

The formula shown pixels y which satisfy the distance to pixel x is 1, then they are neighbor of pixel x. This is my matlab code. However, it still takes long time to find. Could you suggest a faster way to do it. Thank you so much

%-- Find the neighborhood of one pixel % x is pixel coordinate % nrow, ncol is size of image function N = find_neighbor(x,nrow,ncol) i = x(1); j = x(2); I1 = i+1; if (I1 > nrow) I1 = nrow; end I2 = i-1; if (I2 < 1) I2 = 1; end J1 = j+1; if (J1 > ncol) J1 = ncol; end J2 = j-1; if (J2 < 1) J2 = 1; end N = [I1, I2, i, i; j, j, J1, J2];

For example: ncol=128; nrow=128; x =[30;110] then output

N =31 29 30 30; 110 110 111 109]

For calling the function in loop

x=[30 31 32 33; 110 123 122 124] for i=1:length(x) N = find_neighbor(x(:,i),nrow,ncol); end

最满意答案

这是使用bsxfun的矢量化方法:

% define four neighbors as coordinate differences d = [-1 0 ; 1 0 ; 0 -1 ; 0 1]'; % add to pixel coordinates N = bsxfun(@plus, x, permute(d, [1 3 2])); % make one long list for the neighbors of all pixels together N = reshape(N, 2, []); % identify out-of-bounds coordinates ind = (N(1, :) < 1) | (N(1, :) > nrow) | (N(2, :) < 1) | (N(2, :) > ncol); % and remove those "neighbors" N(:, ind) = [];

permute是将四个不同邻居的“维度”移动到第三个数组索引中。 这样,使用bsxfun ,我们得到每对原始像素坐标与每对相对邻居坐标的组合。 越界检查假定nrow属于第一个坐标, nrow属于第二个坐标。

ncol=128; nrow=128; x = [30 31 32 33; 110 123 122 124];

结果是

N = 29 30 31 32 31 32 33 34 30 31 32 33 30 31 32 33 110 123 122 124 110 123 122 124 109 122 121 123 111 124 123 125

不同像素的不同邻居最终可能是相同的像素,因此列表中可能存在重复。 如果您只想要每个结果像素一次,请使用

% remove duplicates? N = unique(N', 'rows')';

要得到

N = 29 30 30 30 31 31 31 32 32 32 33 33 33 34 110 109 111 123 110 122 124 121 123 124 122 123 125 124

Here's a vectorized approach using bsxfun:

% define four neighbors as coordinate differences d = [-1 0 ; 1 0 ; 0 -1 ; 0 1]'; % add to pixel coordinates N = bsxfun(@plus, x, permute(d, [1 3 2])); % make one long list for the neighbors of all pixels together N = reshape(N, 2, []); % identify out-of-bounds coordinates ind = (N(1, :) < 1) | (N(1, :) > nrow) | (N(2, :) < 1) | (N(2, :) > ncol); % and remove those "neighbors" N(:, ind) = [];

The permute is there to move the "dimension" of four different neighbors into the 3rd array index. This way, using bsxfun, we get the combination of every pair of original pixel coordinates with every pair of relative neighbor coordinates. The out-of-bounds check assumes that nrow belongs to the first coordinate and ncol to the second coordinate.

With

ncol=128; nrow=128; x = [30 31 32 33; 110 123 122 124];

the result is

N = 29 30 31 32 31 32 33 34 30 31 32 33 30 31 32 33 110 123 122 124 110 123 122 124 109 122 121 123 111 124 123 125

Different neighbors of different pixels can end up to be the same pixel, so there can be duplicates in the list. If you only want each resulting pixel once, use

% remove duplicates? N = unique(N', 'rows')';

to get

N = 29 30 30 30 31 31 31 32 32 32 33 33 33 34 110 109 111 123 110 122 124 121 123 124 122 123 125 124

更多推荐