xdem
xdem copied to clipboard
Add coregistration by minimization of NMAD
A nice suggestion by Simon Gascoin (@sgascoin). An example of a very simple algorithm that finds a horizontal shift between two DEMs by minimizing the NMAD. A good alternative to the Nuth & Kaab.
import cv2
import numpy as np
import scipy.optimize as optimize
def shiftDEM(Z,shiftX,shiftY):
M = np.float32([[1, 0, shiftX], [0, 1, shiftY]])
shifted = cv2.warpAffine(Z, M, (Z.shape[1], Z.shape[0]),borderValue=np.nan)
return shifted
def nmad(x):
return 1.4826*np.nanmedian(abs(x - np.nanmedian(x)))
def errNmad(params, Zslave, Zmaster):
shiftX, shiftY = params
M = np.float32([[1, 0, shiftX], [0, 1, shiftY]])
shifted = shiftDEM(Zslave,shiftX,shiftY)
return nmad(shifted-Zmaster)
initial_guess = [1, 1] # shift in pixels
result = optimize.minimize(errNmad, initial_guess, args=(Zwinter, Zsummer),method='Nelder-Mead')
if result.success:
print("[shiftX, shiftY]", result.x)
else:
raise ValueError(result.message)