Loading autocnet/matcher/mutual_information.py +36 −40 Original line number Diff line number Diff line from math import floor from autocnet.transformation.roi import Roi import numpy as np from scipy.ndimage.measurements import center_of_mass from skimage.transform import AffineTransform def mutual_information(t1, t2, **kwargs): def mutual_information(reference_arr, moving_arr, affine=AffineTransform(), **kwargs): """ Computes the correlation coefficient between two images using a histogram comparison (Mutual information for joint histograms). The corr_map coefficient Loading @@ -13,12 +14,13 @@ def mutual_information(t1, t2, **kwargs): Parameters ---------- t1 : ndarray reference_arr : ndarray First image to use in the histogram comparison t2 : ndarray moving_arr : ndarray Second image to use in the histogram comparison Returns ------- Loading @@ -31,15 +33,16 @@ def mutual_information(t1, t2, **kwargs): numpy.histogram2d : for the kwargs that can be passed to the comparison """ if np.isnan(t1).any() or np.isnan(t2).any(): if np.isnan(reference_arr.data).any() or np.isnan(moving_arr.data).any(): print('Unable to process due to NaN values in the input data') return if t1.shape != t2.shape: if reference_arr.shape != moving_arr.shape: print('Unable compute MI. Image sizes are not identical.') return hgram, x_edges, y_edges = np.histogram2d(t1.ravel(),t2.ravel(), **kwargs) hgram, x_edges, y_edges = np.histogram2d(reference_arr.ravel(),moving_arr.ravel(), **kwargs) # Convert bins counts to probability values pxy = hgram / float(np.sum(hgram)) Loading @@ -50,7 +53,10 @@ def mutual_information(t1, t2, **kwargs): 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, # TODO # need's to take in a ROI and not ndarray's # and use one clip (to pass arr later on?) def mutual_information_match(moving_roi, reference_roi, subpixel_size=3, func=None, **kwargs): """ Applys the mutual information matcher function over a search image using a Loading @@ -59,11 +65,11 @@ def mutual_information_match(d_template, s_image, subpixel_size=3, Parameters ---------- d_template : ndarray moving_roi : roi The input search template used to 'query' the destination image s_image : ndarray reference_roi : roi The image or sub-image to be searched subpixel_size : int Loading @@ -75,11 +81,8 @@ def mutual_information_match(d_template, s_image, subpixel_size=3, Returns ------- x : float The x offset y : float The y offset new_affine :AffineTransform The affine transformation max_corr : float The strength of the correlation in the range [0, 4]. Loading @@ -88,54 +91,47 @@ def mutual_information_match(d_template, s_image, subpixel_size=3, Map of corrilation coefficients when comparing the template to locations within the search area """ reference_template = reference_roi.clip() moving_image = moving_roi.clip() if func == None: func = mutual_information image_size = s_image.shape template_size = d_template.shape image_size = moving_image.shape template_size = reference_template.shape y_diff = image_size[0] - template_size[0] x_diff = image_size[1] - template_size[1] max_corr = -np.inf corr_map = np.zeros((y_diff+1, x_diff+1)) max_i = -1 # y max_j = -1 # x for i in range(y_diff+1): for j in range(x_diff+1): sub_image = s_image[i:i+template_size[1], # y sub_image = moving_image[i:i+template_size[1], # y j:j+template_size[0]] # x corr = func(sub_image, d_template, **kwargs) corr = func(sub_image, reference_template, **kwargs) if corr > max_corr: max_corr = corr max_i = i max_j = j corr_map[i, j] = corr y, x = np.unravel_index(np.argmax(corr_map, axis=None), corr_map.shape) upper = int(2 + floor(subpixel_size / 2)) lower = upper - 1 # x, y are the location of the upper left hand corner of the template in the image area = corr_map[y-lower:y+upper, x-lower:x+upper] # Compute the y, x shift (subpixel) using scipys center_of_mass function cmass = center_of_mass(area) if area.shape != (subpixel_size + 2, subpixel_size + 2): print("Max correlation is too close to the boundary.") return None, None, 0, None return None, 0, None subpixel_y_shift = subpixel_size - 1 - cmass[0] subpixel_x_shift = subpixel_size - 1 - cmass[1] y = abs(y - (corr_map.shape[1])/2) x = abs(x - (corr_map.shape[0])/2) y += subpixel_y_shift x += subpixel_x_shift # Compute the idealized shift (image center) y -= (s_image.shape[0] / 2) - (d_template.shape[0] / 2) x -= (s_image.shape[1] / 2) - (d_template.shape[1] / 2) return float(x), float(y), float(max_corr), corr_map new_affine = AffineTransform(translation=(-x, -y)) return new_affine, np.max(max_corr), corr_map No newline at end of file autocnet/matcher/tests/test_mutual_information.py +7 −4 Original line number Diff line number Diff line Loading @@ -3,7 +3,7 @@ import os import sys import unittest from unittest.mock import patch from autocnet.transformation.roi import Roi import pytest import numpy as np Loading @@ -17,6 +17,7 @@ def test_good_mi(): 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) Loading @@ -25,10 +26,12 @@ def test_mutual_information(): s_image = np.ones((100, 100)) s_image[25:75, 25:75] = d_template reference_roi = Roi(d_template, 25, 25, 25, 25, ndv=22222222) moving_roi = Roi(s_image, 50, 50, 50, 50, ndv=22222222) 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 y_offset == 0.0 affine, max_corr, corr_map = mutual_information.mutual_information_match(moving_roi, reference_roi, bins=20) assert affine.params[0][2] == -0.5171186125717124 assert affine.params[1][2] == -0.5 assert max_corr == 2.9755967600033015 assert corr_map.shape == (51, 51) assert np.min(corr_map) >= 0.0 autocnet/transformation/affine.py +1 −0 Original line number Diff line number Diff line Loading @@ -69,6 +69,7 @@ def estimate_affine_from_sensors(reference_image, lons, lats = isis.image_to_ground(reference_image.file_name, x_coords, y_coords, allowoutside=True) xs, ys = isis.ground_to_image(moving_image.file_name, lons, lats, allowoutside=True) # Check for any coords that do not project between images base_gcps = [] dst_gcps = [] Loading Loading
autocnet/matcher/mutual_information.py +36 −40 Original line number Diff line number Diff line from math import floor from autocnet.transformation.roi import Roi import numpy as np from scipy.ndimage.measurements import center_of_mass from skimage.transform import AffineTransform def mutual_information(t1, t2, **kwargs): def mutual_information(reference_arr, moving_arr, affine=AffineTransform(), **kwargs): """ Computes the correlation coefficient between two images using a histogram comparison (Mutual information for joint histograms). The corr_map coefficient Loading @@ -13,12 +14,13 @@ def mutual_information(t1, t2, **kwargs): Parameters ---------- t1 : ndarray reference_arr : ndarray First image to use in the histogram comparison t2 : ndarray moving_arr : ndarray Second image to use in the histogram comparison Returns ------- Loading @@ -31,15 +33,16 @@ def mutual_information(t1, t2, **kwargs): numpy.histogram2d : for the kwargs that can be passed to the comparison """ if np.isnan(t1).any() or np.isnan(t2).any(): if np.isnan(reference_arr.data).any() or np.isnan(moving_arr.data).any(): print('Unable to process due to NaN values in the input data') return if t1.shape != t2.shape: if reference_arr.shape != moving_arr.shape: print('Unable compute MI. Image sizes are not identical.') return hgram, x_edges, y_edges = np.histogram2d(t1.ravel(),t2.ravel(), **kwargs) hgram, x_edges, y_edges = np.histogram2d(reference_arr.ravel(),moving_arr.ravel(), **kwargs) # Convert bins counts to probability values pxy = hgram / float(np.sum(hgram)) Loading @@ -50,7 +53,10 @@ def mutual_information(t1, t2, **kwargs): 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, # TODO # need's to take in a ROI and not ndarray's # and use one clip (to pass arr later on?) def mutual_information_match(moving_roi, reference_roi, subpixel_size=3, func=None, **kwargs): """ Applys the mutual information matcher function over a search image using a Loading @@ -59,11 +65,11 @@ def mutual_information_match(d_template, s_image, subpixel_size=3, Parameters ---------- d_template : ndarray moving_roi : roi The input search template used to 'query' the destination image s_image : ndarray reference_roi : roi The image or sub-image to be searched subpixel_size : int Loading @@ -75,11 +81,8 @@ def mutual_information_match(d_template, s_image, subpixel_size=3, Returns ------- x : float The x offset y : float The y offset new_affine :AffineTransform The affine transformation max_corr : float The strength of the correlation in the range [0, 4]. Loading @@ -88,54 +91,47 @@ def mutual_information_match(d_template, s_image, subpixel_size=3, Map of corrilation coefficients when comparing the template to locations within the search area """ reference_template = reference_roi.clip() moving_image = moving_roi.clip() if func == None: func = mutual_information image_size = s_image.shape template_size = d_template.shape image_size = moving_image.shape template_size = reference_template.shape y_diff = image_size[0] - template_size[0] x_diff = image_size[1] - template_size[1] max_corr = -np.inf corr_map = np.zeros((y_diff+1, x_diff+1)) max_i = -1 # y max_j = -1 # x for i in range(y_diff+1): for j in range(x_diff+1): sub_image = s_image[i:i+template_size[1], # y sub_image = moving_image[i:i+template_size[1], # y j:j+template_size[0]] # x corr = func(sub_image, d_template, **kwargs) corr = func(sub_image, reference_template, **kwargs) if corr > max_corr: max_corr = corr max_i = i max_j = j corr_map[i, j] = corr y, x = np.unravel_index(np.argmax(corr_map, axis=None), corr_map.shape) upper = int(2 + floor(subpixel_size / 2)) lower = upper - 1 # x, y are the location of the upper left hand corner of the template in the image area = corr_map[y-lower:y+upper, x-lower:x+upper] # Compute the y, x shift (subpixel) using scipys center_of_mass function cmass = center_of_mass(area) if area.shape != (subpixel_size + 2, subpixel_size + 2): print("Max correlation is too close to the boundary.") return None, None, 0, None return None, 0, None subpixel_y_shift = subpixel_size - 1 - cmass[0] subpixel_x_shift = subpixel_size - 1 - cmass[1] y = abs(y - (corr_map.shape[1])/2) x = abs(x - (corr_map.shape[0])/2) y += subpixel_y_shift x += subpixel_x_shift # Compute the idealized shift (image center) y -= (s_image.shape[0] / 2) - (d_template.shape[0] / 2) x -= (s_image.shape[1] / 2) - (d_template.shape[1] / 2) return float(x), float(y), float(max_corr), corr_map new_affine = AffineTransform(translation=(-x, -y)) return new_affine, np.max(max_corr), corr_map No newline at end of file
autocnet/matcher/tests/test_mutual_information.py +7 −4 Original line number Diff line number Diff line Loading @@ -3,7 +3,7 @@ import os import sys import unittest from unittest.mock import patch from autocnet.transformation.roi import Roi import pytest import numpy as np Loading @@ -17,6 +17,7 @@ def test_good_mi(): 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) Loading @@ -25,10 +26,12 @@ def test_mutual_information(): s_image = np.ones((100, 100)) s_image[25:75, 25:75] = d_template reference_roi = Roi(d_template, 25, 25, 25, 25, ndv=22222222) moving_roi = Roi(s_image, 50, 50, 50, 50, ndv=22222222) 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 y_offset == 0.0 affine, max_corr, corr_map = mutual_information.mutual_information_match(moving_roi, reference_roi, bins=20) assert affine.params[0][2] == -0.5171186125717124 assert affine.params[1][2] == -0.5 assert max_corr == 2.9755967600033015 assert corr_map.shape == (51, 51) assert np.min(corr_map) >= 0.0
autocnet/transformation/affine.py +1 −0 Original line number Diff line number Diff line Loading @@ -69,6 +69,7 @@ def estimate_affine_from_sensors(reference_image, lons, lats = isis.image_to_ground(reference_image.file_name, x_coords, y_coords, allowoutside=True) xs, ys = isis.ground_to_image(moving_image.file_name, lons, lats, allowoutside=True) # Check for any coords that do not project between images base_gcps = [] dst_gcps = [] Loading