提取二维图像的复杂相位

问题描述

我正在用 python 对周期性图像进行一些图像处理。我需要分析具有相当明确的周期性的真实图像。这个项目的长期目标是让我的代码检测图像中的微小扭曲并修复这些扭曲。为此,我需要图像中周期性的相位信息。

我需要通过将原始真实图像乘以围绕图像 FFT 中的峰值振荡的复指数来实现,对应于一个特定的周期。然后使用适当的 sigma 值应用低通滤波,其中 sigma 是用于平滑的高斯掩模的半径。滤波图像的绝对值应该让我得到那个特定周期的振幅,而 np.angle(filteredimage) 或 np.arctan2(imaginary/real) 应该让我得到与那个特定周期相对应的相位。

我的代码可以很好地找到幅度,但不适用于相位。我已经被困在这个问题上好几个星期了,我不知道为什么它不能正常工作。

以下是我针对特定示例的最新代码在这种情况下,Zraw 只是平面波的 (256 x 256) 2D 图像。在此 image 中,我在此示例中使用的 Zraw 与 FFT 一起绘制。我根据最右边的 FFT 峰值选择了 v1。

from scipy.ndimage import gaussian_filter


v1 = np.array([145,128])   # Pixel position of a peak in Zraw's FFT
x0,y0 = 151,130          # Pixel position of a maxima in the original image
sigma = 10                 # Radius in pixels of gaussian mask

N = len(Zraw[0])           # Get the number of pixels of the image (NxN)
ctr = np.floor(N/2) + 1    # Get the center pixel position


# Reciprocal lattice vectors
q1 = ((v1 - np.array([ctr,ctr])) * 2* np.pi) / N

# Create meshgrid 
x = np.linspace(0,N,N)
[X,Y] = np.meshgrid(x,x)

# Create complex image --  multiply real Zraw image by complex exponential
Z1 = Zraw * np.exp(1j*(q1[0]*(X-x0) + q1[1]*(Y-y0)))

# Low pass gaussian filtering
# Filter real and imaginary parts separately because complex inputs not allowed in scipy's gaussian_filter function
Z1_real = gaussian_filter(np.real(Z1),sigma)
Z1_imag = gaussian_filter(np.imag(Z1),sigma)

# Combine filtered real & imag parts to get back a complex image
Z1_filt = Z1_real + 1j*Z1_imag


# Amplitudes
Z1_amplitudes = np.abs(Z1_filt)

# Phase
Z1_phase = np.angle(Z1_filt)

我真的需要帮助弄清楚为什么我的代码不能正常工作 - 它没有提取正确的阶段。 Here's an example of my code's output using sigma=10。我知道我得到的图像很大程度上取决于我为 sigma 选择的值、平滑半径,但我发现无论我使用什么 sigma,我都没有得到正确的相位图像。

我尝试了许多在网上找到的不同技术,但我看不出我到底哪里出错了。之前我也试过直接在傅立叶空间做低通滤波:

    fftZ1 = npf.fftshift(npf.fft2(Z1))
    mask = np.zeros((N,N))
    mask += np.exp(-((X-ctr)**2 + (Y-ctr)**2)/(2*sigma**2))
    
    fftZ1_filt = mask * fftZ1
    
    Z1_filt = npf.ifft2(npf.ifftshift(fftZ1_filt))

    Z1_amplitudes = np.abs(Z1_filt)
    Z1_phase = np.angle(Z1_filt)

这给了我非常相似的相位结果,但我发现我的另一种方法在检索正确的幅度值方面效果更好,所以我一直坚持这样做。

我不知道为什么这个阶段看起来像那样,我已经查看了谷歌和堆栈溢出,尝试了其他人针对类似问题提出的建议,但我无法弄清楚我的情况。 很抱歉这篇很长的帖子,我想让一切都清楚。我对图像处理和傅立叶的东西还很陌生,所以非常感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)