def ace(X, target, background=None, window=None, cov=None, **kwargs): r'''Returns Adaptive Coherence/Cosine Estimator (ACE) detection scores. Usage: y = ace(X, target, background) y = ace(X, target, window=<win> [, cov=<cov>]) Arguments: `X` (numpy.ndarray): For the first calling method shown, `X` can be an ndarray with shape (R, C, B) or an ndarray of shape (R * C, B). If the `background` keyword is given, it will be used for the image background statistics; otherwise, background statistics will be computed from `X`. If the `window` keyword is given, `X` must be a 3-dimensional array and background statistics will be computed for each point in the image using a local window defined by the keyword. `target` (ndarray or sequence of ndarray): If `X` has shape (R, C, B), `target` can be any of the following: A length-B ndarray. In this case, `target` specifies a single target spectrum to be detected. The return value will be an ndarray with shape (R, C). An ndarray with shape (D, B). In this case, `target` contains `D` length-B targets that define a subspace for the detector. The return value will be an ndarray with shape (R, C). A length-D sequence (e.g., list or tuple) of length-B ndarrays. In this case, the detector will be applied seperately to each of the `D` targets. This is equivalent to calling the function sequentially for each target and stacking the results but is much faster. The return value will be an ndarray with shape (R, C, D). `background` (`GaussianStats`): The Gaussian statistics for the background (e.g., the result of calling :func:`calc_stats` for an image). This argument is not required if `window` is given. `window` (2-tuple of odd integers): Must have the form (`inner`, `outer`), where the two values specify the widths (in pixels) of inner and outer windows centered about the pixel being evaulated. Both values must be odd integers. The background mean and covariance will be estimated from pixels in the outer window, excluding pixels within the inner window. For example, if (`inner`, `outer`) = (5, 21), then the number of pixels used to estimate background statistics will be :math:`21^2 - 5^2 = 416`. If this argument is given, `background` is not required (and will be ignored, if given). The window is modified near image borders, where full, centered windows cannot be created. The outer window will be shifted, as needed, to ensure that the outer window still has height and width `outer` (in this situation, the pixel being evaluated will not be at the center of the outer window). The inner window will be clipped, as needed, near image borders. For example, assume an image with 145 rows and columns. If the window used is (5, 21), then for the image pixel at (0, 0) (upper left corner), the the inner window will cover `image[:3, :3]` and the outer window will cover `image[:21, :21]`. For the pixel at (50, 1), the inner window will cover `image[48:53, :4]` and the outer window will cover `image[40:51, :21]`. `cov` (ndarray): An optional covariance to use. If this parameter is given, `cov` will be used for all matched filter calculations (background covariance will not be recomputed in each window) and only the background mean will be recomputed in each window. If the `window` argument is specified, providing `cov` will allow the result to be computed *much* faster. Keyword Arguments: `vectorize` (bool, default True): Specifies whether the function should attempt to vectorize operations. This typicall results in faster computation but will consume more memory. Returns numpy.ndarray: The return value will be the ACE scores for each input pixel. The shape of the returned array will be either (R, C) or (R, C, D), depending on the value of the `target` argument. References: Kraut S. & Scharf L.L., "The CFAR Adaptive Subspace Detector is a Scale- Invariant GLRT," IEEE Trans. Signal Processing., vol. 47 no. 9, pp. 2538-41, Sep. 1999 ''' import spectral as spy if background is not None and window is not None: raise ValueError('`background` and `window` keywords are mutually ' \ 'exclusive.') detector = ACE(target, background, **kwargs) if window is None: # Use common background statistics for all pixels if isinstance(target, np.ndarray): # Single detector score for target subspace for each pixel result = detector(X) else: # Separate score arrays for each target in target list if background is None: detector.set_background(spy.calc_stats(X)) def apply_to_target(t): detector.set_target(t) return detector(X) result = np.array([apply_to_target(t) for t in target]) if result.ndim == 3: result = result.transpose(1, 2, 0) else: # Compute local background statistics for each pixel from spectral.algorithms.spatial import map_outer_window_stats if isinstance(target, np.ndarray): # Single detector score for target subspace for each pixel def ace_wrapper(bg, x): detector.set_background(bg) return detector(x) result = map_outer_window_stats(ace_wrapper, X, window[0], window[1], dim_out=1, cov=cov) else: # Separate score arrays for each target in target list def apply_to_target(t, x): detector.set_target(t) return detector(x) def ace_wrapper(bg, x): detector.set_background(bg) return [apply_to_target(t, x) for t in target] result = map_outer_window_stats(ace_wrapper, X, window[0], window[1], dim_out=len(target), cov=cov) if result.ndim == 3: result = result.transpose(1, 2, 0) # Convert NaN values to zero result = np.nan_to_num(result) if isinstance(result, np.ndarray): return np.clip(result, 0, 1, out=result) else: return np.clip(result, 0, 1)
def rx(X, background=None, window=None, cov=None): r'''Computes RX anomaly detector scores. Usage: y = rx(X [, background=bg]) y = rx(X, window=(inner, outer) [, cov=C]) The RX anomaly detector produces a detection statistic equal to the squared Mahalanobis distance of a spectrum from a background distribution according to .. math:: y=(x-\mu_b)^T\Sigma^{-1}(x-\mu_b) where `x` is the pixel spectrum, :math:`\mu_b` is the background mean, and :math:`\Sigma` is the background covariance. Arguments: `X` (numpy.ndarray): For the first calling method shown, `X` can be an image with shape (R, C, B) or an ndarray of shape (R * C, B). If the `background` keyword is given, it will be used for the image background statistics; otherwise, background statistics will be computed from `X`. If the `window` keyword is given, `X` must be a 3-dimensional array and background statistics will be computed for each point in the image using a local window defined by the keyword. `background` (`GaussianStats`): The Gaussian statistics for the background (e.g., the result of calling :func:`calc_stats`). If no background stats are provided, they will be estimated based on data passed to the detector. `window` (2-tuple of odd integers): Must have the form (`inner`, `outer`), where the two values specify the widths (in pixels) of inner and outer windows centered about the pixel being evaulated. Both values must be odd integers. The background mean and covariance will be estimated from pixels in the outer window, excluding pixels within the inner window. For example, if (`inner`, `outer`) = (5, 21), then the number of pixels used to estimate background statistics will be :math:`21^2 - 5^2 = 416`. The window are modified near image borders, where full, centered windows cannot be created. The outer window will be shifted, as needed, to ensure that the outer window still has height and width `outer` (in this situation, the pixel being evaluated will not be at the center of the outer window). The inner window will be clipped, as needed, near image borders. For example, assume an image with 145 rows and columns. If the window used is (5, 21), then for the image pixel at (0, 0) (upper left corner), the the inner window will cover `image[:3, :3]` and the outer window will cover `image[:21, :21]`. For the pixel at (50, 1), the inner window will cover `image[48:53, :4]` and the outer window will cover `image[40:51, :21]`. `cov` (ndarray): An optional covariance to use. If this parameter is given, `cov` will be used for all RX calculations (background covariance will not be recomputed in each window) and only the background mean will be recomputed in each window. Returns numpy.ndarray: The return value will be the RX detector score (squared Mahalanobis distance) for each pixel given. If `X` has shape (R, C, B), the returned ndarray will have shape (R, C).. References: Reed, I.S. and Yu, X., "Adaptive multiple-band CFAR detection of an optical pattern with unknown spectral distribution," IEEE Trans. Acoust., Speech, Signal Processing, vol. 38, pp. 1760-1770, Oct. 1990. ''' if background is not None and window is not None: raise ValueError('`background` and `window` keywords are mutually ' \ 'exclusive.') if window is not None: from .spatial import map_outer_window_stats rx = RX() def rx_wrapper(bg, x): rx.set_background(bg) return rx(x) return map_outer_window_stats(rx_wrapper, X, window[0], window[1], dim_out=1, cov=cov) else: return RX(background)(X)
def matched_filter(X, target, background=None, window=None, cov=None): r'''Computes a linear matched filter target detector score. Usage: y = matched_filter(X, target, background) y = matched_filter(X, target, window=<win> [, cov=<cov>]) Given target/background means and a common covariance matrix, the matched filter response is given by: .. math:: y=\frac{(\mu_t-\mu_b)^T\Sigma^{-1}(x-\mu_b)}{(\mu_t-\mu_b)^T\Sigma^{-1}(\mu_t-\mu_b)} where :math:`\mu_t` is the target mean, :math:`\mu_b` is the background mean, and :math:`\Sigma` is the covariance. Arguments: `X` (numpy.ndarray): For the first calling method shown, `X` can be an image with shape (R, C, B) or an ndarray of shape (R * C, B). If the `background` keyword is given, it will be used for the image background statistics; otherwise, background statistics will be computed from `X`. If the `window` keyword is given, `X` must be a 3-dimensional array and background statistics will be computed for each point in the image using a local window defined by the keyword. `target` (ndarray): Length-K vector specifying the target to be detected. `background` (`GaussianStats`): The Gaussian statistics for the background (e.g., the result of calling :func:`calc_stats` for an image). This argument is not required if `window` is given. `window` (2-tuple of odd integers): Must have the form (`inner`, `outer`), where the two values specify the widths (in pixels) of inner and outer windows centered about the pixel being evaulated. Both values must be odd integers. The background mean and covariance will be estimated from pixels in the outer window, excluding pixels within the inner window. For example, if (`inner`, `outer`) = (5, 21), then the number of pixels used to estimate background statistics will be :math:`21^2 - 5^2 = 416`. If this argument is given, `background` is not required (and will be ignored, if given). The window is modified near image borders, where full, centered windows cannot be created. The outer window will be shifted, as needed, to ensure that the outer window still has height and width `outer` (in this situation, the pixel being evaluated will not be at the center of the outer window). The inner window will be clipped, as needed, near image borders. For example, assume an image with 145 rows and columns. If the window used is (5, 21), then for the image pixel at (0, 0) (upper left corner), the the inner window will cover `image[:3, :3]` and the outer window will cover `image[:21, :21]`. For the pixel at (50, 1), the inner window will cover `image[48:53, :4]` and the outer window will cover `image[40:51, :21]`. `cov` (ndarray): An optional covariance to use. If this parameter is given, `cov` will be used for all matched filter calculations (background covariance will not be recomputed in each window) and only the background mean will be recomputed in each window. If the `window` argument is specified, providing `cov` will allow the result to be computed *much* faster. Returns numpy.ndarray: The return value will be the matched filter scores distance) for each pixel given. If `X` has shape (R, C, K), the returned ndarray will have shape (R, C). ''' if background is not None and window is not None: raise ValueError('`background` and `window` are mutually ' \ 'exclusive arguments.') if window is not None: from .spatial import map_outer_window_stats def mf_wrapper(bg, x): return MatchedFilter(bg, target)(x) return map_outer_window_stats(mf_wrapper, X, window[0], window[1], dim_out=1, cov=cov) else: from spectral.algorithms.algorithms import calc_stats if background is None: background = calc_stats(X) return MatchedFilter(background, target)(X)
def ace(X, target, background=None, window=None, cov=None, **kwargs): r'''Returns Adaptive Coherence/Cosine Estimator (ACE) detection scores. Usage: y = ace(X, target, background) y = ace(X, target, window=<win> [, cov=<cov>]) Arguments: `X` (numpy.ndarray): For the first calling method shown, `X` can be an ndarray with shape (R, C, B) or an ndarray of shape (R * C, B). If the `background` keyword is given, it will be used for the image background statistics; otherwise, background statistics will be computed from `X`. If the `window` keyword is given, `X` must be a 3-dimensional array and background statistics will be computed for each point in the image using a local window defined by the keyword. `target` (ndarray or sequence of ndarray): If `X` has shape (R, C, B), `target` can be any of the following: A length-B ndarray. In this case, `target` specifies a single target spectrum to be detected. The return value will be an ndarray with shape (R, C). An ndarray with shape (D, B). In this case, `target` contains `D` length-B targets that define a subspace for the detector. The return value will be an ndarray with shape (R, C). A length-D sequence (e.g., list or tuple) of length-B ndarrays. In this case, the detector will be applied seperately to each of the `D` targets. This is equivalent to calling the function sequentially for each target and stacking the results but is much faster. The return value will be an ndarray with shape (R, C, D). `background` (`GaussianStats`): The Gaussian statistics for the background (e.g., the result of calling :func:`calc_stats` for an image). This argument is not required if `window` is given. `window` (2-tuple of odd integers): Must have the form (`inner`, `outer`), where the two values specify the widths (in pixels) of inner and outer windows centered about the pixel being evaulated. Both values must be odd integers. The background mean and covariance will be estimated from pixels in the outer window, excluding pixels within the inner window. For example, if (`inner`, `outer`) = (5, 21), then the number of pixels used to estimate background statistics will be :math:`21^2 - 5^2 = 416`. If this argument is given, `background` is not required (and will be ignored, if given). The window is modified near image borders, where full, centered windows cannot be created. The outer window will be shifted, as needed, to ensure that the outer window still has height and width `outer` (in this situation, the pixel being evaluated will not be at the center of the outer window). The inner window will be clipped, as needed, near image borders. For example, assume an image with 145 rows and columns. If the window used is (5, 21), then for the image pixel at (0, 0) (upper left corner), the the inner window will cover `image[:3, :3]` and the outer window will cover `image[:21, :21]`. For the pixel at (50, 1), the inner window will cover `image[48:53, :4]` and the outer window will cover `image[40:51, :21]`. `cov` (ndarray): An optional covariance to use. If this parameter is given, `cov` will be used for all matched filter calculations (background covariance will not be recomputed in each window). Only the background mean will be recomputed in each window). If the `window` argument is specified, providing `cov` will allow the result to be computed *much* faster. Keyword Arguments: `vectorize` (bool, default True): Specifies whether the function should attempt to vectorize operations. This typicall results in faster computation but will consume more memory. Returns numpy.ndarray: The return value will be the ACE scores for each input pixel. The shape of the returned array will be either (R, C) or (R, C, D), depending on the value of the `target` argument. References: Kraut S. & Scharf L.L., "The CFAR Adaptive Subspace Detector is a Scale- Invariant GLRT," IEEE Trans. Signal Processing., vol. 47 no. 9, pp. 2538-41, Sep. 1999 ''' import spectral as spy if background is not None and window is not None: raise ValueError('`background` and `window` keywords are mutually ' \ 'exclusive.') detector = ACE(target, background, **kwargs) if window is None: # Use common background statistics for all pixels if isinstance(target, np.ndarray): # Single detector score for target subspace for each pixel result = detector(X) else: # Separate score arrays for each target in target list if background is None: detector.set_background(spy.calc_stats(X)) def apply_to_target(t): detector.set_target(t) return detector(X) result = np.array([apply_to_target(t) for t in target]) if result.ndim == 3: result = result.transpose(1, 2, 0) else: # Compute local background statistics for each pixel from spectral.algorithms.spatial import map_outer_window_stats if isinstance(target, np.ndarray): # Single detector score for target subspace for each pixel def ace_wrapper(bg, x): detector.set_background(bg) return detector(x) result = map_outer_window_stats(ace_wrapper, X, window[0], window[1], dim_out=1, cov=cov) else: # Separate score arrays for each target in target list def apply_to_target(t, x): detector.set_target(t) return detector(x) def ace_wrapper(bg, x): detector.set_background(bg) return [apply_to_target(t, x) for t in target] result = map_outer_window_stats(ace_wrapper, X, window[0], window[1], dim_out=len(target), cov=cov) if result.ndim == 3: result = result.transpose(1, 2, 0) # Convert NaN values to zero result = np.nan_to_num(result) if isinstance(result, np.ndarray): return np.clip(result, 0, 1, out=result) else: return np.clip(result, 0, 1)
def rx(X, background=None, window=None, cov=None): r'''Computes RX anomaly detector scores. Usage: y = rx(X [, background=bg]) y = rx(X, window=(inner, outer) [, cov=C]) The RX anomaly detector produces a detection statistic equal to the squared Mahalanobis distance of a spectrum from a background distribution according to .. math:: y=(x-\mu_b)^T\Sigma^{-1}(x-\mu_b) where `x` is the pixel spectrum, :math:`\mu_b` is the background mean, and :math:`\Sigma` is the background covariance. Arguments: `X` (numpy.ndarray): For the first calling method shown, `X` can be an image with shape (R, C, B) or an ndarray of shape (R * C, B). If the `background` keyword is given, it will be used for the image background statistics; otherwise, background statistics will be computed from `X`. If the `window` keyword is given, `X` must be a 3-dimensional array and background statistics will be computed for each point in the image using a local window defined by the keyword. `background` (`GaussianStats`): The Gaussian statistics for the background (e.g., the result of calling :func:`calc_stats`). If no background stats are provided, they will be estimated based on data passed to the detector. `window` (2-tuple of odd integers): Must have the form (`inner`, `outer`), where the two values specify the widths (in pixels) of inner and outer windows centered about the pixel being evaulated. Both values must be odd integers. The background mean and covariance will be estimated from pixels in the outer window, excluding pixels within the inner window. For example, if (`inner`, `outer`) = (5, 21), then the number of pixels used to estimate background statistics will be :math:`21^2 - 5^2 = 416`. The window are modified near image borders, where full, centered windows cannot be created. The outer window will be shifted, as needed, to ensure that the outer window still has height and width `outer` (in this situation, the pixel being evaluated will not be at the center of the outer window). The inner window will be clipped, as needed, near image borders. For example, assume an image with 145 rows and columns. If the window used is (5, 21), then for the image pixel at (0, 0) (upper left corner), the the inner window will cover `image[:3, :3]` and the outer window will cover `image[:21, :21]`. For the pixel at (50, 1), the inner window will cover `image[48:53, :4]` and the outer window will cover `image[40:51, :21]`. `cov` (ndarray): An optional covariance to use. If this parameter is given, `cov` will be used for all RX calculations (background covariance will not be recomputed in each window). Only the background mean will be recomputed in each window). Returns numpy.ndarray: The return value will be the RX detector score (squared Mahalanobis distance) for each pixel given. If `X` has shape (R, C, B), the returned ndarray will have shape (R, C).. References: Reed, I.S. and Yu, X., "Adaptive multiple-band CFAR detection of an optical pattern with unknown spectral distribution," IEEE Trans. Acoust., Speech, Signal Processing, vol. 38, pp. 1760-1770, Oct. 1990. ''' if background is not None and window is not None: raise ValueError('`background` and `window` keywords are mutually ' \ 'exclusive.') if window is not None: from .spatial import map_outer_window_stats rx = RX() def rx_wrapper(bg, x): rx.set_background(bg) return rx(x) return map_outer_window_stats(rx_wrapper, X, window[0], window[1], dim_out=1, cov=cov) else: return RX(background)(X)
def matched_filter(X, target, background=None, window=None, cov=None): r'''Computes a linear matched filter target detector score. Usage: y = matched_filter(X, target, background) y = matched_filter(X, target, window=<win> [, cov=<cov>]) Given target/background means and a common covariance matrix, the matched filter response is given by: .. math:: y=\frac{(\mu_t-\mu_b)^T\Sigma^{-1}(x-\mu_b)}{(\mu_t-\mu_b)^T\Sigma^{-1}(\mu_t-\mu_b)} where :math:`\mu_t` is the target mean, :math:`\mu_b` is the background mean, and :math:`\Sigma` is the covariance. Arguments: `X` (numpy.ndarray): For the first calling method shown, `X` can be an image with shape (R, C, B) or an ndarray of shape (R * C, B). If the `background` keyword is given, it will be used for the image background statistics; otherwise, background statistics will be computed from `X`. If the `window` keyword is given, `X` must be a 3-dimensional array and background statistics will be computed for each point in the image using a local window defined by the keyword. `target` (ndarray): Length-K vector specifying the target to be detected. `background` (`GaussianStats`): The Gaussian statistics for the background (e.g., the result of calling :func:`calc_stats` for an image). This argument is not required if `window` is given. `window` (2-tuple of odd integers): Must have the form (`inner`, `outer`), where the two values specify the widths (in pixels) of inner and outer windows centered about the pixel being evaulated. Both values must be odd integers. The background mean and covariance will be estimated from pixels in the outer window, excluding pixels within the inner window. For example, if (`inner`, `outer`) = (5, 21), then the number of pixels used to estimate background statistics will be :math:`21^2 - 5^2 = 416`. If this argument is given, `background` is not required (and will be ignored, if given). The window is modified near image borders, where full, centered windows cannot be created. The outer window will be shifted, as needed, to ensure that the outer window still has height and width `outer` (in this situation, the pixel being evaluated will not be at the center of the outer window). The inner window will be clipped, as needed, near image borders. For example, assume an image with 145 rows and columns. If the window used is (5, 21), then for the image pixel at (0, 0) (upper left corner), the the inner window will cover `image[:3, :3]` and the outer window will cover `image[:21, :21]`. For the pixel at (50, 1), the inner window will cover `image[48:53, :4]` and the outer window will cover `image[40:51, :21]`. `cov` (ndarray): An optional covariance to use. If this parameter is given, `cov` will be used for all matched filter calculations (background covariance will not be recomputed in each window). Only the background mean will be recomputed in each window). If the `window` argument is specified, providing `cov` will allow the result to be computed *much* faster. Returns numpy.ndarray: The return value will be the matched filter scores distance) for each pixel given. If `X` has shape (R, C, K), the returned ndarray will have shape (R, C). ''' if background is not None and window is not None: raise ValueError('`background` and `window` are mutually ' \ 'exclusive arguments.') if window is not None: from .spatial import map_outer_window_stats def mf_wrapper(bg, x): return MatchedFilter(bg, target)(x) return map_outer_window_stats(mf_wrapper, X, window[0], window[1], dim_out=1, cov=cov) else: from spectral.algorithms.algorithms import calc_stats if background is None: background = calc_stats(X) return MatchedFilter(background, target)(X)