MATLAB SPMD 和 nlfilter - 更改窗口大小

发布于 2024-12-10 15:58:40 字数 2720 浏览 2 评论 0原文

我正在使用 John Burkardt 的 CONTRAST2_SPMD 的修改版本对大型灰度图像运行 3x3 方差计算。这样做的好处是我可以使用 PCT 在我的本地机器上使用 8 核,但是我想更改窗口大小(目前为 3x3),但我想尝试几种窗口大小。我如何修改下面的代码以允许可变的窗口大小? (我将使用奇数方形窗口,例如 3x3、5x5、7x7、9x9 等)

function y = parwinvar ( x )

%**************************************************************************
%
%
%% PARWINVAR uses MATLAB's SPMD command for parallel windowed variance.
%
%  Discussion:
%    Calculates windowed standard deviation (squared to get variance).
%    Based on CONTRAST2_SPMD by John Burkardt.
%
%
%  Parameters:
%
%    Input, image X, the initial black and white image.
%    Output, image Y, the contrast-enhanced black and white image.
%
%
%
%  Open the matlabpool
%
    matlabpool open local 8
%
% Check image is grayscale, if not covert it
% 
    if ndims(x)>2
         x=rgb2gray(x);
    end
%
%  Since the image is black and white, it is a 2D array.
%  Hence, it will be distributed by columns.
%
  xd = distributed ( x );
%
%  Have each worker enhance the contrast in its portion of the picture.
%
%  You will see lots of meaningless error messages, because NLFILTER
%  wants to put out a "waitbar" telling you to wait.  But the workers
%  don't have an associated display.
%
  spmd

    xl = getLocalPart ( xd );
%
%  In order to use LABSENDRECEIVE, we need to reference the previous
%  and next labindex.
%
    if ( labindex ~= 1 )
      previous = labindex - 1;
    else
      previous = numlabs;
    end

    if ( labindex ~= numlabs )
      next = labindex + 1;
    else
  next = 1;
end
%
%  Each worker sends its first column to the previous worker,
%  and receives corresponding data from the next worker.
%
column = labSendReceive ( previous, next, xl(:,1) );

if ( labindex < numlabs )
  xl = [ xl, column ];
end
%
%  Each worker sends its last column to the next worker,
%  and receives corresponding data from the previous worker.
%
column = labSendReceive ( next, previous, xl(:,end) );

if ( 1 < labindex )
  xl = [ column, xl ];
end
%
%  Now do the enhancement.
%  We can only do a 3x3 neighborhood, because we're only sending
%  one column to the left and right.
%   
xl = nlfilter ( xl, [3,3], @std2 );
%
%  Now strip off the extra columns.
%
    if ( 1 < labindex )
      xl = xl(:,2:end);
    end

    if ( labindex < numlabs )
      xl = xl(:,1:end-1);
    end

   xl = uint8 ( xl );

  end
%
%  We are working with a black and white image, so we can simply
%  concatenate the submatrices to get the whole object.
%
  y = [ xl{:} ];
  y = (y .* y); % square to get variance
% Close matlabpool
matlabpool close
  return
end

提前感谢您的帮助。

I am using a modified version of CONTRAST2_SPMD by John Burkardt to run a 3x3 variance calculation on a large grayscale image. The advantage of this is that I can use the PCT to use 8 cores on my local machine, however I would like to change the window size (at the moment its 3x3), but I would like to try several window sizes. How would I modify the code below to allow for variable window sizes? (I will be using odd, square windows, e.g. 3x3,5x5,7x7,9x9 etc.)

function y = parwinvar ( x )

%**************************************************************************
%
%
%% PARWINVAR uses MATLAB's SPMD command for parallel windowed variance.
%
%  Discussion:
%    Calculates windowed standard deviation (squared to get variance).
%    Based on CONTRAST2_SPMD by John Burkardt.
%
%
%  Parameters:
%
%    Input, image X, the initial black and white image.
%    Output, image Y, the contrast-enhanced black and white image.
%
%
%
%  Open the matlabpool
%
    matlabpool open local 8
%
% Check image is grayscale, if not covert it
% 
    if ndims(x)>2
         x=rgb2gray(x);
    end
%
%  Since the image is black and white, it is a 2D array.
%  Hence, it will be distributed by columns.
%
  xd = distributed ( x );
%
%  Have each worker enhance the contrast in its portion of the picture.
%
%  You will see lots of meaningless error messages, because NLFILTER
%  wants to put out a "waitbar" telling you to wait.  But the workers
%  don't have an associated display.
%
  spmd

    xl = getLocalPart ( xd );
%
%  In order to use LABSENDRECEIVE, we need to reference the previous
%  and next labindex.
%
    if ( labindex ~= 1 )
      previous = labindex - 1;
    else
      previous = numlabs;
    end

    if ( labindex ~= numlabs )
      next = labindex + 1;
    else
  next = 1;
end
%
%  Each worker sends its first column to the previous worker,
%  and receives corresponding data from the next worker.
%
column = labSendReceive ( previous, next, xl(:,1) );

if ( labindex < numlabs )
  xl = [ xl, column ];
end
%
%  Each worker sends its last column to the next worker,
%  and receives corresponding data from the previous worker.
%
column = labSendReceive ( next, previous, xl(:,end) );

if ( 1 < labindex )
  xl = [ column, xl ];
end
%
%  Now do the enhancement.
%  We can only do a 3x3 neighborhood, because we're only sending
%  one column to the left and right.
%   
xl = nlfilter ( xl, [3,3], @std2 );
%
%  Now strip off the extra columns.
%
    if ( 1 < labindex )
      xl = xl(:,2:end);
    end

    if ( labindex < numlabs )
      xl = xl(:,1:end-1);
    end

   xl = uint8 ( xl );

  end
%
%  We are working with a black and white image, so we can simply
%  concatenate the submatrices to get the whole object.
%
  y = [ xl{:} ];
  y = (y .* y); % square to get variance
% Close matlabpool
matlabpool close
  return
end

Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你穿错了嫁妆 2024-12-17 15:58:40

您需要修改 labSendReceive 行以发送更多数据。特别是,像这样的东西:

%
%  Each worker sends its first column to the previous worker,
%  and receives corresponding data from the next worker.
%
column = labSendReceive ( previous, next, xl(:,1) );

需要变成:

%
%  Each worker sends its first N columns to the previous worker,
%  and receives corresponding data from the next worker.
%
columns = labSendReceive ( previous, next, xl(:,1:N) );

其中N(blocksize-1)/2。在发送右侧“幽灵单元”以及剥离“额外列”时,需要进行相应的更改。

You need to modify the labSendReceive lines to send more data. In particular, something like this:

%
%  Each worker sends its first column to the previous worker,
%  and receives corresponding data from the next worker.
%
column = labSendReceive ( previous, next, xl(:,1) );

Needs to become:

%
%  Each worker sends its first N columns to the previous worker,
%  and receives corresponding data from the next worker.
%
columns = labSendReceive ( previous, next, xl(:,1:N) );

where N is (blocksize-1)/2. There's a corresponding change to be made when sending the right-hand-side 'ghost cells', and when stripping off the 'extra columns'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文