Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Industries Academia Support User Community Company

 

Student Center

Solution: Load MRI images into MATLAB


%1
%Load mri.mat file
load mri;
%note the variables that are now loaded into the
%workspace: D, map, siz

%2
%Display all of the frames in one figure
figure
montage(D, map)
title('MRI Slices')

figure 1

%3
%Isolate frame 5 and display in individual figure
%Movies in MATLAB are typically represented with four
%dimensions. The first and second dimensions are
%spatial (i.e. the vertical and horizontal axes as
%seen by any one image of the movie.) The third
%dimension is the color dimension with a size of one
%because it indexes into the colormap. The fourth
%dimension is temporal. Dimensions 1, 2, and 4 for
%this example are as follows:

  • Dimension 1: Front to back of head (rostral/anterior to caudal/posterior)
  • Dimension 2: Left to right of head
  • Dimension 4: Bottom to top of head (inferior to superior)

%Therefore, to isolate frame 5, we use the fourth
%dimension.
frame_5=D(:,:,:,5);
figure
imshow(frame_5, map)
title('Frame 5 of MRI data')

figure 2

%4
%Show all frames of MRI data as a movie
figure
immovie(D, map)

figure 3

%5
%Obtain sagittal slice image of brain and show as
%individual image.

%The sagittal slice is a 2-D image showing the image
%from the bottom to %top of the head
%(inferior to superior) and front to back of the head
%(rostral/anterior to caudal/posterior).
%In this example, these two dimensions are the 1st and %4th dimension as described above.
%Therefore, D is now a 128 x 128 x 1 x 27 variable. We %want to take one slice of the brain (or one frame of
%the movie) and transform it using the 1st and 4th
%dimension.

%There are two functions you can use: imtransform,
%tformarray

%Limit one dimension of 4-D data to 1 point (Here,
%randomly selecting to limit second dimension and
%isolating 60th entry)
slice=D(:,60,:,:);
size_slice=size(slice)

%MATLAB will return:
size_slice =
128   1   1   27

%slice variable is now 128x1x1x27.
%make 4-D matrix into 2-D matrix (could also use
%SQUEEZE function)

slice2=reshape(slice, [128 27]);
size_slice2=size(slice2)

%MATLAB will return:
size_slice2 =
128    27

%construct transform to change horizontal view into
%sagittal view
T=maketform('affine',[0 -2.5; 1 0; 0 0]);
slice3=imtransform(slice2, T,'cubic');

%view as individual figure
figure
imshow(slice3, map)
title('Sagittal Slice')

figure 4
Contact sales
Software evaluation for instructors
Free technical kit
E-mail this page

MATLAB & Simulink Student Version

Includes full-featured versions of both MATLAB and Simulink

From School To Industry

"The only limit to MATLAB is your imagination."
Carl Klopstein
Radar Systems Engineer

Free Code for LEGO MINDSTORMS NXT

LEGO® MINDSTORMS® NXT robotics invention system lets you create and control robots using programmable motors and sensors