def increment_mccf(A, B, X, y, nu=0.125, l=0.01, boundary='constant'): r""" Incremental Multi-Channel Correlation Filter (MCCF) """ # number of images; number of channels, height and width n, k, hx, wx = X.shape x_shape = (hx, wx) # height and width of desired responses _, hy, wy = y.shape y_shape = (hy, wy) # extended shape ext_h = hx + hy - 1 ext_w = wx + wy - 1 ext_shape = (ext_h, ext_w) # extended dimensionality ext_d = ext_h * ext_w # extend desired response ext_y = pad(y, ext_shape) # fft of extended desired response fft_ext_y = fft2(ext_y) # auto and cross spectral energy matrices sXX = 0 sXY = 0 # for each training image and desired response for x in X: # extend image ext_x = pad(x, ext_shape, boundary=boundary) # fft of extended image fft_ext_x = fft2(ext_x) # store extended image fft as sparse diagonal matrix diag_fft_x = spdiags(fft_ext_x.reshape((k, -1)), -np.arange(0, k) * ext_d, ext_d * k, ext_d).T # vectorize extended desired response fft diag_fft_y = fft_ext_y.ravel() # update auto and cross spectral energy matrices sXX += diag_fft_x.conj().T.dot(diag_fft_x) sXY += diag_fft_x.conj().T.dot(diag_fft_y) # combine old and new auto and cross spectral energy matrices sXY = (1 - nu) * A + nu * sXY sXX = (1 - nu) * B + nu * sXX # solve ext_d independent k x k linear systems (with regularization) # to obtain desired extended multi-channel correlation filter fft_ext_f = spsolve(sXX + l * speye(sXX.shape[-1]), sXY) # reshape extended filter to extended image shape fft_ext_f = fft_ext_f.reshape((k, ext_h, ext_w)) # compute filter inverse fft ext_f = np.real(ifftshift(ifft2(fft_ext_f), axes=(-2, -1))) # crop extended filter to match desired response shape f = crop(ext_f, y_shape) return f, sXY, sXX
def learn_kcf(x, y, kernel_correlation=gaussian_correlation, l=0.01, boundary='constant', **kwargs): r""" Kernelized Correlation Filter (KCF). Parameters ---------- x : ``(channels, height, width)`` `ndarray` Template image. y : ``(1, height, width)`` `ndarray` Desired response. kernel_correlation: `callable`, optional Callable implementing a particular type of kernel correlation i.e. gaussian, polynomial or linear. l: `float`, optional Regularization parameter. boundary: str {`constant`, `symmetric`}, optional Determines how the image is padded. Returns ------- alpha: ``(channels, height, width)`` `ndarray` Kernelized Correlation Filter (KFC) associated to the template image. References ---------- .. [1] J. F. Henriques, R. Caseiro, P. Martins, J. Batista. "High-Speed Tracking with Kernelized Correlation Filters". TPAMI, 2015. """ # extended shape x_shape = np.asarray(x.shape[-2:]) y_shape = np.asarray(y.shape[-2:]) ext_shape = x_shape + y_shape - 1 # extend desired response ext_x = pad(x, ext_shape, boundary=boundary) ext_y = pad(y, ext_shape) # ffts of extended auto kernel correlation and extended desired response fft_ext_kxx = fft2(kernel_correlation(ext_x, ext_x, **kwargs)) fft_ext_y = fft2(ext_y) # extended kernelized correlation filter ext_alpha = np.real(ifftshift(ifft2(fft_ext_y / (fft_ext_kxx + l)), axes=(-2, -1))) return crop(ext_alpha, y_shape), crop(x, y_shape)
def increment_mosse(A, B, X, y, nu=0.5, l=0.01, boundary='constant'): r""" Incremental Minimum Output Sum od Squared Errors (iMOSSE) filter """ # number of images, number of channels, height and width n, k, hx, wx = X.shape x_shape = (hx, wx) # height and width of desired responses _, hy, wy = y.shape y_shape = (hy, wy) # extended shape ext_h = hx + hy - 1 ext_w = wx + wy - 1 ext_shape = (ext_h, ext_w) # extend desired response ext_y = pad(y, ext_shape) # fft of extended desired response fft_ext_y = fft2(ext_y) # auto and cross spectral energy matrices sXX = 0 sXY = 0 # for each training image and desired response for x in X: # extend image ext_x = pad(x, ext_shape, boundary=boundary) # fft of extended image fft_ext_x = fft2(ext_x) # update auto and cross spectral energy matrices sXX += fft_ext_x.conj() * fft_ext_x sXY += fft_ext_x.conj() * fft_ext_y # combine old and new auto and cross spectral energy matrices sXY = (1 - nu) * A + nu * sXY sXX = (1 - nu) * B + nu * sXX # compute desired correlation filter fft_ext_f = sXY / (sXX + l) # reshape extended filter to extended image shape fft_ext_f = fft_ext_f.reshape((k, ext_h, ext_w)) # compute filter inverse fft ext_f = np.real(ifftshift(ifft2(fft_ext_f), axes=(-2, -1))) # crop extended filter to match desired response shape f = crop(ext_f, y_shape) return f, sXY, sXX
def learn_mosse(X, y, l=0.01, boundary='constant'): r""" Minimum Output Sum od Squared Errors (MOSSE) filter. Parameters ---------- X : ``(n_images, n_channels, height, width)`` `ndarray` Training images. y : ``(1, height, width)`` `ndarray` Desired response. l: `float`, optional Regularization parameter. boundary: str {`constant`, `symmetric`}, optional Determines how the image is padded. Returns ------- mosse: ``(1, height, width)`` `ndarray` Minimum Output Sum od Squared Errors (MOSSE) filter associated to the training images. References ---------- .. [1] David S. Bolme, J. Ross Beveridge, Bruce A. Draper and Yui Man Lui. "High-Speed Tracking with Kernelized Correlation Filters". CVPR, 2010. """ # number of images, number of channels, height and width n, k, hx, wx = X.shape # height and width of desired responses _, hy, wy = y.shape y_shape = (hy, wy) # extended shape ext_h = hx + hy - 1 ext_w = wx + wy - 1 ext_shape = (ext_h, ext_w) # extend desired response ext_y = pad(y, ext_shape) # fft of extended desired response fft_ext_y = fft2(ext_y) # auto and cross spectral energy matrices sXX = 0 sXY = 0 # for each training image and desired response for x in X: # extend image ext_x = pad(x, ext_shape, boundary=boundary) # fft of extended image fft_ext_x = fft2(ext_x) # update auto and cross spectral energy matrices sXX += fft_ext_x.conj() * fft_ext_x sXY += fft_ext_x.conj() * fft_ext_y # compute desired correlation filter fft_ext_f = sXY / (sXX + l) # reshape extended filter to extended image shape fft_ext_f = fft_ext_f.reshape((k, ext_h, ext_w)) # compute filter inverse fft ext_f = np.real(ifftshift(ifft2(fft_ext_f), axes=(-2, -1))) # crop extended filter to match desired response shape f = crop(ext_f, y_shape) return f, sXY, sXX
def learn_mccf(X, y, l=0.01, boundary='constant'): r""" Multi-Channel Correlation Filter (MCCF). Parameters ---------- X : ``(n_images, n_channels, height, width)`` `ndarray` Training images. y : ``(1, height, width)`` `ndarray` Desired response. l: `float`, optional Regularization parameter. boundary: str {`constant`, `symmetric`}, optional Determines how the image is padded. Returns ------- mccf: ``(1, height, width)`` `ndarray` Multi-Channel Correlation Filter (MCCF) filter associated to the training images. References ---------- .. [1] Hamed Kiani Galoogahi, Terence Sim, Simon Lucey. "Multi-Channel Correlation Filters". ICCV, 2013. """ # number of images; number of channels, height and width n, k, hx, wx = X.shape # height and width of desired responses _, hy, wy = y.shape y_shape = (hy, wy) # extended shape ext_h = hx + hy - 1 ext_w = wx + wy - 1 ext_shape = (ext_h, ext_w) # extended dimensionality ext_d = ext_h * ext_w # extend desired response ext_y = pad(y, ext_shape) # fft of extended desired response fft_ext_y = fft2(ext_y) # auto and cross spectral energy matrices sXX = 0 sXY = 0 # for each training image and desired response for x in X: # extend image ext_x = pad(x, ext_shape, boundary=boundary) # fft of extended image fft_ext_x = fft2(ext_x) # store extended image fft as sparse diagonal matrix diag_fft_x = spdiags(fft_ext_x.reshape((k, -1)), -np.arange(0, k) * ext_d, ext_d * k, ext_d).T # vectorize extended desired response fft diag_fft_y = fft_ext_y.ravel() # update auto and cross spectral energy matrices sXX += diag_fft_x.conj().T.dot(diag_fft_x) sXY += diag_fft_x.conj().T.dot(diag_fft_y) # solve ext_d independent k x k linear systems (with regularization) # to obtain desired extended multi-channel correlation filter fft_ext_f = spsolve(sXX + l * speye(sXX.shape[-1]), sXY) # reshape extended filter to extended image shape fft_ext_f = fft_ext_f.reshape((k, ext_h, ext_w)) # compute filter inverse fft ext_f = np.real(ifftshift(ifft2(fft_ext_f), axes=(-2, -1))) # crop extended filter to match desired response shape f = crop(ext_f, y_shape) return f, sXY, sXX