Menu

Using Matched FIlter and RX Anomaly Detectors

Help
2013-07-24
2013-09-07
  • Tom Fitzgerald

    Tom Fitzgerald - 2013-07-24

    Can you give an example of using each of these with the test dataset? I'm confused about the implementation of each.

     
    • Thomas Boggs

      Thomas Boggs - 2013-07-25

      Sure. Both algorithms look for a target (known target for MF, unknown for RX) relative to a background so we need to compute the background statistics.

      In [1]: from spectral import *
      
      In [2]: from spectral.algorithms.detectors import RX, MatchedFilter
      
      In [3]: data = open_image('92AV3C.lan').load()
      
      In [4]: background = calc_stats(data)
      
      In [5]: rx = RX(background)
      
      In [6]: result = rx(data)
      
      In [7]: view(result)
      

      rx

      Because of the wide range of values for the RX algorithm you may want to reduce the range of the result to see more detail:

      In [8]: view(result**0.25)
      

      rx_025

      (If you notice the black stripe down at row 112 - it appears there was a scan line dropped by the sensor)

      For the matched filter algorithm, you also need to specify the target you want to filter. I'll just pick a pixel from the image (a bright spot on a building near the top left corner).

      In [9]: mf = MatchedFilter(background, data[21, 48])
      
      In [10]: mfresult = mf(data)
      
      In [11]: view(mfresult)
      

      mf

      Since the matched filter is normalized for the specified target, we should get a value of one if we pass the target pixel back to the filter:

      In [12]: mf(data[21, 48])
      Out[13]: array([ 1.00000006])
      

      Close enough. Of course, you would get the same result by looking at element (21, 48) of the mfresult array.

      One note: The doc string for RX says that the stats argument is optional (if not given, it will be calculated from the data passed to the detector). There is a bug that will throw an exception if you don't give it the statistics. I've already fixed it and it is pushed to the SPy git repository so it will also be fixed in the next SPy release, which should be in a few weeks.

      Oh, one more note: the next update will also provide an optional window argument to RX to tell it to use a sliding window for computing background statistics. It is computationally more expensive but provides better results when the entire image does not have a homogeneous background.

       

      Last edit: Thomas Boggs 2013-07-25
  • Tom Fitzgerald

    Tom Fitzgerald - 2013-07-25

    Excellent example. Thanks. Just needed to see it in action once.

    Tom

     
    • Thomas Boggs

      Thomas Boggs - 2013-09-07

      Spectral Python 0.12 was just released and it provides a more general function (called rx) for computing RX anomaly scores. This function is autmatically imported into the main spectral namespace. The RX examples given above will still work but it is recommended that you instead use the more general function.

      To compute the RX scores for an image, using all pixels in the image for background statistics:

      scores = rx(data)
      

      To compute RX scores using alternate background statistics:

      scores = rx(data, background=stats)
      

      To compute RX scores using a 21x21 pixel rolling window centered on the target pixel for background statistics (but ignoring the innermost 5x5 block of pixels):

      scores = rx(data, window=(5, 21))
      

      Note that using windowed statistics will cause the algorithm to take significantly longer since the covariance is recomputed for each pixel in the image.

      Additional details are on the web site.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.