Skip to end of metadata
Go to start of metadata

Overview

This is a sample Python script that utilizes Opticks and the Python Scripting Extension for Opticks. This script allows you to perform an RX anomaly detection on a hyperspectral image in Opticks. You can view the results as a threshold layer.

If you want to execute this script in Opticks while learning about it, see the Quick Start.

What Will I Learn?

You will learn the following about the Python scripting extension for Opticks:

  • How to get the current window.
  • How to get the raster data of a given window.
  • How to execute an Opticks plug-in from Python.
  • How to perform RX using NumPy.
  • How to push a NumPy array back to Opticks and display the result.

What Do I Need?

The Script

Let's discuss this script as two parts:

  1. Calculate the covariance matrix for the data.
  2. Calculate the RX anomaly detection using the covariance matrix.

Before you continue, let me offer the following reading tips:

  • You can type every single line of Python code you see below directly into the "Python" tab of the Scripting Window. Just make sure you have loaded the image first.
  • You should also take a look at the Quick Start if you want to be able to run this script before or while you are reading the below explanation.
Part 1. Calculate the covariance matrix for the data.

The first thing we need to do is get the primary raster element for the active view, this is accomplished with the following Python code.

We need to calculate a covariance matrix for the spectral data. This is an LxL matrix (for an image with L bands) and can be calculated by the Covariance plug-in in the Opticks core. We'll create an instance of the Covariance plug-in and execute it with the following code.

Part 2. Calculate the RX anomaly detection using the covariance matrix.

Now we have the covariance matrix, we'll calculate the RX values. We start by accessing the raster element's data as a NumPy array and calculating the mean pixel value for each band.

Now we are ready to calculate RX. We'll calculate for each pixel in the image and store the results in a NumPy array.

Now you have a threshold layer showing anomalous pixels as detected by the Classic RX algorithm.

Quick Start

  1. Download and install everything listed under "What Do I Need?"
  2. Download the Class RX script bundled into an Opticks extension and install it. See How To Install an Extension for details on installing.
  3. Start up Opticks and display the "Python Samples" toolbar if it isn't already visible.
  4. Load an image. Look under "What Do I Need?" for an ideal image to use. You can load the image by dragging and dropping the file onto the Opticks application window. Or you can load the image using the "File->Import..." menu item. You may need to remove "bad bands" from the image. Access the import options dialog and select Custom Selection in the Subset tab. Select the bad bands file. Alternately, highlight only the bands which are good in the Bands section of the Subset tab.
    Opticks v4.5.0 has a bug which prevents this code from executing properly on data which is not already in BIP interleave. When importing the data, ensure that the import options dialog has BIP interleave selected on the Data tab.
  5. Once the image is loaded, select the "Spectral/Anomaly Detection/Classic RX" from the "Spectral" toolbar to execute the Python script. This will calculate the RX anomaly detection for the image window you have currently selected.
  6. You will likely need to adjust the threshold layer in the Histogram window. Try selecting a lower limit of about 2 standard deviations.
  7. You can find the Python script in "OPTICKS_INSTALL\SupportFiles\site-packages\RX.py", assuming you installed the script as detailed in step 2.
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.