usage: unwarp trial slice EPI_dir SPGR_image.bw overlay.bw
unwarp averages all images in EPI_dir whose names are of the format trialT.SS.NNNN, where T is the trial number given as trial in the command line, SS is a two-digit representation of the slice number given by slice in the command line, and NNNN is a four-digit sequence number. Sequence numbers begin with 0001 and count upward. Echo-planar images should be in two-byte, MSB:LSB binary form.
For each horizontal scan line in the averaged echo-planar image and in SPGR_image.bw, the average of the horizontal coordinates of all pixels whose intensities are between two hardcoded threshold values is computed. These threshold values are chosen to exclude empty space and some of the skull while including most of the brain. Assuming that the head is symmetric about the vertical axis of the image, this procedure yields the horizontal coordinate of this axis. Again assuming that the vertical axis of the image matches the vertical axis of the head (i.e., the head was alligned well with the bore of the magnet), the median of all these axis coordinates across all the scan lines that contain brain pixels is a good estimate of the actual position of the vertical axis.
After this median of averages is computed, it is compared with the average horizontal coordinate of brain pixels in each scan line of the averaged echo-planar image. The result is a vector of deviations, starting at the bottom of the head and going up to the top. A three-point smoothing of the vector is iterated until the vector becomes nondecreasing. Each scan line in the echo-planar image is then shifted by the number of pixels indicated by the corresponding element of this deviation vector.
The overlay.bw file is assumed to be a thresholded correlation image warped identically to the averaged echo-planar image, and therefore correctable using the deviation vector that was generated from the averaged echo-planar image.
unwarp produces the following output files in the current directory:
Author: Matthew Belmonte