【问题标题】:quickest way to calculate neighbourhood of pixels计算像素邻域的最快方法
【发布时间】:2013-09-06 14:03:48
【问题描述】:

我有一个由正值和负值组成的矩阵。我需要做这些事情。

让u(i,j) 表示矩阵u 的像素。

  1. 计算过零像素。如果u(i-1,j) 和u(i+1,j) 的符号相反,或者u(i,j-1) 和u(i,j+1) 的符号相反,则这些是网格中的像素。
  2. 然后我需要计算这些过零像素周围的窄带。每个像素的窄带宽度为(2r+1)X(2r+1)。我正在使用r=1,因此我必须实际获取每个过零像素的 8 个邻域像素。

我已经在一个程序中做到了这一点。请看下面。

%// calculate the zero crossing pixels  
front = isfront(u);
%// calculate the narrow band of around the zero crossing pixels
band  = isband(u,front,1);

我还附加了isfront 和isband 函数。

function front = isfront( phi )
%// grab the size of phi
[n, m] = size(phi);

%// create an boolean matrix whose value at each pixel is 0 or 1
%// depending on whether that pixel is a front point or not
front = zeros( size(phi) );

%// A piecewise(Segmentation) linear approximation to the front is contructed by
%// checking each pixels neighbour. Do not check pixels on border.
for i = 2 : n - 1;
  for j = 2 : m - 1;
    if (phi(i-1,j)*phi(i+1,j)<0) || (phi(i,j-1)*phi(i,j+1)<0)
        front(i,j) = 100;
    else
        front(i,j) = 0;
    end
  end
end

function band = isband(phi, front, width)
%// grab size of phi
[m, n] = size(phi);

%// width=r=1;
width = 1;

[x,y] = find(front==100);

%// create an boolean matrix whose value at each pixel is 0 or 1
%// depending on whether that pixel is a band point or not
band = zeros(m, n);

%// for each pixel in phi
for ii = 1:m
  for jj = 1:n
    for k = 1:size(x,1)
        if (ii==x(k)) && (jj==y(k))
            band(ii-1,jj-1) = 100;  band(ii-1,jj) = 100; band(ii-1,jj+1) = 100;
            band(ii  ,jj-1) = 100;  band(ii  ,jj) = 100; band(ii,jj+1) = 100;
            band(ii+1,jj-1) = 100;  band(ii+1,jj) = 100; band(ii+1,jj+1) = 100;
        end
    end
  end
end

输出如下:,以及计算时间:

%// Computation time

%// for isfront function
Elapsed time is 0.003413 seconds.

%// for isband function
Elapsed time is 0.026188 seconds.

当我运行代码时,我确实得到了正确的答案,但任务的计算对我来说太过分了。有更好的方法吗?特别是isband 函数?如何进一步优化我的代码?

提前致谢。

【问题讨论】:

  • 你考虑过形态学运算吗,比如bwmorph?
  • 注意:isband 右下角两个相邻像素的访问编码不正确;您可能复制粘贴了,忘记将 -1 更正为 +1 和 +0。
  • @RodyOldenhuis 感谢您的评论。是的,我复制并粘贴了它,所以这是一个错误。编辑我的问题以更正它。

标签: matlab optimization image-processing neighbours


【解决方案1】:

如果你只对r==1感兴趣,看makelut和对应的函数bwloolup。

[编辑]

% Let u(i,j) denote the pixels of the matrix u. Calculate the zero crossing
% pixels. These are the pixels in the grid if u(i-1,j) and u(i+1,j) are of
% opposite signs or u(i,j-1) and u(i,j+1) are of opposite signs.

% First, create a function which will us if a pixel is a zero crossing
% pixel, given its 8 neighbors. 

% uSign = u>0; % Note - 0 is treated as negative here. 

% uSign is 3x3, we are evaluating the center pixel uSign(2,2)
zcFun = @(uSign) (uSign(1,2)~=uSign(3,2)) || (uSign(2,1)~=uSign(2,3));

% Make a look up table which tells us what the output should be for the 2^9
% = 512 possible patterns of 3x3 arrays with 1's and 0's.
lut   = makelut(zcFun,3);

% Test image
im = double(imread('pout.tif'));
% Create positve and negative values
im = im -mean(im(:));

% Apply lookup table
imSign = im>0;
imZC   = bwlookup(imSign,lut);

imshowpair(im, imZC);

【讨论】:

  • 你能告诉我怎么做吗?我浏览了材料,但我不清楚。我对形态学方法的想法很少。请提供一些有关如何应用它的提示。
【解决方案2】:

正如 EitanT 所建议的,至少有 bwmorph 已经可以满足您的需求。

如果你无权使用图像处理工具箱,或者只是坚持自己做:

您可以将isfront 中的三重循环替换为矢量化

front = zeros(n,m);

zero_crossers = ...
    phi(1:end-2,2:end-1).*phi(3:end,2:end-1) < 0 | ...
    phi(2:end-1,1:end-2).*phi(2:end-1,3:end) < 0;

front([...
                   false(1,m)
    false(n-2,1)  zero_crossers  false(n-2,1)
                   false(1,m)                 ]...
) = 100;

你可以用这个单循环替换isband:

[n,m] = size(front);
band = zeros(n,m);
[x,y] = find(front);
for ii = 1:numel(x)
    band(...
        max(x(ii)-width,1) : min(x(ii)+width,n),...
        max(y(ii)-width,1) : min(y(ii)+width,m)) = 1;
end

或者,按照米兰的建议,您可以通过convolution 应用图像膨胀:

kernel = ones(2*width+1);    
band = conv2(front, kernel);
band = 100 * (band(width+1:end-width, width+1:end-width) > 0);

应该会更快。

您当然可以进行一些其他的小优化(isband 不需要 phi 作为参数,您可以将 front 作为逻辑数组传递,以便 find 更快,等等)。

【讨论】:

  • 一个快速信息我试图用尺寸为 75X79 的图像来计时,我对我的函数 is_front 和你的函数 is_front 都进行了计时。以下是结果:run1:经过的时间是 0.003101 秒。经过的时间是 0.004594 秒。 run2:经过的时间为 0.002801 秒。经过的时间是 0.004535 秒。
  • 然后我将图像大小调整为 4000X4000,我得到了这个计时结果:run1: Elapsed time is 2.646254 seconds。经过的时间是 0.766821 秒。 run2:经过的时间是 2.853837 秒。经过的时间是 0.737387 秒。为什么我的代码对于较小的数组更快,而对于较大的数组更慢?你可以解释吗 ?向量化代码不应该总是运行得更快吗?
  • 函数 is_band run1 类似:我的函数:0.006310 秒。你的函数:0.004052 秒。 run2:我的函数:0.005674 秒。你的函数:0.004003 秒。 run3:我的函数:0.005919 秒。你的函数:0.003908 秒。
  • 当图像调整为 4000X4000 时:函数 is_band run1 类似:我的函数:4.978568 秒。你的函数:0.072092 秒。 run2:我的函数:5.035173 秒。你的函数:0.072937 秒。 run3:我的函数:5.049851 秒。你的函数:0.082769 秒。
  • @roni:这通常是矢量化的情况;它开始对更大的阵列有益。这是因为它有一些开销(例如,所有这些索引向量都需要由 MATLAB 创建和计算出来),这会转化为实际计算开始之前 的非零时间。在那段时间里,小的(和 JIT 的)循环可能已经开始计算了。但是在阈值大小之后循环无法更快。无论如何,我认为你可以忍受 0.004 秒的等待,对吧? :)
猜你喜欢
  • 2017-11-01
  • 2013-02-13
  • 2013-07-02
  • 2010-09-07
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2013-06-15
  • 2018-01-10
相关资源
最近更新 更多