JHU OCT Segmentation Version 2.11
2/10/2015

Andrew Lang
Johns Hopkins University
Department of Electrical and Computer Engineering
Image Analysis and Communications Lab (http://iacl.ece.jhu.edu)
http://www.nitrc.org/projects/aura_tools/

--------------------------------
** Requirements - IMPORTANT!! **
--------------------------------

All code has been tested on computers running either 64-bit Windows or 
64-bit Linux (running Fedora) and MATLAB R2011b and R2014a. It is NOT 
recommended to run the code on a 32-bit computer, due to the memory 
requirements. The total memory requirement for Spectralis data containing 
49 B-scans, 1024 A-scans, and 496 pixels per A-scan is about 5-10 GB of 
memory (see Algorithm parameters below for guidelines), depending on the 
settings used. On Cirrus data with 128 B-scans, 512 A-scans and 1024 pixels 
per A-scan, it can take up to 14 GB of memory. Doubling the size of the 
data can potentially double the memory requirement, so care must be taken. 
At a minimum, the computer should have 16 GB of memory to be able to run 
the algorithm with any set of parameters. On Windows machines, a warning 
will be issued if it looks like too much memory will be used up. The 
computer will simply become slow/unresponsive if it goes over the system 
memory (it will not crash or throw an out of memory error unfortunately). 

----------------------------------------------------------- 
** Instructions to install and run the segmentation code **
-----------------------------------------------------------

1) Unzip the code to directory of your choice
    - Note that results are able to be saved to a directory of your 
      choosing

2) Add files to MATLAB path
    - In MATLAB, navigate to the directory containing the code from the 
      'Current Folder' window. Then, right click on the 'Source' folder 
      and click 'Add to Path' -> 'Selected Folders and Subfolders'

3) Compile code, if necessary
    - Code is already pre-compiled for 64-bit Windows. It will need to be 
      compiled for other systems. It can be compiled by running the 
      script 'build.m', and should take 10-20 seconds to complete, 
      hopefully without any errors.

4) Confirm compilation
    - To confirm that the build worked, run the script 'build_test.m'. 
      Three figures should pop up, one confirming the random forest code 
      compiled, one confirming the graph cut code compiled, and one 
      confirming the Gaussian smoothing code compiled. Otherwise, an error 
      will appear in the command window.

5) Run algorithm
    - The script 'seg_script.m' contains a demonstration showing how to run 
      the algorithm. It is should be straightforward; you need to specify 
      the file name or list of file names you wish to segment, and the 
      parameters of the algorithm. The main function to do the segmentation 
      is called as 'OCTLayerSegmentation(filenames,parameters)'. There is 
      no output to the function, all relevent results are saved to a 
      directory specified by the input parameters. All parameters of the 
      algorithm must be specified, and the default/recommended options are 
      provided. Simply change the results directory and the input data to 
      begin.

----------------
** File input **
----------------

The algorithm accepts data from both Heidelberg Spectralis (filetype: .vol) 
and Zeiss Cirrus scanners (filetype: .img). Scans must be centered at the 
macula - optic nerve head scans are currently unsupported. The simplest way 
to run the algorithm is to input a text file containing a list of the file 
names to segment. The segmentation will then run on each file listed within 
the text file. See 'file_list.txt' for an example.

The scripts 'make_file_list_spectralis.m' and 'make_file_list_cirrus.m' 
will automatically generate a text file containing all of the scans within 
a given directory, searching recursively in every subfolder for '.vol' or 
'.img' files. You can then manually delete any files from the text file 
that you do not wish to process.

Alternatively, the function OCTLayerSegmentation can accept two other types 
of input for 'filename'. First, it can take a string containing the path 
to the data, i.e. 'C:\OCTData\OCT_volume.vol'. Second, it can take a cell 
array containing the path to multiple files, i.e. 
{'C:\OCTData\OCT_volume1.vol', 'C:\OCTData\OCT_volume2.vol'}.

--------------------------
** Algorithm parameters **
--------------------------

There are several different parameters that need to be set and input into 
the algorithm. Generally speaking, these parameters relate to algorithm 
specific parameters, display options, and result options. A description of 
the parameters can be found in the help for the OCTLayerSegmentation 
function. The script 'seg_script.m' also has some description of the 
parameters. All options are required to be set as input for the algorithm 
(no input checking currently).

Memory guidelines - Approximate memory consumption for using different 
parameter settings on Spectralis data with 49 B-scans, 1024 A-scans, and 
496 pixels per A-scan:
    params.segmethod = 1 -> 4-8 GB
    params.segmethod = 2 -> 10-12 GB
    params.segmethod = 3, params.minseg = false -> 4-6 GB
    params.segmethod = 3, params.minseg = true -> 1.5 GB
Note that segmentation accuracy decreases from segmethod 1 to 3

Also note that the Cirrus data has a higher memory requirement, needing 
6-16 GB depending on the parameters.

-----------------------
** Algorithm outputs **
-----------------------

Two output files are always saved for each subject processed. These results 
are saved in the '(params.resultfolder)/(vol file name)/' directory. The 
first output file is a .mat file containing the boundary points. This file 
can be used to visualize the results after completion using the OCT Viewer 
(see below). If the input data is named 'OCT_volume.vol', then the .mat 
file output will be named 

    OCT_volume_result.mat
 
The other output is a .csv file. Following the previous example, this file 
will be named

    OCT_volume_result.csv

This file contains averaged thickness measurements for each of 8 
layers over 10 different regions across the macula. The regions are those
defined by a standard ETDRS grid. The size of the grid can be changed using 
the params.gridradii option. To visualize an example of the grid, set
'params.displaygrid = true' with a single subject as input.

If 'params.saveXMLfiles = true' is set, then two XML image files will be 
produced

    OCT_volume.xml
    OCT_volume_result.xml

The first file contains the OCT intensity data, while the second contains 
the layer segmentation. There is also a corresponding '.raw' format file 
saved with each '.xml' file. These files can be read by the MIPAV software 
(http://mipav.cit.nih.gov/). 

-------------------------
** Visualizing results **
-------------------------

After completion of the algorithm, results can be visualized by calling an 
OCT visualization GUI using the command 'octViewer'. After the viewer is 
opened, go to 'File->Load volume->(Scanner type)' in the menu bar. 
After loading the volume, you may scroll through the images using the mouse 
wheel (thus a mouse with a mouse wheel is required, to be changed in the 
future...). To load the segmentation result, go to 
'Segmentation->Load file->.mat file' in the menu bar. Then, navigate to the 
results directory, choosing the .mat result file with '_result' appended to 
the volume file name.

-----------------------------------------
** Detecting problems with the results **
-----------------------------------------

It is possible for the algorithm to fail. In the case that the algorithm 
fails, no result files will be generated. It is likely that the failure is 
due to a problem with the data (an artifact of some sort). If the data 
appears to be entirely of good quality, please let me know, as this could 
be a bug in the code. The best way to find errors is to search for 'Error' 
in the log file.

There is currently only one mechanism for detecting artifacts within the 
data, which can compromise the results. When the algorithm is running (and 
in the log file), you will see an output that looks like this:

    Detecting retina boundaries...done! (289 outlier points)

If the number of outlier points is sufficiently large, a warning will be 
output, such as:

    Warning: poor fit of retina boundaries detected (1056 outlier points). 
    Check for artifacts in the data.

The threshold for the warning is set to half the number of A-scans in the 
data (so if there is 1024 A-scans, then a warning will occur at 512 
outliers). An outlier represents a poor fit of the initial retina boundary 
estimates, and could compromise the rest of the algorithm. It is therefore 
important to examine all results where a warning is output. 

-----------------------
** Algorithm details **
-----------------------

The segmentation algorithm is described with full details in the following
publication. The described RF+GC algorithm can be reproduced with using the 
setting params.segmethod = 2. Changing params.segmethod to 1 adds spatially 
varying constraints on the layer thicknesses and change in the boundary
smoothness (see Garvin et al. 2009). The described minimum parameter set 
can be also be enabled with the setting params.minseg = true. The RF+CAN 
method can be reproduced using the setting params.segmethod = 3. Finally, 
note that the results in the paper were generated without additional 
post-smoothing (params.smooth = false).

A. Lang, A. Carass, M. Hauser, E. S. Sotirchos, P. A. Calabresi,
       H. S. Ying, and J. L. Prince, "Retinal layer segmentation of
       macular OCT images using boundary classification,"
       Biomed. Opt. Express 4, 11331152 (2013).

This algorithm was validated for Spectralis data only, however the 
following conference abstract details the consistency between the results 
on both scanners:

P. Bhargava, A. Lang, O. Al-Louzi, A. Carass, S. Saidha, J. Prince, P.
       A. Calabresi. "Cross-platform comparison of retinal layers in
       multiple sclerosis utilizing a novel open-source optical coherence
       tomography automated segmentation algorithm." 2014 Cooperative
       Meeting of CMSC and ACTRIMS, Dallas, TX, May 2014.
       (http://dx.doi.org/10.7224/1537-2073-16.S3.1)

-------------
** Contact **
-------------

Please contact me at alang9@jhu.edu with any questions, bug 
reports, and feature requests.
