# Advanced EMMA Image Manipulation edit

This tutorial covers advanced image manipulation using MATLAB and the EMMA toolkit. It assumes that the reader already has a familiarity with EMMA and basic image manipulation.

### Creating and Using a Mask edit

Let's start by assuming that we have loaded 21 frames from one slice of a dynamic PET study into a variable called `PET`. We want to create a mask that contains 1's where the brain is, and 0's everywhere else. If this is an oxygen study, one simple approach is to integrate the image over time, and then choose values for mask pixels by using a threshold. Assuming a variable called `ftimes` that contains the mid-frame times of the frames:

img = ntrapz(ftimes,PET')';

is the integrated image. We now wish to choose pixels for the mask. MATLAB is able to perform boolean operations on a vector of values, so this is very easy:

mask = img>(mean(img)*threshold);

In this case, pixels in `mask` will be set to 1 if the value of the integrated image is greater than the mean of the entire image times some threshold. To create a mask that masks out everything except brain, we would generally choose a threshold of one (all pixels in the integrated image that have a value greater than the mean of the integrated image are chosen):

mask = img>(mean(img));

This produces an image that looks like:

This mask can be easily applied to the original images by simply multiplying the two together:

masked_frame = PET(:,17) .* mask;

This will produce a masked version of frame 17, which looks like:

### Averaging a Set of Images edit

When performing a PET study, a set of data from several subjects is usually collected. Once all of the data is collected, the data from the various separate studies must be averaged together. MATLAB allows math to be performed on images in a very straight forward way. Assuming that we have a set of images stored in variables `P1`, `P2`, `P3`, `P4`, and that we wish to average these four images, the result is simply:

average_image = (P1 P2 P3 P4)/4;

### Producing Time Activity Curves edit

Time activity curves are very simple to produce using MATLAB. EMMA provides a function called `maketac` that given the x and y coordinates of a point, and a set of dynamic PET images, will return a time-activity curve based on a 5 pixel square region of interest (ROI). The methods used in the `maketac` function can be extended to any size/shape ROI. One example of its use is:

viewimage(img); [x,y]=getpixel(1); tac = maketac(x,y,PET); figure; plot(ftimes,tac);

The first line displays the integrated image (calculated previously), the second line prompts the user to click on a point in the image (returning the x and y coordinates of the point), the third line creates the time activity curve and saves it in the variable `tac`, and the last two lines create a plot of the curve (remember that `ftimes` contains the mid-frame times, retrieved previously). The resulting plot might look something like this: