diff --git a/autocnet/matcher/mutual_information.py b/autocnet/matcher/mutual_information.py index a33a4839e58f5f09f1db51b447bef3dfbd4dff6c..a2689c3000e2a9741573acd1feb1ef96a2f20633 100644 --- a/autocnet/matcher/mutual_information.py +++ b/autocnet/matcher/mutual_information.py @@ -3,55 +3,10 @@ from math import floor import numpy as np from scipy.ndimage.measurements import center_of_mass - -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])) +from skimage.metrics import normalized_mutual_information 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 defined template @@ -82,15 +37,16 @@ def mutual_information_match(d_template, s_image, subpixel_size=3, The y offset max_corr : float - The strength of the correlation in the range [0, 4]. + The strength of the correlation corr_map : ndarray Map of corrilation coefficients when comparing the template to locations within the search area - """ - if func == None: - func = mutual_information + See Also + -------- + skimage.metrics.normalized_mutual_information : for the kwargs that can be passed to the matcher + """ image_size = s_image.shape template_size = d_template.shape diff --git a/autocnet/matcher/tests/test_mutual_information.py b/autocnet/matcher/tests/test_mutual_information.py index 1b0a00a8877f941abbd6988d97d745465e83bd73..b2c7807d20ac658cfe2304330024ad778cb309c4 100644 --- a/autocnet/matcher/tests/test_mutual_information.py +++ b/autocnet/matcher/tests/test_mutual_information.py @@ -9,17 +9,6 @@ import numpy as np 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(): d_template = np.array([[i for i in range(50, 100)] for j in range(50)]) s_image = np.ones((100, 100)) @@ -27,8 +16,8 @@ def test_mutual_information(): 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) - assert x_offset == 0.01711861257171421 + assert x_offset == 0.010530473741837909 assert y_offset == 0.0 - assert max_corr == 2.9755967600033015 + assert max_corr == 2.0 assert corr_map.shape == (51, 51) assert np.min(corr_map) >= 0.0 diff --git a/environment.yml b/environment.yml index d4553a9565148ad1cc362060e2ce30afb4866471..5dcff1d6ee51789ecd1e469c3fdb4c8723a203fc 100644 --- a/environment.yml +++ b/environment.yml @@ -34,7 +34,7 @@ dependencies: - pytest-cov - pytest-mock - richdem - - scikit-image>=0.17 + - scikit-image>=0.19 - scikit-learn - scipy<=1.2.1 - shapely