SOCR ≫ | DSPA ≫ | DSPA2 Topics ≫ |
This DSPA section Appendix.3.5 (3D Volume Rendering) is part of the DSPA Appendix on visualization of geometric and parametric surfaces. This DSPA Appendix (5) the isosurface-based volume rendering of 3D solids, such as 3D MRI brain scans.
DSPA Chapter 2 (Visualization) covers much of the necessary background. Below we provide some additional 3D/4D PET, sMRI, and fMRI volumes in .nii.gz format:
#, results="hide"}
library(plotly)
library(tidyverse)
# install.packages("brainR") ## if necessary
library(brainR)
# Test data: https://socr.umich.edu/HTML5/BrainViewer/data/TestBrain.nii.gz
brainURL <- "https://socr.umich.edu/HTML5/BrainViewer/data/TestBrain.nii.gz"
brainFile <- file.path(tempdir(), "TestBrain.nii.gz")
download.file(brainURL, dest=brainFile, quiet=TRUE)
brainVolume <- readNIfTI(brainFile, reorient=FALSE)
brainVolDims <- dim(brainVolume); brainVolDims
## [1] 181 217 181
This function plots functions that can be described in this parametric form \(w = f(x,y,z)\). This will allow us to render 3D solid as a data frame of four columns representing the flattened (unwinded) \(x,y,z, value\) location and intensity value pairs of the 3D solid volume.
# lower resolution to speed interactive 3D volume rendering
scaleDownFactor <- 2
x = seq(1, brainVolDims[1], by=scaleDownFactor)
y = seq(1, brainVolDims[2], by=scaleDownFactor)
z = seq(1, brainVolDims[3], by=scaleDownFactor)
volume_df <- expand.grid(x = x, y = y, z= z)
a = brainVolume@.Data
aDownSample <- a[x,y,z]
dim(a) <- c(brainVolDims[1]*brainVolDims[2]*brainVolDims[3])
# str(a)
normalize<-function(x){
return((x-min(x))/(max(x)-min(x)))
}
volume_df$value <- normalize(as.vector(aDownSample))
minVol <- min(volume_df$value)
maxVol <- max(volume_df$value)
minVol <- minVol + 0.1*(maxVol-minVol)
maxVol <- maxVol - 0.1*(maxVol-minVol)
colorscale_3dplot <- c("Viridis", "Cividis", "Inferno", "Magma", "Plasma",
"Turbo", "Hot", "Jet", "Rainbow", "Electric",
"YlGnBu", "RdBu", "Blues", "Greens", "Greys")
plot_ly(showscale = FALSE) %>%
# add_surface(x = x, y = y, z = z, opacity = 0.4) %>%
add_trace(type = "volume", data = volume_df,
x = ~x, y = ~y, z = ~z, value = ~value, opacity = 0.8,
#isomin = minVol, isomax = maxVol, surface = list(count = 3),
isomin = 0.1, isomax = 0.6, surface = list(count = 3),
# colorscale = list(c(minVol, maxVol), c("lightgray", "red"))) %>%
colorscale = colorscale_3dplot[1]) %>%
layout(title="Isosurface Rendering of a 3D MRI Brain") %>% hide_colorbar()