% function call: matlab -nodisplay -nosplash -nodesktop -nojvm -r "n3Correct('inputImg.nii', 'maskImg.nii', 'outputImg.nii'); quit"
function n3Correct(input, brainMask, output)

    image=load_untouch_nii(input);
    m=load_untouch_nii(brainMask);

    [correctedData, biasField, mask] = iic(image.img, ...
    'smoothingDistance', 30, ...
    'stepSize', [image.hdr.dime.pixdim(2), image.hdr.dime.pixdim(3), image.hdr.dime.pixdim(4)], ...
    'smoothingRegularization', 1e-4, ...
    'mask', double(m.img));

    image.img=correctedData;
    save_untouch_nii(image,output);
end

