# 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:
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:
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:
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 PET (the seventeenth frame from the eighth slice of the study opened previously), we want to view PET(:,17), since this will return all elements of the 17th column of the matrix PET:
This will produce an image that looks something like this:
File:MINC emma1PET.png

### Creating a New MINC File

We may also create a new MINC file and write images into it, using the EMMA newimage function. If we wanted to save the variable PET in a new MINC file called new.mnc, we would first create the MINC file with the newimage function:
Like the openimage function, newimage 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 newimage, it is empty, and should be filled with images. This can be done with the EMMA putimages function:
This writes the 21 images in the variable PET into the MINC file (represented by h2) 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 PET 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 getimageinfo function to get this information:
This will create a new variable called ftimes that contains the mid-frame times for each frame. We may now integrate with the EMMA ntrapz function (which has the same functionality as the MATLAB trapz function, but performs its calculations an order of magnitude more quickly):
We must transpose the PET variable before passing it to the ntrapz function since ntrapz expects the second variable to have the same number of rows as the first variable (PET is 16384x21, so PET' is 21x16384, and ftimes is 21x1, satisfying the requirement). The answer must also be transposed so that img 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:
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 putimages function:
This writes the data from the variable img into the MINC file represented by the handle h3 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.