dafni

Brain images in Matlab (DS)

The aims of this lesson are:

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.

Reading images into matlab

check version of 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

now load and display image:

% 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)

Background: specification for makeMontage() function

The 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

Picture of montage

In small groups

%% 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. ...

Helpful commands / concepts

The following information should help tackle the problem:

Version control

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

Bonus, making montages of rendered FSL images

The 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??

rendered image

Notes (overall)

all slices

Solution:

[pre-2017b] paths, etc

Make sure that the mrTools toolbox is on the path:

addpath(genpath('/Volumes/practicals/ds1/mrTools'))

which mlrImageReadNifti
% should return a valid path!

now load and display image:

% try out  - make sure you have ; at end of line
[data hdr] = mlrImageReadNifti('path_to_3d_testfile.nii');

% then manipulate in matlab