-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Robust matched filter #138
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from .matched_filter import normalized_matched_filter | ||
from .matched_filter import normalized_matched_filter, robust_filter_vector, robust_matched_filter |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import math | ||
|
||
import numpy as np | ||
from scipy.optimize import root_scalar | ||
|
||
from hyperspectral.math.rpca import whitening_matrix, whiten | ||
|
||
|
@@ -30,9 +31,85 @@ def normalized_matched_filter(image, target, clutter_cov, center=False): | |
|
||
if center: | ||
r,c,d = image.shape | ||
image = image - np.median(image.reshape((r * c, d)), axis=0) | ||
med = np.median(image.reshape((r * c, d)), axis=0) | ||
image = image - med | ||
target = target - med | ||
|
||
X̃ = whiten(image, white_matrix=white) | ||
s̃ = whiten(target, white_matrix=white) | ||
|
||
return np.divide(np.einsum('i,rci->rc', s̃, X̃), np.sqrt(np.einsum('rci,rci->rc',X̃, X̃)))/math.sqrt(np.inner(s̃, s̃)) | ||
|
||
|
||
def quad_form(ζ, s̃, λ, ε): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some discussion/description in a comment would be welcome here. It is not clear which if any of the references this is associated with. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like you are finding the Lagrange multiplier as in equation 13 of Manolakis, et al.? |
||
val = np.sum(np.divide(np.square(s̃), np.square(1 + ζ * λ))) - ε | ||
deriv = -2 * np.sum( | ||
np.multiply(λ, | ||
np.divide( | ||
np.square(s̃), | ||
(1 + ζ * λ) ** 3 | ||
) | ||
) | ||
) | ||
return val, deriv | ||
|
||
|
||
def robust_filter_vector(Σ, s0, ε, ζ0): | ||
assert len(s0.shape)==1, "Expecting vector-valued s0" | ||
k = len(s0) | ||
assert Σ.shape == (k, k), "Expecting compatible, square Σ" | ||
|
||
λ, Q = np.linalg.eigh(Σ) | ||
s̃ = np.matmul(Q.T, s0) | ||
result = root_scalar(quad_form, args=(s̃, λ, ε), method="newton", fprime=True, x0=ζ0) | ||
|
||
if not result.converged: | ||
raise ValueError("Iteration to find ζ failed to converge!") | ||
|
||
ζ = result.root | ||
|
||
Minv = np.linalg.inv(Σ - (1/ζ) * np.eye(k)) | ||
|
||
return np.matmul(Minv, s0) / np.inner(s0, np.matmul(np.matmul(np.matmul(Minv, Σ), Minv), s0)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Equation 16 from Manolakis, et. al. |
||
|
||
|
||
def robust_matched_filter(image, target, clutter_cov, ε=1e-6, ζ0= 1e-6, center=False): | ||
""" | ||
Apply a robust matched filter to perform a target detection on a set of spectra. | ||
|
||
Arguments: | ||
image (np.array): spectral data to perform the matched filter on; can be of | ||
shape (n, p) or (r, c, p) for p-dimensional spectra | ||
target (np.array): p-dimensional array giving the approximate target spectrum | ||
clutter_cov (np.array): p×p matrix describing the background covariance; best | ||
to derive this with a shrinkage covariance estimator such as | ||
sklearn.covariance.LedoitWolf | ||
ε (real): max distance from given target spectrum to actual target spectrum | ||
ζ (real): initial guess for RMF regularization parameter | ||
center (bool or np.array): Boolean flag to determine if data should be | ||
median-centered before applying filter, or spectrum to center signals on | ||
prior to filtering | ||
|
||
Reference: | ||
Manolakis, D., Lockwood, R., Cooley, T., & Jacobson, J. (2009, August). | ||
Hyperspectral detection algorithms: Use covariances or subspaces?. In Imaging | ||
Spectrometry XIV (Vol. 7457, p. 74570Q). International Society for Optics and | ||
Photonics. | ||
""" | ||
if isinstance(center, bool) and center: | ||
r,c,d = image.shape | ||
med = np.median(image.reshape((r * c, d)), axis=0) | ||
image = image - med | ||
target = target - med | ||
elif isinstance(center, np.ndarray): | ||
image = image - center | ||
target = target - center | ||
|
||
h = robust_filter_vector(clutter_cov, target, ε, ζ0) | ||
|
||
if len(image.shape) == 2: | ||
return np.dot(image, h) | ||
elif len(image.shape) == 3: | ||
return np.einsum('rci,i->rc', image, h) | ||
else: | ||
raise ValueError("Input image must be n×p or r×c×p numpy array") |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ jupyter==1.0.0 | |
matplotlib==3.1.2 | ||
rasterio==1.1.7 | ||
Shapely==1.7.1 | ||
sklearn==0.23.2 | ||
tqdm==4.51.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks useful.