python - 如何将二维高斯从笛卡尔坐标转换为极坐标?
问题描述
我有一个二维高斯,定义为:
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 数据转换为所需的极坐标高斯?
解决方案
经过一番研究,我找到了一些将二维笛卡尔高斯变换为极坐标高斯的方法[ 1 ][ 2 ]。主要有 3 种使用不同 Python 库的方法:abel、OpenCV和Skimage。我测试了 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()
推荐阅读
- javascript - href 在第二次点击时附加到先前生成的“a”标签中
- typescript - TypeScript 在新闭包中使用时忽略可能未分配的变量
- javascript - 我如何使高度和宽度全屏
- java - 仅当 N 个文件出现在 Apache Camel 的多节点设置中时才开始处理
- javascript - Web3 未按预期返回承诺
- java - 有什么方法可以从本地计算机(本地服务器)读取数据库文件并复制到android中的资产文件夹?
- visual-studio - 无法评估扩展元数据的“Cosmos.CRTCompat.dll”。异常消息:错误的 IL 格式
- r - 如何在内存方面超越 R 的限制
- javascript - 如何验证office-fabric ui DatePicker
- laravel - 存储中文件的 Laravel URL 在浏览器中不起作用