MATLAB: Masking across image but keeping centre

arraysdigital signal processingimage processingindexing

Hi, I have multidimensional data that I want to mask. I can do this by using a mask of ones, and then selectively taking elements that I want to ignore and making those in the mask 0. I then multiply my data with this mask, like so:
% Masking - Factor 2

MASK_ODD_ECHOS = ones(size(kspace));
MASK_ODD_ECHOS(:,1:2:end,:,:,:,:,1:2:end,:) = 0;
MASK_EVEN_ECHOS = ones(size(kspace));
MASK_EVEN_ECHOS(:,2:2:end,:,:,:,:,2:2:end,:) = 0;
However, I also want to always take the central 10% of this array on the (2nd dimension), which extends from 1 to 320. Thus I want to take 32 data points (ie: 10 % of 320) from the centre of a length 320 array, which means taking 16 on points on the left of datapoint 160, and 16 points to the right, giving me a central region index of 144:176. But I can't easily set this to 1 as some would have already been zero'd out by then.
I have a hacky solution to this which is to have 2 sets of mask. Start from the beginning, mask until centre. Skip centre, then mask again after centre.
% Masking - Factor 2
MASK_ODD_ECHOS_1 = ones(size(kspace));
MASK_ODD_ECHOS_2 = ones(size(kspace));
MASK_ODD_ECHOS_1(:,1:2:143,:,:,:,:,1:2:end,:) = 0;
MASK_ODD_ECHOS_2(:,177:2:end,:,:,:,:,1:2:end,:) = 0;
% Undersample even echos (2:2:end), skip every 2nd PE line with shift of one (2:2:end)
MASK_EVEN_ECHOS_1 = ones(size(kspace));
MASK_EVEN_ECHOS_2 = ones(size(kspace));
MASK_EVEN_ECHOS_1(:,2:2:142,:,:,:,:,2:2:end,:) = 0;
MASK_EVEN_ECHOS_2(:,178:2:end,:,:,:,:,2:2:end,:) = 0;
% Applying Masks:
kspace_CAIPI = kspace .* MASK_ODD_ECHOS_1;
kspace_CAIPI = kspace_CAIPI .* MASK_ODD_ECHOS_2;
kspace_CAIPI = kspace_CAIPI .* MASK_EVEN_ECHOS_1;
kspace_CAIPI = kspace_CAIPI .* MASK_EVEN_ECHOS_2;
This is inelegant and I feel this could be done in the opposite way: Start with a mask of zero, and set to 1 all points.
MASK_ODD_ECHOS = zeros(size(kspace));
MASK_ODD_ECHOS(:,1:2:end,:,:,:,:,1:2:end,:) = 1;
But this would mean losing all other data points. Is there a way to access those points in a more elegant way without making everything 0?

Best Answer

  • But I can't easily set this to 1 as some would have already been zero'd out by then.
    I don't understand why you don't think you can just restore the mask values you've set to zero back to 1,
    MASK_ODD_ECHOS = true(size(kspace));
    MASK_ODD_ECHOS(:,1:2:end,:,:,:,:,1:2:end,:) = 0;
    MASK_ODD_ECHOS(:,144:176,:,:,:,:,1:2:end,:) = 1;
    MASK_EVEN_ECHOS = true(size(kspace));
    MASK_EVEN_ECHOS(:,2:2:end,:,:,:,:,2:2:end,:) = 0;
    MASK_EVEN_ECHOS(:,144:176,:,:,:,:,2:2:end,:) = 1;
    but regardless, a slightly shorter way to accomplish the same would be,
    % Masking - Factor 2
    MASK_ODD_ECHOS = true(size(kspace));
    MASK_ODD_ECHOS(:,[1:2:143, 177:2:end],:,:,:,:,1:2:end,:) = 0;
    MASK_EVEN_ECHOS = true(size(kspace));
    MASK_EVEN_ECHOS(:,[2:2:142, 178:2:end],:,:,:,:,2:2:end,:) = 0;