-
Notifications
You must be signed in to change notification settings - Fork 13
RXDetector
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
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
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
The function imports the following python libraries
- os
- sys
- cv2
- numpy
- scipy
- spectral
- spicy.stats.chi2
- custom timer class
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_valueis available for future use if an image needs to be scaled. By default all images are analyzed at native resolution. -
chi_thresholdis 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_thresholdis the minimum pixel score required for the image to be flagged as anomalous.
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.
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)
-
Xis the image to be analyzed and is the only required argument.
When onlyXis used the anomaly detection algorithm computes and uses a global background mean and covariance.
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)
-
qis the percentage of scores that will be filtered out. The value used ischi_threshold. -
dfis the nbands of the image. You can usesrc_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
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.
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.
The RX Detector uses the Spectral Library.
http://www.spectralpython.net/algorithms.html