MATLAB 3D sliding window on a volume

920 views Asked by At

I have a MxNxD volume and I need a lxhxw sliding window that goes through all the voxels of the volume. In each sliding window I need to compute the Root Mean Square Contrast. Which is the smarter way to do it?

I would like to limit the use of for loops because the volume is pretty big, 1024x1024x146.

2

There are 2 answers

5
user1543042 On

It sounds like you want to divide into voxels that are l x h x w. If this is the case and assuming your matrix is called q where size(q) = [M, N, D].

function N = test(q, l, h, w)
    qPrime = mat2cell(q, ...
        [l*ones(1, floor(size(q,1)/l)), mod(size(q,1), l)], ...
        [h*ones(1, floor(size(q,2)/h)), mod(size(q,2), h)], ...
        [w*ones(1, floor(size(q,3)/w)), mod(size(q,3), w)]);

    N = cellfun(@RMS, qPrime, 'uni', 0);
end

function N = RMS(M)
    var = M - mean(M(:));
    N = sqrt(sum(var(:).^2)) ./ numel(M);
end

This makes a cell array with each voxel in a cell. Apply the function to each voxel using cellfun(@foo, qPrime, 'uni', 0).

0
BugsFree On

I have been able to solve this specific problem using the stdfilt function of MATLAB because the Root Mean Square Contrast is the standard deviation of the pixel intensities inside the sliding window. The stdfiltfunction allows you to define a

multidimensional array of zeros and ones where the nonzero elements specify the neighbors. (MATLAB Help)