MINC/Tools/emma/emma-demo1

= Basic EMMA Image Manipulation = EMMA (Extensible Matlab Medical Analysis) is a general purpose toolkit of MATLAB functions intended to help researchers with image analysis performed under MATLAB. It provides functions for reading MINC (Medical Image Net CDF) files, writing MINC files, displaying images, and performing general manipulations on images.

Reading in Images from File
Let's start with a quick demonstration. We will read in 21 frames from a dynamic PET stored in a MINC file, and display one frame from this data set. First, we must open the file. The EMMA function openimage opens the image file for reading, and returns a handle. Therefore, we call it like this:

h=openimage('/local/matlab/toolbox/emma/examples/yates_19445.mnc');

The variable h is now the handle used for dealing with this image file (its actual value is unimportant to the user). Now that the file is open, we can read it with the EMMA getimages function. We will read all 21 frames from slice 8 of the study:

PET=getimages(h,8,1:21);

The variable PET now contains the images for each frame of slice 8. We can see what variables we have and their sizes by using the MATLAB whos command: &gt;&gt; whos Name       Size       Elements     Bytes    Density   Complex PET  16384 by 21       344064   2752512       Full      No                  h       1 by 1             1         8       Full      No Grand total is 344065 elements using 2752520 bytes

The images themselves are 128 pixels by 128 pixels (16384 elements), and are stored by EMMA in column form. When the getimages function reads in the image, it concatenates the lines of the image together to form one column. Therefore, the PET variable read in previously contains 16384 rows (128x128=16384), and 21 columns (21 images). Although this may seem somewhat unwieldy at first, it allows for efficient image manipulation under MATLAB.

Viewing an Image
We may view one image from this data set by using the EMMA viewimage function. It takes an image stored in either column format like that just described, or as a matrix, and displays it in a figure window. If we want to view the 17th image in the variable <tt>PET</tt> (the seventeenth frame from the eighth slice of the study opened previously), we want to view <tt>PET(:,17)</tt>, since this will return all elements of the 17th column of the matrix PET: viewimage(PET(:,17));

This will produce an image that looks something like this:



Creating a New MINC File
We may also create a new MINC file and write images into it, using the EMMA <tt>newimage</tt> function. If we wanted to save the variable <tt>PET</tt> in a new MINC file called <tt>new.mnc</tt>, we would first create the MINC file with the <tt>newimage</tt> function:

h2 = newimage('new.mnc', [21 1 128 128]);

Like the <tt>openimage</tt> function, <tt>newimage</tt> also returns a handle to an open MINC file (this time the MINC file is open for writing). The above command creates a MINC file with 21 frames, 1 slice, and images that are 128x128. When the file is created with <tt>newimage</tt>, it is empty, and should be filled with images. This can be done with the EMMA <tt>putimages</tt> function: putimages(h2,PET,1,1:21);

This writes the 21 images in the variable PET into the MINC file (represented by <tt>h2</tt>) as slice one, frames one through twenty-one.

A More Complex Example
Let's try something a little more involved. We will take the variable <tt>PET</tt> that we already read in from disk, integrate each pixel over the frames (time), and write out the resulting image. First, we require the mid-frame times for the 21 frames loaded. We can use the EMMA <tt>getimageinfo</tt> function to get this information:

ftimes = getimageinfo(h,'MidFrameTimes');

This will create a new variable called <tt>ftimes</tt> that contains the mid-frame times for each frame. We may now integrate with the EMMA <tt>ntrapz</tt> function (which has the same functionality as the MATLAB <tt>trapz</tt> function, but performs its calculations an order of magnitude more quickly):

img = ntrapz(ftimes,PET')';

We must transpose the <tt>PET</tt> variable before passing it to the <tt>ntrapz</tt> function since <tt>ntrapz</tt> expects the second variable to have the same number of rows as the first variable (<tt>PET</tt> is 16384x21, so <tt>PET'</tt> is 21x16384, and <tt>ftimes</tt> is 21x1, satisfying the requirement). The answer must also be transposed so that <tt>img</tt> is a column vector (not a row vector) representation of the image. (Note: This example is not the most memory efficient way to calculate the integrated image. Please consult [matlab_memory.html Controlling MATLAB Memory Use] for more information.)

One might imagine a situation where we wanted to calculate the integrated image for each slice in a study, and then write the result into a new MINC file. We would want the new MINC file to have the same dimensional information as the original MINC file, except with no frames. This can be easily accomplished, since newimage allows the specification of a "parent image" from which to take the required dimensional information. So, using the original MINC file as the parent, we can create a new MINC file:

h3 = newimage('new2.mnc', [0 15], ...     '/local/matlab/toolbox/emma/examples/yates_19445.mnc');

This new file contains no frames, fifteen slices, and the same spacing as the parent image. We could then write our integrated image into this new MINC file using the <tt>putimages</tt> function: putimages(h3,img,8);

This writes the data from the variable <tt>img</tt> into the MINC file represented by the handle <tt>h3</tt> as slice 8 (remember that we originally read the data from slice 8). In order to write data for every slice into the new MINC file, we would need to repeat the above procedure (read, integrate, write result) for each slice.