The aims of this lesson are:
matlab
nifti
images (pre-2017b we needed a toolbox, now support is native) and we can use the function niftiread()
et al. See the first exercise below.makeMontage()
Matlab has powerful functions / toolboxes to make working with images and multi-dimensional arrays quite straightforward and I hope you will dig around the documentation to find out some things beyond what’s covered here.
matlab
The following check is not really necessary in the Psychology computer labs, but if you want to check on other lab machines, at another uni (or on your laptop)
ver % should be R2017b or higher
which niftiread % should return a path
% cd to a folder with a nifti file in it (like the one from last time):
% cd ~/data/subject-C/
% for the 2022/23 data
cd ~/Desktop/sub-03/
% try out - make sure you have ; at end of line
data = niftiread('mprage.nii.gz');
% then manipulate in matlab
Good commands to play around with are image
, imagesc
, … You could also look back the returnSlice()
, the function from the learningMatlab
materials I shared earlier in the year. If you haven’t written this function, we can do a quick walk-through at the front of class to make this useful little helper function.
figure
imagesc(returnSlice(data, 12, 1))
axis('image')
colormap(gray)
makeMontage()
functionThe user starts calls the function from the command line with a filename and a number that specifies how many images / tiles to display in the montage. E.g.
makeMontage('mprage.nii.gz', 25)
results in
doc images
+/- googling and looking on stackoverflow%% 0. function signature (first line of m-file)
% plus H1 line and comments
%% 1. load in data (and deal with errors)
%% 2. prepare data for visualisation step
%% 3. ...
The following information should help tackle the problem:
imagesc
, min/max
(or prctile
), colorbar
, doc images
Now that you have figured out the logic of what needs to happen. You can start adding files (make a copy!) to your github repository and adding/committing the changes. Try to
commit
ed some changes, also git push
to github.comThe images produced by FSL in our analysis are also candidates for displaying in matlab figures (for your report?). However, displaying them with the correct colors using makeMontage()
is not trivial… however simple slices are doable!
To make things a bit easier, I provide
% load a special colormap
load('renderMap.txt') % I got this from FSL and made it Matlab friendly
% load stats image
renderedImage = niftiread('rendered_thresh_zstat1.nii');
% display
figure
imagesc(returnSlice(renderedImage, 12, 3))
axis('image')
colormap(renderMap)
colorbar
% can you see what trick they used here??
permute
, reshape
and similar functions. If you get images as follows, you are nearly there… don’t hesitate to ask for help!Make sure that the mrTools
toolbox is on the path:
addpath(genpath('/Volumes/practicals/ds1/mrTools'))
which mlrImageReadNifti
% should return a valid path!
% try out - make sure you have ; at end of line
[data hdr] = mlrImageReadNifti('path_to_3d_testfile.nii');
% then manipulate in matlab