def apply_shifts_dft(src_freq, shifts, diffphase, is_freq = True, border_nan = False): ''' apply shifts using inverse dft src_freq: ndarray if is_freq it is fourier transform image else original image shifts: shifts to apply diffphase: comes from the register_translation output ''' shifts = shifts[::-1] if not is_freq: src_freq = np.dstack([np.real(src_freq),np.imag(src_freq)]) src_freq = fftn(src_freq,flags=cv2.DFT_COMPLEX_OUTPUT+cv2.DFT_SCALE) src_freq = src_freq[:,:,0]+1j*src_freq[:,:,1] src_freq = np.array(src_freq, dtype=np.complex128, copy=False) nr,nc = np.shape(src_freq) Nr = ifftshift(np.arange(-np.fix(old_div(nr,2.)),np.ceil(old_div(nr,2.)))) Nc = ifftshift(np.arange(-np.fix(old_div(nc,2.)),np.ceil(old_div(nc,2.)))) Nr,Nc = np.meshgrid(Nr,Nc) Greg = src_freq*np.exp(1j*2*np.pi*(-shifts[0]*1.*Nr/nr-shifts[1]*1.*Nc/nc)) Greg = Greg.dot(np.exp(1j*diffphase)) Greg = np.dstack([np.real(Greg),np.imag(Greg)]) new_img = ifftn(Greg)[:,:,0] if border_nan: max_w,max_h,min_w,min_h=0,0,0,0 max_h,max_w = np.ceil(np.maximum((max_h,max_w),shifts)).astype(np.int) min_h,min_w = np.floor(np.minimum((min_h,min_w),shifts)).astype(np.int) new_img[:max_h,:] = np.nan if min_h < 0: new_img[min_h:,:] = np.nan new_img[:,:max_w] = np.nan if min_w < 0: new_img[:,min_w:] = np.nan return new_img
def register_translation(src_image, target_image, upsample_factor=1, space="real", shifts_lb = None, shifts_ub = None, max_shifts = (10,10), opencv=True): """ adapted from SIMA (https://github.com/losonczylab) and the scikit-image (http://scikit-image.org/) package. Unless otherwise specified by LICENSE.txt files in individual directories, all code is Copyright (C) 2011, the scikit-image team All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of skimage nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Efficient subpixel image translation registration by cross-correlation. This code gives the same precision as the FFT upsampled cross-correlation in a fraction of the computation time and with reduced memory requirements. It obtains an initial estimate of the cross-correlation peak by an FFT and then refines the shift estimation by upsampling the DFT only in a small neighborhood of that estimate by means of a matrix-multiply DFT. Parameters: ---------- src_image : ndarray Reference image. target_image : ndarray Image to register. Must be same dimensionality as ``src_image``. upsample_factor : int, optional Upsampling factor. Images will be registered to within ``1 / upsample_factor`` of a pixel. For example ``upsample_factor == 20`` means the images will be registered within 1/20th of a pixel. Default is 1 (no upsampling) space : string, one of "real" or "fourier" Defines how the algorithm interprets input data. "real" means data will be FFT'd to compute the correlation, while "fourier" data will bypass FFT of input data. Case insensitive. Returns: ------- shifts : ndarray Shift vector (in pixels) required to register ``target_image`` with ``src_image``. Axis ordering is consistent with numpy (e.g. Z, Y, X) error : float Translation invariant normalized RMS error between ``src_image`` and ``target_image``. phasediff : float Global phase difference between the two images (should be zero if images are non-negative). Raise: ------ NotImplementedError("Error: register_translation only supports " "subpixel registration for 2D images") ValueError("Error: images must really be same size for " "register_translation") ValueError("Error: register_translation only knows the \"real\" " "and \"fourier\" values for the ``space`` argument.") References: ---------- .. [1] Manuel Guizar-Sicairos, Samuel T. Thurman, and James R. Fienup, "Efficient subpixel image registration algorithms," Optics Letters 33, 156-158 (2008). """ # images must be the same shape if src_image.shape != target_image.shape: raise ValueError("Error: images must really be same size for " "register_translation") # only 2D data makes sense right now if src_image.ndim != 2 and upsample_factor > 1: raise NotImplementedError("Error: register_translation only supports " "subpixel registration for 2D images") # assume complex data is already in Fourier space if space.lower() == 'fourier': src_freq = src_image target_freq = target_image # real data needs to be fft'd. elif space.lower() == 'real': if opencv: src_freq_1 = fftn(src_image,flags=cv2.DFT_COMPLEX_OUTPUT+cv2.DFT_SCALE) src_freq = src_freq_1[:,:,0]+1j*src_freq_1[:,:,1] src_freq = np.array(src_freq, dtype=np.complex128, copy=False) target_freq_1 = fftn(target_image,flags=cv2.DFT_COMPLEX_OUTPUT+cv2.DFT_SCALE) target_freq = target_freq_1[:,:,0]+1j*target_freq_1[:,:,1] target_freq = np.array(target_freq , dtype=np.complex128, copy=False) else: src_image_cpx = np.array(src_image, dtype=np.complex128, copy=False) target_image_cpx = np.array(target_image, dtype=np.complex128, copy=False) src_freq = np.fft.fftn(src_image_cpx) target_freq = np.fft.fftn(target_image_cpx) else: raise ValueError("Error: register_translation only knows the \"real\" " "and \"fourier\" values for the ``space`` argument.") # Whole-pixel shift - Compute cross-correlation by an IFFT shape = src_freq.shape image_product = src_freq * target_freq.conj() if opencv: image_product_cv = np.dstack([np.real(image_product),np.imag(image_product)]) cross_correlation = fftn(image_product_cv,flags=cv2.DFT_INVERSE+cv2.DFT_SCALE) cross_correlation = cross_correlation[:,:,0]+1j*cross_correlation[:,:,1] else: shape = src_freq.shape image_product = src_freq * target_freq.conj() cross_correlation = np.fft.ifftn(image_product) # Locate maximum new_cross_corr = np.abs(cross_correlation) if (shifts_lb is not None) or (shifts_ub is not None): if (shifts_lb[0]<0) and (shifts_ub[0]>=0): new_cross_corr[shifts_ub[0]:shifts_lb[0],:] = 0 else: new_cross_corr[:shifts_lb[0],:] = 0 new_cross_corr[shifts_ub[0]:,:] = 0 if (shifts_lb[1]<0) and (shifts_ub[1]>=0): new_cross_corr[:,shifts_ub[1]:shifts_lb[1]] = 0 else: new_cross_corr[:,:shifts_lb[1]] = 0 new_cross_corr[:,shifts_ub[1]:] = 0 else: new_cross_corr[max_shifts[0]:-max_shifts[0],:] = 0 new_cross_corr[:,max_shifts[1]:-max_shifts[1]] = 0 maxima = np.unravel_index(np.argmax(new_cross_corr), cross_correlation.shape) midpoints = np.array([np.fix(old_div(axis_size, 2)) for axis_size in shape]) shifts = np.array(maxima, dtype=np.float64) shifts[shifts > midpoints] -= np.array(shape)[shifts > midpoints] if upsample_factor == 1: src_amp = old_div(np.sum(np.abs(src_freq) ** 2), src_freq.size) target_amp = old_div(np.sum(np.abs(target_freq) ** 2), target_freq.size) CCmax = cross_correlation.max() # If upsampling > 1, then refine estimate with matrix multiply DFT else: # Initial shift estimate in upsampled grid shifts = old_div(np.round(shifts * upsample_factor), upsample_factor) upsampled_region_size = np.ceil(upsample_factor * 1.5) # Center of output array at dftshift + 1 dftshift = np.fix(old_div(upsampled_region_size, 2.0)) upsample_factor = np.array(upsample_factor, dtype=np.float64) normalization = (src_freq.size * upsample_factor ** 2) # Matrix multiply DFT around the current shift estimate sample_region_offset = dftshift - shifts*upsample_factor cross_correlation = _upsampled_dft(image_product.conj(), upsampled_region_size, upsample_factor, sample_region_offset).conj() cross_correlation /= normalization # Locate maximum and map back to original pixel grid maxima = np.array(np.unravel_index( np.argmax(np.abs(cross_correlation)), cross_correlation.shape), dtype=np.float64) maxima -= dftshift shifts = shifts + old_div(maxima, upsample_factor) CCmax = cross_correlation.max() src_amp = _upsampled_dft(src_freq * src_freq.conj(), 1, upsample_factor)[0, 0] src_amp /= normalization target_amp = _upsampled_dft(target_freq * target_freq.conj(), 1, upsample_factor)[0, 0] target_amp /= normalization # If its only one row or column the shift along that dimension has no # effect. We set to zero. for dim in range(src_freq.ndim): if shape[dim] == 1: shifts[dim] = 0 return shifts, cross_correlation, src_freq,_compute_phasediff(CCmax)
def register_translation(src_image, target_image, upsample_factor=1, space="real", shifts_lb = None, shifts_ub = None, max_shifts = (10,10)): """ Efficient subpixel image translation registration by cross-correlation. This code gives the same precision as the FFT upsampled cross-correlation in a fraction of the computation time and with reduced memory requirements. It obtains an initial estimate of the cross-correlation peak by an FFT and then refines the shift estimation by upsampling the DFT only in a small neighborhood of that estimate by means of a matrix-multiply DFT. Parameters ---------- src_image : ndarray Reference image. target_image : ndarray Image to register. Must be same dimensionality as ``src_image``. upsample_factor : int, optional Upsampling factor. Images will be registered to within ``1 / upsample_factor`` of a pixel. For example ``upsample_factor == 20`` means the images will be registered within 1/20th of a pixel. Default is 1 (no upsampling) space : string, one of "real" or "fourier" Defines how the algorithm interprets input data. "real" means data will be FFT'd to compute the correlation, while "fourier" data will bypass FFT of input data. Case insensitive. Returns ------- shifts : ndarray Shift vector (in pixels) required to register ``target_image`` with ``src_image``. Axis ordering is consistent with numpy (e.g. Z, Y, X) error : float Translation invariant normalized RMS error between ``src_image`` and ``target_image``. phasediff : float Global phase difference between the two images (should be zero if images are non-negative). References ---------- .. [1] Manuel Guizar-Sicairos, Samuel T. Thurman, and James R. Fienup, "Efficient subpixel image registration algorithms," Optics Letters 33, 156-158 (2008). """ # images must be the same shape if src_image.shape != target_image.shape: raise ValueError("Error: images must be same size for " "register_translation") # only 2D data makes sense right now if src_image.ndim != 2 and upsample_factor > 1: raise NotImplementedError("Error: register_translation only supports " "subpixel registration for 2D images") # assume complex data is already in Fourier space if space.lower() == 'fourier': src_freq = src_image target_freq = target_image # real data needs to be fft'd. elif space.lower() == 'real': if opencv: src_freq_1 = fftn(src_image,flags=cv2.DFT_COMPLEX_OUTPUT+cv2.DFT_SCALE) src_freq = src_freq_1[:,:,0]+1j*src_freq_1[:,:,1] src_freq = np.array(src_freq, dtype=np.complex128, copy=False) target_freq_1 = fftn(target_image,flags=cv2.DFT_COMPLEX_OUTPUT+cv2.DFT_SCALE) target_freq = target_freq_1[:,:,0]+1j*target_freq_1[:,:,1] target_freq = np.array(target_freq , dtype=np.complex128, copy=False) else: src_image_cpx = np.array(src_image, dtype=np.complex128, copy=False) target_image_cpx = np.array(target_image, dtype=np.complex128, copy=False) src_freq = np.fft.fftn(src_image_cpx) target_freq = fftn(target_image_cpx) else: raise ValueError("Error: register_translation only knows the \"real\" " "and \"fourier\" values for the ``space`` argument.") # Whole-pixel shift - Compute cross-correlation by an IFFT shape = src_freq.shape image_product = src_freq * target_freq.conj() if opencv: image_product_cv = np.dstack([np.real(image_product),np.imag(image_product)]) cross_correlation = fftn(image_product_cv,flags=cv2.DFT_INVERSE+cv2.DFT_SCALE) cross_correlation = cross_correlation[:,:,0]+1j*cross_correlation[:,:,1] else: shape = src_freq.shape image_product = src_freq * target_freq.conj() cross_correlation = ifftn(image_product) # Locate maximum new_cross_corr = np.abs(cross_correlation) if (shifts_lb is not None) or (shifts_ub is not None): if (shifts_lb[0]<0) and (shifts_ub[0]>=0): new_cross_corr[shifts_ub[0]:shifts_lb[0],:] = 0 else: new_cross_corr[:shifts_lb[0],:] = 0 new_cross_corr[shifts_ub[0]:,:] = 0 if (shifts_lb[1]<0) and (shifts_ub[1]>=0): new_cross_corr[:,shifts_ub[1]:shifts_lb[1]] = 0 else: new_cross_corr[:,:shifts_lb[1]] = 0 new_cross_corr[:,shifts_ub[1]:] = 0 else: new_cross_corr[max_shifts[0]:-max_shifts[0],:] = 0 new_cross_corr[:,max_shifts[1]:-max_shifts[1]] = 0 # pl.cla() # ranges = np.percentile(np.abs(cross_correlation),[1,99.99]) # pl.subplot(1,2,1) # pl.imshow( np.abs(cross_correlation),interpolation = 'none',vmin = ranges[0],vmax = ranges[1]) # pl.pause(.1) # pl.subplot(1,2,2) # pl.imshow(new_cross_corr,interpolation = 'none',vmin = ranges[0],vmax = ranges[1]) # pl.pause(1) maxima = np.unravel_index(np.argmax(new_cross_corr), cross_correlation.shape) midpoints = np.array([np.fix(old_div(axis_size, 2)) for axis_size in shape]) shifts = np.array(maxima, dtype=np.float64) shifts[shifts > midpoints] -= np.array(shape)[shifts > midpoints] if upsample_factor == 1: src_amp = old_div(np.sum(np.abs(src_freq) ** 2), src_freq.size) target_amp = old_div(np.sum(np.abs(target_freq) ** 2), target_freq.size) CCmax = cross_correlation.max() # If upsampling > 1, then refine estimate with matrix multiply DFT else: # Initial shift estimate in upsampled grid shifts = old_div(np.round(shifts * upsample_factor), upsample_factor) upsampled_region_size = np.ceil(upsample_factor * 1.5) # Center of output array at dftshift + 1 dftshift = np.fix(old_div(upsampled_region_size, 2.0)) upsample_factor = np.array(upsample_factor, dtype=np.float64) normalization = (src_freq.size * upsample_factor ** 2) # Matrix multiply DFT around the current shift estimate sample_region_offset = dftshift - shifts*upsample_factor cross_correlation = _upsampled_dft(image_product.conj(), upsampled_region_size, upsample_factor, sample_region_offset).conj() cross_correlation /= normalization # Locate maximum and map back to original pixel grid maxima = np.array(np.unravel_index( np.argmax(np.abs(cross_correlation)), cross_correlation.shape), dtype=np.float64) maxima -= dftshift shifts = shifts + old_div(maxima, upsample_factor) CCmax = cross_correlation.max() src_amp = _upsampled_dft(src_freq * src_freq.conj(), 1, upsample_factor)[0, 0] src_amp /= normalization target_amp = _upsampled_dft(target_freq * target_freq.conj(), 1, upsample_factor)[0, 0] target_amp /= normalization # If its only one row or column the shift along that dimension has no # effect. We set to zero. for dim in range(src_freq.ndim): if shape[dim] == 1: shifts[dim] = 0 return shifts, src_freq,_compute_phasediff(CCmax) #, _compute_error(CCmax, src_amp, target_amp),\