Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix diffusion mask in voxels outside the FOV #129

Merged
merged 19 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions DiffusionPreprocessing/scripts/DiffusionToStructural.sh
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ cp "$DataDirectory"/bvals "$T1wOutputDirectory"/bvals

DilateDistance=`echo "$DiffRes * 4" | bc` # Extrapolates the diffusion data up to 4 voxels outside of the FOV

#Register diffusion data to T1w space. Account for gradient nonlinearities if requested
# Register diffusion data to T1w space. Account for gradient nonlinearities (using one-step resampling) if requested
if [ ${GdcorrectionFlag} -eq 1 ]; then
echo "Correcting Diffusion data for gradient nonlinearities and registering to structural space"
# We combine the GDC warp with the diff2str affine, and apply to warped/data_warped (i.e., the post-eddy, pre-GDC data)
Expand All @@ -108,11 +108,13 @@ if [ ${GdcorrectionFlag} -eq 1 ]; then
${FSLDIR}/bin/applywarp --rel -i "$DataDirectory"/warped/data_warped_dilated -r "$T1wRestoreImage"_${DiffRes} -w "$WorkingDirectory"/grad_unwarp_diff2str --interp=spline -o "$T1wOutputDirectory"/data

# Transforms field of view mask to T1-weighted space
# (Be sure to use the fov_mask derived prior to application of GDC)
${FSLDIR}/bin/applywarp --rel -i "$DataDirectory"/warped/fov_mask -r "$T1wRestoreImage"_${DiffRes} -w "$WorkingDirectory"/grad_unwarp_diff2str --interp=trilinear -o "$T1wOutputDirectory"/fov_mask
mharms marked this conversation as resolved.
Show resolved Hide resolved
MichielCottaar marked this conversation as resolved.
Show resolved Hide resolved

# Now register the grad_dev tensor
${FSLDIR}/bin/vecreg -i "$DataDirectory"/grad_dev -o "$T1wOutputDirectory"/grad_dev -r "$T1wRestoreImage"_${DiffRes} -t "$WorkingDirectory"/diff2str.mat --interp=spline
${FSLDIR}/bin/fslmaths "$T1wOutputDirectory"/grad_dev -mas "$T1wOutputDirectory"/nodif_brain_mask_temp "$T1wOutputDirectory"/grad_dev #Mask-out values outside the brain
${FSLDIR}/bin/imrm $DataDirectory/warped/data_warped_dilated
else
# Dilation outside of the field of view to minimise the effect of the hard field of view edge on the interpolation
${CARET7DIR}/wb_command -volume-dilate $DataDirectory/data.nii.gz $DilateDistance NEAREST $DataDirectory/data_dilated.nii.gz
Expand All @@ -121,13 +123,15 @@ else

# Transforms field of view mask to T1-weighted space
${FSLDIR}/bin/flirt -in "$DataDirectory"/fov_mask -ref "$T1wRestoreImage"_${DiffRes} -applyxfm -init "$WorkingDirectory"/diff2str.mat -interp trilinear -out "$T1wOutputDirectory"/fov_mask
${FSLDIR}/bin/imrm $DataDirectory/data_dilated
fi

# only include voxels fully(!) within the field of view for every volume
${FSLDIR}/bin/fslmaths "$T1wOutputDirectory"/fov_mask -thr 0.999 -bin "$T1wOutputDirectory"/fov_mask

${FSLDIR}/bin/fslmaths "$T1wOutputDirectory"/data -mas "$T1wOutputDirectory"/nodif_brain_mask_temp -mas "$T1wOutputDirectory"/fov_mask "$T1wOutputDirectory"/data #Mask-out data outside the brain
${FSLDIR}/bin/fslmaths "$T1wOutputDirectory"/data -thr 0 "$T1wOutputDirectory"/data #Remove negative intensity values (caused by spline interpolation) from final data
# Mask out data outside the brain adn outside the fov
MichielCottaar marked this conversation as resolved.
Show resolved Hide resolved
${FSLDIR}/bin/fslmaths "$T1wOutputDirectory"/data -mas "$T1wOutputDirectory"/nodif_brain_mask_temp -mas "$T1wOutputDirectory"/fov_mask "$T1wOutputDirectory"/data
${FSLDIR}/bin/fslmaths "$T1wOutputDirectory"/data -thr 0 "$T1wOutputDirectory"/data #Remove negative intensity values (from eddy) from final data
${FSLDIR}/bin/imrm "$T1wOutputDirectory"/nodif_brain_mask_temp

# Identify any voxels that are zeros across all frames and remove those from the final nodif_brain_mask
Expand All @@ -144,10 +148,5 @@ echo "${PctCoverage}, ${NvoxFinalMask}, ${NvoxBrainMask}" >> "$T1wOutputDirector

${FSLDIR}/bin/imrm "$T1wOutputDirectory"/temp
${FSLDIR}/bin/imrm "$T1wOutputDirectory"/nodif_brain_mask_old
if [ ${GdcorrectionFlag} -eq 1 ]; then
${FSLDIR}/bin/imrm $DataDirectory/warped/data_warped_dilated
else
${FSLDIR}/bin/imrm $DataDirectory/data_dilated
fi

echo " END: DiffusionToStructural"
12 changes: 7 additions & 5 deletions DiffusionPreprocessing/scripts/eddy_postproc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ fi
#fi


#Create a mask representing voxels within the field of view for all volumes prior to dilation
# Create a mask representing voxels within the field of view for all volumes prior to dilation
# 'eddy' can return negative values in some low signal locations, so use -abs for determining the fov mask
${FSLDIR}/bin/fslmaths ${datadir}/data -abs -Tmin -bin -fillh ${datadir}/fov_mask
MichielCottaar marked this conversation as resolved.
Show resolved Hide resolved


Expand All @@ -106,12 +107,10 @@ if [ ! $GdCoeffs = "NONE" ] ; then
# apply gradient distortion correction
${globalscriptsdir}/GradientDistortionUnwarp.sh --workingdir="${datadir}" --coeffs="${GdCoeffs}" --in="${datadir}/data_warped_dilated" --out="${datadir}/data" --owarp="${datadir}/fullWarp"

# Transform field of view mask (using conservative trilinear interolation with high threshold)
# Transform field of view mask (using conservative trilinear interpolation with high threshold)
${FSLDIR}/bin/immv ${datadir}/fov_mask ${datadir}/fov_mask_warped
${FSLDIR}/bin/applywarp --rel --interp=trilinear -i ${datadir}/fov_mask_warped -r ${datadir}/fov_mask_warped -w ${datadir}/fullWarp -o ${datadir}/fov_mask
${FSLDIR}/bin/fslmaths ${datadir}/fov_mask -thr 0.999 -bin ${datadir}/fov_mask
# mask out any data outside the field of view
${FSLDIR}/bin/fslmaths ${datadir}/data -mas ${datadir}/fov_mask ${datadir}/data

echo "Computing gradient coil tensor to correct for gradient nonlinearities"
${FSLDIR}/bin/calc_grad_perc_dev --fullwarp=${datadir}/fullWarp -o ${datadir}/grad_dev
Expand All @@ -130,7 +129,10 @@ if [ ! $GdCoeffs = "NONE" ] ; then
${FSLDIR}/bin/immv ${datadir}/fullWarp_abs ${datadir}/warped
fi
MichielCottaar marked this conversation as resolved.
Show resolved Hide resolved

#Remove negative intensity values (caused by spline interpolation) from final data
# mask out any data outside the field of view
${FSLDIR}/bin/fslmaths ${datadir}/data -mas ${datadir}/fov_mask ${datadir}/data

# Remove negative intensity values (from eddy) from final data
${FSLDIR}/bin/fslmaths ${datadir}/data -thr 0 ${datadir}/data
${FSLDIR}/bin/fslroi ${datadir}/data ${datadir}/nodif 0 1
${FSLDIR}/bin/bet ${datadir}/nodif ${datadir}/nodif_brain -m -f 0.1
Expand Down