You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've observed pixel shifts of order a couple of pixels on Sentinel 2 and created the following solution which can be integrated/adapted:
fromscipy.ndimageimportshiftasnd_shiftfromskimage.registrationimportphase_cross_correlationfromscipyimportfftpackdeflow_pass_filter_fourier(image, cutoff_radius):
""" Apply a low-pass filter to an image in the Fourier domain. The function performs the Fourier transform on the input image, multiplies it with a low-pass filter mask, and then performs an inverse Fourier transform to get the filtered image. Parameters: ---------- image : np.ndarray The input 2D image to be filtered. cutoff_radius : float The cutoff radius for the low-pass filter in the Fourier domain. This value should be between 0 and 0.5, where 0.5 corresponds to the Nyquist frequency. Returns: ------- np.ndarray The filtered image in the spatial domain. Example: ------- >>> image = np.random.rand(100, 100) >>> filtered_image = low_pass_filter_fourier(image, 0.2) """# Fourier TransformF=fftpack.fftshift(fftpack.fft2(image))
# Generate low-pass filter maskrows, cols=image.shapex=np.fft.fftfreq(rows)
y=np.fft.fftfreq(cols)
x, y=np.meshgrid(x, y, indexing='ij')
radius=np.sqrt(x**2+y**2)
# Apply low-pass filterfilter_mask=np.fft.fftshift(radius<=cutoff_radius)
F_filtered=F*filter_mask# Inverse Fourier Transformimage_filtered=np.fft.ifft2(np.fft.ifftshift(F_filtered)).realreturnimage_filtereddefregister_xarrays(xarray1: xr.DataArray, xarray2: xr.DataArray) ->xr.DataArray:
"""Register xarray2 onto xarray1 using the 'red' band for registration."""array1=xarray1.sel(band='red').valuesarray2=xarray2.sel(band='red').valuesarray1=low_pass_filter_fourier(array1, 0.2)
array2=low_pass_filter_fourier(array2, 0.2)
shift, _, _=phase_cross_correlation(array1, array2, upsample_factor=100)
print(shift)
corrected_xarray2=xarray2.copy()
# Apply shift to all bandsforbandinxarray2['band'].values:
corrected_xarray2.loc[dict(band=band)] =nd_shift(xarray2.sel(band=band).values, shift[:2])
returncorrected_xarray2
The text was updated successfully, but these errors were encountered:
I've observed pixel shifts of order a couple of pixels on Sentinel 2 and created the following solution which can be integrated/adapted:
The text was updated successfully, but these errors were encountered: