Skip to content

RXDetector

Travis Stewart edited this page Aug 30, 2018 · 4 revisions

Description

This program defines the function RXD(image_file, Params) which implements the RX spectral detection algorithm as described in the following paper:

Spectral Anomaly Detection with Machine Learning for Wilderness Search and Rescue
Julia Proft, Jesus Suarez, Robin Murphy

Input

The function RXD(image_file, Params) takes in image_file and Params.

image_file is the path to the image to be analyzed, or an OpenCV image object (Numpy matrix). Python OpenCV uses a Numpy matrix to store images.

Params contains the parameters from the parameters.ini file that are used by the RX detection algorithm. These are as follows:

  • RxChiThreshold
  • RxThreshold

Output

This function will output the following in this order:

  • The name of the original file
  • An image (matrix) containing the raw pixel scores
  • The time taken to analyze the image
  • The stats of the analysis, which is a percentage of pixel scores that meet the anomaly threshold

Imports

The function imports the following python libraries

  • os
  • sys
  • cv2
  • numpy
  • scipy
  • spectral
  • spicy.stats.chi2
  • custom timer class

Global Variables

The following code block is all the global variables in the function

scale_value = 1.0
chi_threshold = Params["RxChiThreshold"]
anomaly_threshold = Params["RxThreshold"]
  • scale_value is available for future use if an image needs to be scaled. By default all images are analyzed at native resolution.
  • chi_threshold is the percentage value of pixel scores that will be filtered out by the Percent Point Function. By default the value is 0.999, which removes 99.9% of background noise from the matrix of pixel scores. This leaves the top 0.1% of pixel scores and sets all other pixel scores to 0.
  • anomaly_threshold is the minimum pixel score required for the image to be flagged as anomalous.

Setup

The function initially creates and starts a timer to record the analyze time of the image. The image is then read in from image_file using OpenCV's imread function and is stored as src_img.

The file name of the image is extracted and stored as result_name for later use when returning the results back to analyze.py.

The image would then be scaled based on the scale_value. Currently, all images are handled at native resolutions and are not scaled.

RX Scores

Using the Spectral library the raw scores for each pixel are calculated and stored in rx_scores. To compute the pixel values the Spectral RX Anomaly Detector function is called

rx(X, background=None, window=None, cov=None)
  • X is the image to be analyzed and is the only required argument.
    When only X is used the anomaly detection algorithm computes and uses a global background mean and covariance.

Filtering Background Noise

Background noise is removed using the chi-square percent point function in the SciPy Statistics Chi2 library.

scipy.stats.chi2.ppf(q, df, loc=0, scale=1)
  • q is the percentage of scores that will be filtered out. The value used is chi_threshold.
  • df is the nbands of the image. You can use src_img.shape[-1] to get this value.

The value from the percent point function is stored in rx_chi.

rx_chi = chi2.ppf( chi_threshold, src_img.shape[-1])

A mask is created using the returned value from the percent point function. The mask has a value of 1 for every element of the rx_scores matrix whose value is greater than the value of rx_chi. All other elements are set to 0. The mask matrix is stored in rx_mask.

rx_mask = (1 * (rx_scores > rx_chi))

Once rx_mask has been created, it is applied by multiplying rx_scores and rx_mask. The resulting matrix is stored back into rx_mask.

rx_mask = rx_mask * rx_scores

Generating Statistics

The anomaly statistics are generated by taking the sum of all pixel values greater than or equal to the anomaly_threshold and dividing it by the total number of pixel. The value represents the percentage of anomalous pixels in the image and is stored in stats.

Flagging the Image

The image is flagged as containing anomalous pixel if there exists a pixel whose value is greater than or equal to the anomaly_threshold value.

References

The RX Detector uses the Spectral Library.
http://www.spectralpython.net/algorithms.html

Clone this wiki locally