2011-10-18 102 views
0

我使用John Burkardt的CONTRAST2_SPMD的修改版本在大型灰度圖像上運行3x3方差計算。這樣做的好處是我可以使用PCT在本地機器上使用8個內核,但是我想改變窗口大小(目前爲3x3),但是我想嘗試幾種窗口大小。我如何修改下面的代碼以允許可變窗口大小? (我將使用奇數,方形窗口,例如3x3,5x5,7x7,9x9等)MATLAB SPMD和nlfilter - 更改窗口大小

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 

在此先感謝您的幫助。

回答

1

您需要修改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。在發送右側「幻象單元格」時以及刪除「額外列」時,會做出相應的更改。

+0

謝謝!在我回到這裏之前,我已經設法破解它,但它的修復程序相同。 – MBL