Step 2: Volume Visualization

subplot(2,1,2) hold on load mri colormap(map); D = squeeze(D); D = smooth3(D); patch(isosurface(D,5),... 'FaceColor',[1,.75,.65],... 'EdgeColor','none',... 'FaceAlpha','0.9'); patch(isocaps(D,5),... 'FaceColor','interp',... 'EdgeColor','none'); lightangle(45,30); lighting gouraud view(35,30) axis tight daspect([1,1,.4]) title('Crafting artisanal meems for patricians in MATLAB')

Click bolded functions for documentation

Understanding this segment is a bit difficult, since most of the commands use very specific volume visualization functions to manipulate existing volume data. Although I'll try my best to walk through this segment, I highly recommend reading up on this guide made by Mathworks, which pretty much walks through this exact process step-by-step. As a matter of fact, to save on the meme turnaround time, I took the values directly from Mathworks' guide.

Step 2.1: Setting up the plotting environment

Since I want to plot the head model below the image, I ran subplot() again to specify the new position of my subplot of interest.

Unless specified otherwise, MATLAB will not automatically superimpose different plots on top of each other in a given subplot. Instead, it wipes the existing plot and create a clean view of the second plot. Fortunately, the way to specify this is not difficult. By using MATLAB's inline hold function, I can control whether I want the current subplot to retain the previous plot when plotting the second plot. I know that I need to superimpose lines on this subplot to keep the meme format consistent*, so I enable the hold functionality.

* Holding functionality is required for this demo even without the line visualization, but that's not an important detail to stress here. More on that later.

Step 2.2: Setting up the head model

2.2.0: Loading the data

One interesting thing to note here is that MATLAB comes with a set of pre-defined volumetric data, and, luckily, MRI head scan data is one of them. By simply calling "load mri," MATLAB pulls this dataset from its internal file directory and places it neatly.

The variables that are loaded when loading the MRI scan data are:

'D', a 4D array that comprises of a 3D dataset that specifies the RGB information of each image slice, which is then stacked along the fourth dimension to reconstruct a 3D spatial model of the MRI data,

'map', a colormap array that specifies how different segments of each image slice are meant to be colored, and

'siz', a 1x3 vector that specifies the effective size of the volume.

The colormap won't affect the color of the head model itself, but it WILL impact how the top slice of the MRI image is colored. The colormap specified in 'map' is entirely in grayscale, but if you wanted to pursue other coloring schemes for the top slice, I highly recommend that you read up on how colormaps work in MATLAB.

2.2.1: Processing the volumetric data

MATLAB provides all of its volume data in the form of a 4D array, but can only take 3D arrays as input values. Weird, right?

To put 'D' in a plot-friendly format, I used squeeze() to eliminate the RGB dimension and "squeeze" the slices to the third dimension. In effect, 'D' becomes a 3D array that only contains raw information about the volume in the y, x, and z dimensions respectively.

smooth3() is an optional function that makes the volumetric visualization more "smooth" in the final result. I included it to make the final volume more "head-like", but if you want your 3D models to accurately reflect the original dataset or prefer to have a more jagged look in your head model, you can skip it entirely.