fslmaths code example

Example 1: fslmaths

#!/bin/sh

# Any image containing NANs may not be read correctly 
# by fslmaths or fslstats.
# -nan : replace NaNs (improper numbers) with 0

fslmaths img -nan img_no_nans

Example 2: fslmaths

# FSL: fsl dilate or fsl erode: 
# Kernel operations (set BEFORE filtering operation if desired): 
# -kernel 3D : 3x3x3 box centered on target voxel (set as default kernel)
# -kernel 2D : 3x3x1 box centered on target voxel 
# Spatial Filtering operations: 
# N.B. all options apart from -s use the default kernel or that 
# previously specified by -kernel 
# -dilM : Mean Dilation of non-zero voxels 
# -dilD : Modal Dilation of non-zero voxels 
# -dilF : Maximum filtering of all voxels 
# -dilall : Apply -dilM repeatedly until the entire FOV is covered 
# -ero : Erode by zeroing non-zero voxels when zero voxels found in kernel 
# -eroF : Minimum filtering of all voxels 

# dilation example 
# -odt sets the output data type. 
# odt options are char short int float double
fslmaths mask -kernel 3D -dilM mask -odt short

# erosion example
fslmaths mask -kernel 2D -ero mask -odt short

Example 3: fslmaths

#!/bin/sh

# Subtract one mask from another.  Binarize the resulting difference
# with -bin to get rid of negative numbers. See the jaccard index to expand on this use.

fslmaths mask1 -sub mask2 -bin mask_diff

Example 4: fslmaths

#!/bin/sh

# Add 100 to every voxel in image (making the image uniformly brighter)

fslmaths img -add 100 img_bright

Example 5: fslmaths

#!/bin/sh

# Multiply image by mask to extract just those voxels 
# from image that are in the mask (see also -mas):

fslmaths img -mul mask img_in_mask

Example 6: fslmaths

#!/bin/bash

# usage: jaccard filename1 filename2
# this script uses the fsl toolbox and "bc" to calculate the Jaccard Index
# and Jaccard distance statistics for two images in the same space.
# See http://en.wikipedia.org/wiki/Jaccard_index
# the scale=6 variable sets the number of decimal places to use.
# Written by Dianne Patterson 11/9/2011
# Based on a dice_kappa calculation script written by Matt Petoe 11/2/2011


: <<COMMENTBLOCK

Function: Jaccard
Purpose: The Jaccard index measures the similarity between 2 sample sets
The Jaccard distance measures the dissimilarity between 2 sample
data sets
Input: 2 image file names
Output: jaccard_index and jaccard_distance

COMMENTBLOCK

Jaccard ()
{
    roi1=$1 # Define the first argument as roi1
	roi2=$2 # Define the second argument as roi2
	stem1=$(basename -s .nii.gz ${roi1})
	stem2=$(basename -s .nii.gz ${roi2})
	dir1=$(dirname ${roi1})
	dir2=$(dirname ${roi2})

	# Use fslstats to get the number of voxels in roi1
	total_voxels1=$(fslstats ${roi1} -V | awk '{printf $1 "\n"}')
	# Use fslstats to get the number of voxels in roi2
	total_voxels2=$(fslstats "$2" -V | awk '{printf $1 "\n"}')

	if [ ${total_voxels1} -ne 0 ] && [ ${total_voxels2} -ne 0 ]; then
		# Use fslstats -k to mask roi2 with roi1, and get the number of voxels in the intersection
		intersect_voxels=$(fslstats ${roi1} -k ${roi2} -V | awk '{printf $1 "\n"}')
		# Use fslstats -add to add roi1 and roi2, and get the number of voxels in the union
		fslmaths ${roi1} -add ${roi2} -bin union
		union_voxels=$(fslstats union -V | awk '{printf $1 "\n"}')
		# Use bc to calculate the jaccard index (intersection/union)
		jaccard_index=$(echo "scale=6; ${intersect_voxels}/${union_voxels}" | bc)
		# Use bc to calculate the jaccard distance 1-(intersection/union)
		jaccard_distance=$(echo "scale=6; 1-(${intersect_voxels}/${union_voxels})" | bc)
		#echo "Jaccard distance is ${jaccard_distance}"
	else
		echo "At least one volume is 0"
	fi

rm union.nii.gz

}

#==============================================================================


roi1=$1 # Define the first argument as roi1
roi2=$2 # Define the second argument as roi2

Jaccard ${roi1} ${roi2}

if [ ${total_voxels1} -ne 0 ] && [ ${total_voxels2} -ne 0 ]; then
  echo "Total voxels ${stem1} is ${total_voxels1}"
  echo "Total voxels ${stem2} is ${total_voxels2}"
  echo "Intersect voxels are ${intersect_voxels}"
  echo "Union voxels are ${union_voxels}"
  echo "Jaccard index is ${jaccard_index}"
  echo "Jaccard distance is ${jaccard_distance}"
else
  echo "At least one volume is empty"
fi

# echo roi1 roi2 jaccard_index jaccard_distance to the text file.
# create the header row if jaccard.txt does not exist.
if [ ! -e jaccard.txt ]; then
	touch jaccard.txt
	echo "dir1 roi1 dir2 roi2 total_voxels1 total_voxels2 intersect_voxels union_voxels jaccard_index jaccard_distance" > jaccard.txt
fi

echo "${dir1} ${stem1} ${dir2} ${stem2} ${total_voxels1} ${total_voxels2} ${intersect_voxels} ${union_voxels}  ${jaccard_index} ${jaccard_distance}" >>jaccard.txt

#==============================================================================

Example 7: fslmaths

#!/bin/sh

# Make a 5 percent mask (default choice for fdt results).
# fdt is FSL's diffusion toolbox.
# Use following percentage (0-100) of ROBUST RANGE of 
# non-zero voxels and threshold below that percentage

fslmaths img -thrp img_5p

Example 8: fslmaths

#!/bin/sh

# Make an empty image (same dimensions as input image). 
# See also fslhd.

fslmaths img -sub img empty_img # subtract the image from itself
# OR
fslmaths img -mul 0 empty_img  # mulitply the image by 0

Example 9: fslmaths

#!/bin/sh

# Make an outline from a mask (using the -edge flag)

fslmaths mask -edge mask_outline

Example 10: fslmaths

#!/bin/sh

# Make image containing positive values only by thresholding at 0

fslmaths img -thr 0 img_pos