首页 > 解决方案 > 如何将二维高斯从笛卡尔坐标转换为极坐标?

问题描述

我有一个二维高斯,定义为:

import numpy as np
import matplotlib.pyplot as plt

max_val, x_mean, y_mean, sig_x, sig_y, alpha = [1, 0, 0, 0.01, 0.01, 0]
xline = np.arange(-0.5, 0.5, 0.001)
yline = np.arange(-0.5, 0.5, 0.001)
x_grid, y_grid = np.meshgrid(xline, yline)  

n_grid = max_val * np.exp(-((x_grid - x_mean) ** 2 / (2 * sig_x) + (y_grid - y_mean) ** 2 / (2 * sig_y)))

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('2D view (upper view)');
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.pcolor(x_grid, y_grid, n_grid, cmap='seismic', vmin=-3, vmax=3)
plt.colorbar()
plt.show()

看起来像: 在此处输入图像描述

我想把它转换成极坐标,所以它看起来像下面的极坐标高斯[1]在此处输入图像描述

我在互联网上找到的笛卡尔变换函数将笛卡尔(x_grid 和 y_grid)变换为 rho 和 phi,但这并没有按照需要重新排列我的 ngrid 数据。如何将 ngrid 数据转换为所需的极坐标高斯?

标签: pythonnumpymatplotlib

解决方案


经过一番研究,我找到了一些将二维笛卡尔高斯变换为极坐标高斯的方法[ 1 ][ 2 ]。主要有 3 种使用不同 Python 库的方法:abelOpenCVSkimage。我测试了 abel 和 skimage 库。要获得弯曲的形状,您必须将高斯从原始图像的中心移开,或者在变换函数中设置中心点。在这种情况下,我做了第一个选项。我更喜欢 skimage 函数(warp_polar),因为可以在函数中使用参数“output_shape”调整网格分辨率。

import numpy as np
import matplotlib.pyplot as plt
import abel
from skimage.transform import warp_polar


max_val, x_mean, y_mean, sig_x, sig_y, alpha = [1, 0, 0, 0.01, 0.01, 0]
xline = np.arange(-0.5, 0.5, 0.001)
yline = np.arange(-0.5, 0.5, 0.001)
x_grid, y_grid = np.meshgrid(xline, yline)

n_grid = max_val * np.exp(-((x_grid - x_mean+0.2) ** 2 / (2 * sig_x/4) + (y_grid - y_mean) ** 2 / (2 * sig_y*4)))
n_grid_polar_abel, r_grid, theta_grid = abel.tools.polar.reproject_image_into_polar(n_grid)
n_grid_polar_skimage = warp_polar(n_grid, radius=1000, output_shape=(1000, 1000))

fig, axs = plt.subplots(1, 3, figsize=(7, 3.5))
axs[0].imshow(n_grid, aspect='auto', origin='lower')
axs[1].imshow(n_grid_polar_abel, aspect='auto', origin='lower', extent=(np.min(theta_grid), np.max(theta_grid), np.min(r_grid), np.max(r_grid)))
axs[2].imshow(n_grid_polar_skimage)

axs[0].set_title('Cartesian')
axs[0].set_xlabel('x')
axs[0].set_ylabel('y')

axs[1].set_title('n_grid_polar_abel')
axs[1].set_xlabel('Theta')
axs[1].set_ylabel('r')

axs[2].set_title('n_grid_polar_skimage')
axs[2].set_xlabel('Theta')
axs[2].set_ylabel('r')

plt.tight_layout()
plt.show()

在此处输入图像描述


推荐阅读