This document is demo how to use R to Pre-Processing Neuro Image .We divide image pre-processing into four main conceptual steps
options(fsl.path= "/usr/share/fsl/5.0") # fsl-complete install 2 version 4.1 and 5.0 on ubuntu/debian
library(oro.nifti)
## oro.nifti 0.5.2
library(fslr)
## Loading required package: stringr
destfile <- "113-01-MPRAGE.nii.gz"
if(!file.exists(destfile))
{
url <- "https://raw.githubusercontent.com/muschellij2/Neurohacking_data/master/kirby21/visit_1/113/113-01-MPRAGE.nii.gz"
name <- file.path(getwd(), destfile)
download.file(url, destfile,mode="wb") # NIfTI is binaryfile format
}
nim=readNIfTI("113-01-MPRAGE.nii.gz", reorient=FALSE)
Sys.setenv("LD_LIBRARY_PATH"="/usr/local/lib/") # R process maybe ignore LD_LIBRARY_PATH so i set it in code
mean(nim)
## [1] 117377.2
fslstats(nim, opts= "-m")
## Warning in get.fsloutput(): Can't find FSLOUTPUTTYPE, setting to NIFTI_GZ
## FSLDIR='/usr/share/fsl/5.0'; PATH=${FSLDIR}/bin:${PATH};export PATH FSLDIR; sh "${FSLDIR}/etc/fslconf/fsl.sh"; FSLOUTPUTTYPE=NIFTI_GZ; export FSLOUTPUTTYPE; ${FSLDIR}/bin/fslstats "/tmp/RtmpLNWQ63/file759329b61fc.nii.gz" -m
## [1] "117377.191744"
fslstats("113-01-MPRAGE.nii.gz",opts="-m")
## FSLDIR='/usr/share/fsl/5.0'; PATH=${FSLDIR}/bin:${PATH};export PATH FSLDIR; sh "${FSLDIR}/etc/fslconf/fsl.sh"; FSLOUTPUTTYPE=NIFTI_GZ; export FSLOUTPUTTYPE; ${FSLDIR}/bin/fslstats "/home/ngocbd/NeuroHacking/113-01-MPRAGE.nii.gz" -m
## [1] "117377.191744"
fslstats.help()
## Usage: fslstats [preoptions] <input> [options]preoption -t will give a separate output line for each 3D volume of a 4D timeseriespreoption -K < indexMask > will generate seperate n submasks from indexMask, for indexvalues 1..n where n is the maximum index value in indexMask, and generate statistics for each submaskNote - options are applied in order, e.g. -M -l 10 -M will report the non-zero mean, apply a threshold and then report the new nonzero mean-l <lthresh> : set lower threshold-u <uthresh> : set upper threshold-r : output <robust min intensity> <robust max intensity>-R : output <min intensity> <max intensity>-e : output mean entropy ; mean(-i*ln(i))-E : output mean entropy (of nonzero voxels)-v : output <voxels> <volume>-V : output <voxels> <volume> (for nonzero voxels)-m : output mean-M : output mean (for nonzero voxels)-s : output standard deviation-S : output standard deviation (for nonzero voxels)-w : output smallest ROI <xmin> <xsize> <ymin> <ysize> <zmin> <zsize> <tmin> <tsize> containing nonzero voxels-x : output co-ordinates of maximum voxel-X : output co-ordinates of minimum voxel-c : output centre-of-gravity (cog) in mm coordinates-C : output centre-of-gravity (cog) in voxel coordinates-p <n> : output nth percentile (n between 0 and 100)-P <n> : output nth percentile (for nonzero voxels)-a : use absolute values of all image intensities-n : treat NaN or Inf as zero for subsequent stats-k <mask> : use the specified image (filename) for masking - overrides lower and upper thresholds-d <image> : take the difference between the base image and the image specified here-h <nbins> : output a histogram (for the thresholded/masked voxels only) with nbins-H <nbins> <min> <max> : output a histogram (for the thresholded/masked voxels only) with nbins and histogram limits of min and maxNote - thresholds are not inclusive ie lthresh<allowed<uthresh
fast_img = fsl_biascorrect(nim, retimg=TRUE)
## FSLDIR='/usr/share/fsl/5.0'; PATH=${FSLDIR}/bin:${PATH};export PATH FSLDIR; sh "${FSLDIR}/etc/fslconf/fsl.sh"; FSLOUTPUTTYPE=NIFTI_GZ; export FSLOUTPUTTYPE; ${FSLDIR}/bin/fast -B --nopve --out="/tmp/RtmpLNWQ63/file75935855a906" "/tmp/RtmpLNWQ63/file7593732bf62d.nii.gz";
orthographic(nim)
orthographic(fast_img)
library(scales)
sub.bias <- niftiarr(nim, nim-fast_img)
# quantile the difference image using these as breaks
q=quantile(sub.bias[sub.bias !=0],probs = seq(0,1,by=0.1))
# get a diverging gradient palette
fcol=div_gradient_pal(low="blue",mid="yellow",high ="red")
ortho2(nim,sub.bias,col.y = alpha(fcol(seq(0,1, length=10)),
0.5), ybreaks = q, ycolorbar=TRUE, text = paste0("Original
Image Minus N4", "\n Bias-Corrected Image"))
library(ggplot2)
library(reshape2)
slices = c(2, 6, 10, 14, 18)
vals = lapply(slices, function(x) {
cbind(img = c(nim[,,x]), fast = c(fast_img[,,x]),
slice = x)
})
vals = do.call("rbind", vals)
vals = data.frame(vals)
vals = vals[ vals$img > 0 & vals$fast > 0, ]
colnames(vals)[1:2] = c("Original Value", "Bias-Corrected Value")
v = melt(vals, id.vars = "slice")
g = ggplot(aes(x = value, colour = factor(slice)), data = v) + geom_line(stat = "density") + facet_wrap(~ variable)
g = g + scale_colour_discrete(name = "Slice #")
print(g)
bet_fast = fslbet(infile=fast_img, retimg=TRUE)
## FSLDIR='/usr/share/fsl/5.0'; PATH=${FSLDIR}/bin:${PATH};export PATH FSLDIR; sh "${FSLDIR}/etc/fslconf/fsl.sh"; FSLOUTPUTTYPE=NIFTI_GZ; export FSLOUTPUTTYPE; ${FSLDIR}/bin/bet2 "/tmp/RtmpLNWQ63/file759361d0934d.nii.gz" "/tmp/RtmpLNWQ63/file759327862e44"
bet_fast_mask <- niftiarr(bet_fast, 1)
is_in_mask = bet_fast>0
bet_fast_mask[!is_in_mask]<-NA
orthographic(bet_fast)
orthographic(fast_img,bet_fast_mask)
FSL’s Brain Extraction Tool (BET) can be used for skull stripping BET: fast, robust, and popular
cog = cog(bet_fast, ceil=TRUE)
cog = paste("-c", paste(cog, collapse= " "))
bet_fast2 = fslbet(infile=fast_img,retimg=TRUE,opts=cog)
## FSLDIR='/usr/share/fsl/5.0'; PATH=${FSLDIR}/bin:${PATH};export PATH FSLDIR; sh "${FSLDIR}/etc/fslconf/fsl.sh"; FSLOUTPUTTYPE=NIFTI_GZ; export FSLOUTPUTTYPE; ${FSLDIR}/bin/bet2 "/tmp/RtmpLNWQ63/file75932fe6d9bf.nii.gz" "/tmp/RtmpLNWQ63/file759352b1849e" -c 85 136 130
orthographic(bet_fast2)
dim(bet_fast2)
## [1] 170 256 256
With FSL easy to pre-processing Neuro image and perform some task as Brain Extraction .
Full genarated file can see here : http://rpubs.com/ngocbd/Pre-Processing-Neurohacking