问题描述
我正在尝试使用2D FFT和傅里叶变换移位定理对表示子图像精度的2D阵列进行移位.当偏移值是整数(像素精度)时,它可以很好地工作,但是当偏移值不是整数(即像素的分数)时,我会得到很多假象.代码如下: 将numpy导入为np从scipy.fftpack导入fftfreqdef shift_fft(input_array,shift):shift_rows,shift_cols =移位nr,nc = input_array.shapeNr,Nc = fftfreq(nr),fftfreq(nc)Nc,Nr = np.meshgrid(Nc,Nr)fft_inputarray = np.fft.fft2(输入数组)fourier_shift = np.exp(1j * 2 * np.pi *(((shift_rows * Nr)+(shift_cols * Nc))))output_array = np.fft.ifft2(fft_inputarray * fourier_shift)返回np.real(output_array)
因此,shift_fft(input_array,[2,0])将起作用,但是shift_fft(input_array,[2.4,0])在没有伪像的情况下将不起作用.我做错了什么?例如,考虑具有128x128像素的Lena图像.如果我想在每个方向上移动10.4像素,则会对图像产生一些波动的调制.图像如下:
之前:
之后:
您可以尝试使用 scipy.ndimage.shift .它可以移位类似于 numpy.roll
的像素,但是还可以通过插值来实现分数移位值.
对于彩色图像,请确保第三轴(通道)的偏移量为0.
import scipy.ndimagescipy.ndimage.shift(input_array,(2.4,0))
默认情况下,它将背景设置为黑色,但是您可以调整模式以使其环绕或具有自定义颜色.
I am trying to shift a 2D array representing an image with subpixel precision using 2D FFTs and the Fourier transform shift theorem. It works well when the shift value is in an integer (pixel precision), however I get a lot of artifacts when the shift value is not an integer,ie., a fraction of a pixel.The code is below:
import numpy as np
from scipy.fftpack import fftfreq
def shift_fft(input_array,shift):
shift_rows,shift_cols = shift
nr,nc = input_array.shape
Nr, Nc = fftfreq(nr), fftfreq(nc)
Nc,Nr = np.meshgrid(Nc,Nr)
fft_inputarray = np.fft.fft2(input_array)
fourier_shift = np.exp(1j*2*np.pi*((shift_rows*Nr)+(shift_cols*Nc)))
output_array = np.fft.ifft2(fft_inputarray*fourier_shift)
return np.real(output_array)
Thus, shift_fft(input_array,[2,0]) will work, but shift_fft(input_array,[2.4,0]) will not work without artifacts. What I am doing wrong?For example, considering the image of Lena with 128x128 pixels. If I want to shift by 10.4 pixel in each direction, I get some wobbling modulation of the image.The images are the following:
Before:
After:
You can try using scipy.ndimage.shift. It shifts pixels similar to numpy.roll
, but also allows fractional shift values with interpolations.
For a colored image, make sure to provide a shift of 0 for the 3rd axis (channels).
import scipy.ndimage
scipy.ndimage.shift(input_array, (2.4, 0))
By default it'll set the background to black, but you can adjust the mode to have it wrap around or have a custom color.
这篇关于如何在python中使用超像素精度移动图像阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!