Search code examples
matlabimage-processingmulticoreparfor

Using parfor in image processing with matrixes


We're developing an application which is processing medical images of an eye retina.

Very often the straight iterating through the pixel indices is used. And even when the size of images is fixed to 1024*768 pixels it may be a CPU-consuming operation e.g. to assign certain values to binarized pixels we need.

lowlayers2 = zeros(img_y_size, img_x_size);
for i=1:numel(lowlayers)
    y = rem(lowlayers(i),img_y_size);
    x = fix(lowlayers(i)/img_y_size)+1;
    lowlayers2(y,x) = 1;
end;

When trying to use parfor in a simple loop above the debugger types that all variables in the loop must be presented as sliced ones. I guess it's in order to divide iterations more primitively inside the loop.

How can I modify the loop or variable to be able to use parfor? May every variable be presented as a sliced variable (mean more multidim matrix with 2 or 3 dimentions)?


Solution

  • a sliced variable is a variable that has a reference out of parfor loop and each of its element only accessed by a single worker (in parfor paralle workers)

    sometime matlab doesnt recognize a variable in parfor loop as "sliced variable" so you could use a temporary variable and collect results after the parfor loop,

    lowlayers2 = zeros(img_y_size, img_x_size);
    parfor i=1:numel(lowlayers)
        y = rem(lowlayers(i),img_y_size);
        x = fix(lowlayers(i)/img_y_size)+1;
        t(i)= sub2ind(size(lowlayers2),y,x);
    end
    
    lowlayers2(t)=1;
    

    NOTE 1: It is better to vectorise code in the older versions because loops didn't use to be as good as they are now in R2017 referring to (this).