```{r setup, include=FALSE, message = FALSE} library(methods) knitr::opts_chunk$set(echo = TRUE, comment = "") library(ggplot2) library(ms.lesion) library(neurobase) library(extrantsr) library(scales) ``` ## Again we read in the data ```{r colon_twice, eval = FALSE} t1 = neurobase::readnii("training01_01_t1.nii.gz") ``` ```{r colon_twice_run, echo = FALSE} t1 = neurobase::readnii("../training01_01_t1.nii.gz") ``` ## Density of an Image Let's do a marginal density of the values: ```{r dens} plot(density(t1)) # large spike at 0 ``` ## Density with a mask You can also pass in a mask to most standard functions: ```{r dens_with_mask} plot(density(t1, mask = t1 > 0)) ``` ## Similarly: a Histogram Note the high intensities: ```{r histog, echo = TRUE} hist(t1) ``` ## Orthographic view with additions The `neurobase::ortho2` function displays nifti objects in 3 different planes. ```{r ortho2} neurobase::ortho2(t1) ``` ## Brightening up the image We see a dark image; a this single large value affects how values are mapped. The function `robust_window` calculates Winsorizes an image, by default the 0 (min) and 99.9th quantile, and sets values outside of this range to that quantile (97.5th below). ```{r ortho2_rob} ortho2(robust_window(t1, probs = c(0, 0.975))) ``` ## Robust Density Note the x-axis: ```{r dens_robust, echo = FALSE, fig.height = 5, fig.width = 10} par(mfrow = c(1,2)) plot(density(t1), main = "Density of T1") plot(density(robust_window(t1, probs = c(0, 0.975))), main = "Density of Windowed T1") par(mfrow = c(1,1)) ``` ## Overlaying images in `ortho2` For the rest of the slides we will use the robust `t1` for plotting ```{r robust_final, echo = FALSE} t1 = robust_window(t1, probs = c(0, 0.975)) ``` Here we plot the T1 and a mask of values over $300$: ```{r ortho_nona} ortho2(t1, y = t1 > 300) ``` ## Double orthographic view Sometimes you would like to represent 2 images side by side, of the same dimensions and orientation of course (useful for checking registration), use `double_ortho` ```{r double_ortho} double_ortho(t1, y = t1 > 300, col.y = "white") ``` ## Lightbox: view all slices The `oro.nifti::image` function shows a lightbox view, all slices of an image: ```{r all_slices} image(t1, useRaster = TRUE) # look at average brightness over each slice ``` ## Viewing specific slices The `slice` function can plot individual slices: ```{r two_slicewslice} oro.nifti::slice(t1, z = c(60, 80)) ``` ## Different Planes We can specify `z` the same way but change the `plane` to be different to get a different slice of the brain (could also do coronal): ```{r one_slice_sag} oro.nifti::slice(t1, z = 125, plane = "sagittal") ``` ## Overlaying slices We can also overlay one slice of an image upon another using the `oro.nifti::slice_overlay` function. ```{r one_slice_overlay} slice_overlay(t1, y = t1 > 300, z = 80) ``` ## Smoothing an Image (not extensively covered) If you want to do 3D Gaussian smoothing, the `extrantsr::smooth_image` is helpful: ```{r smoothed} library(extrantsr) sm_img = smooth_image(t1, sigma = 2) double_ortho(t1, sm_img) ``` ## Conclusions - `ortho2` - show orthographic images (and with overlays) - `image` - shows multiple slices of an image - `slice` - shows only specified slices - `slice_overlay` - similar to `image` but with an overlay - `double_ortho` - similar to `ortho2` but side-by-side - `robust_window` - good for setting high values to not so high ## Website http://johnmuschelli.com/imaging_in_r