Skip to content
Snippets Groups Projects
Commit b78574a9 authored by Adam Paquette's avatar Adam Paquette Committed by acpaquette
Browse files

Update to use scikit-image 0.19 when it is released

parent 579cccd0
No related branches found
No related tags found
No related merge requests found
...@@ -3,55 +3,10 @@ from math import floor ...@@ -3,55 +3,10 @@ from math import floor
import numpy as np import numpy as np
from scipy.ndimage.measurements import center_of_mass from scipy.ndimage.measurements import center_of_mass
from skimage.metrics import normalized_mutual_information
def mutual_information(t1, t2, **kwargs):
"""
Computes the correlation coefficient between two images using a histogram
comparison (Mutual information for joint histograms). The corr_map coefficient
will be between 0 and 4
Parameters
----------
t1 : ndarray
First image to use in the histogram comparison
t2 : ndarray
Second image to use in the histogram comparison
Returns
-------
: float
Correlation coefficient computed between the two images being compared
between 0 and 4
See Also
--------
numpy.histogram2d : for the kwargs that can be passed to the comparison
"""
if np.isnan(t1).any() or np.isnan(t2).any():
print('Unable to process due to NaN values in the input data')
return
if t1.shape != t2.shape:
print('Unable compute MI. Image sizes are not identical.')
return
hgram, x_edges, y_edges = np.histogram2d(t1.ravel(),t2.ravel(), **kwargs)
# Convert bins counts to probability values
pxy = hgram / float(np.sum(hgram))
px = np.sum(pxy, axis=1) # marginal for x over y
py = np.sum(pxy, axis=0) # marginal for y over x
px_py = px[:, None] * py[None, :] # Broadcast to multiply marginals
# Now we can do the calculation using the pxy, px_py 2D arrays
nzs = pxy > 0 # Only non-zero pxy values contribute to the sum
return np.sum(pxy[nzs] * np.log(pxy[nzs] / px_py[nzs]))
def mutual_information_match(d_template, s_image, subpixel_size=3, def mutual_information_match(d_template, s_image, subpixel_size=3,
func=None, **kwargs): func=normalized_mutual_information, **kwargs):
""" """
Applys the mutual information matcher function over a search image using a Applys the mutual information matcher function over a search image using a
defined template defined template
...@@ -82,15 +37,16 @@ def mutual_information_match(d_template, s_image, subpixel_size=3, ...@@ -82,15 +37,16 @@ def mutual_information_match(d_template, s_image, subpixel_size=3,
The y offset The y offset
max_corr : float max_corr : float
The strength of the correlation in the range [0, 4]. The strength of the correlation
corr_map : ndarray corr_map : ndarray
Map of corrilation coefficients when comparing the template to Map of corrilation coefficients when comparing the template to
locations within the search area locations within the search area
"""
if func == None: See Also
func = mutual_information --------
skimage.metrics.normalized_mutual_information : for the kwargs that can be passed to the matcher
"""
image_size = s_image.shape image_size = s_image.shape
template_size = d_template.shape template_size = d_template.shape
......
...@@ -9,17 +9,6 @@ import numpy as np ...@@ -9,17 +9,6 @@ import numpy as np
from .. import mutual_information from .. import mutual_information
def test_good_mi():
test_image1 = np.array([[i for i in range(50)] for j in range(50)])
corrilation = mutual_information.mutual_information(test_image1, test_image1)
assert corrilation == pytest.approx(2.30258509299404)
def test_bad_mi():
test_image1 = np.array([[i for i in range(50)] for j in range(50)])
test_image2 = np.ones((50, 50))
corrilation = mutual_information.mutual_information(test_image1, test_image2)
assert corrilation == pytest.approx(0)
def test_mutual_information(): def test_mutual_information():
d_template = np.array([[i for i in range(50, 100)] for j in range(50)]) d_template = np.array([[i for i in range(50, 100)] for j in range(50)])
s_image = np.ones((100, 100)) s_image = np.ones((100, 100))
...@@ -27,8 +16,8 @@ def test_mutual_information(): ...@@ -27,8 +16,8 @@ def test_mutual_information():
s_image[25:75, 25:75] = d_template s_image[25:75, 25:75] = d_template
x_offset, y_offset, max_corr, corr_map = mutual_information.mutual_information_match(d_template, s_image, bins=20) x_offset, y_offset, max_corr, corr_map = mutual_information.mutual_information_match(d_template, s_image, bins=20)
assert x_offset == 0.01711861257171421 assert x_offset == 0.010530473741837909
assert y_offset == 0.0 assert y_offset == 0.0
assert max_corr == 2.9755967600033015 assert max_corr == 2.0
assert corr_map.shape == (51, 51) assert corr_map.shape == (51, 51)
assert np.min(corr_map) >= 0.0 assert np.min(corr_map) >= 0.0
...@@ -34,7 +34,7 @@ dependencies: ...@@ -34,7 +34,7 @@ dependencies:
- pytest-cov - pytest-cov
- pytest-mock - pytest-mock
- richdem - richdem
- scikit-image>=0.17 - scikit-image>=0.19
- scikit-learn - scikit-learn
- scipy<=1.2.1 - scipy<=1.2.1
- shapely - shapely
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment