0

I have a 2d Gaussian that is defined as:

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()

and looks like: enter image description here

and I would like to transform it to polar coordinates, so it looks like the following polar Gaussian [1]: enter image description here

The cartesian2polar transformation functions that I find on the internet transform the Cartesian (x_grid and y_grid) to rho and phi, but this does not rearrange my ngrid data as desired. How could I transform the ngrid data to the desired polar Gaussian?

Desperate Morty
  • 66
  • 2
  • 11
  • 1
    Not sure I understand your question: Why do you want to transform the n_grid values? They should remain the same?! Maybe [this](https://math.stackexchange.com/questions/49380/polar-coordinates-of-gaussian-distribution-with-non-zero-mean) post on math.stackexchange helps? Which equation describes the "polar gaussian" in the paper? – mss Jul 03 '21 at 11:31
  • sorry , maybe rearrange is a more accurate word for this case. – Desperate Morty Jul 03 '21 at 11:44

1 Answers1

0

After some research, I found some approaches to transform a 2D Cartesian Gaussian into a polar Gaussian [1][2]. There are mainly 3 approaches with different python libraries: abel, OpenCV and Skimage. I tested abel and skimage libraries. To get the curved shape, you have either to move the Gaussian away of the centre from the original image or set the centre point in the transformation functions. In this case I did the first option. I prefer the skimage function (warp_polar), because the grid resolution can be adjusted in the function with the argument "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()

enter image description here

Desperate Morty
  • 66
  • 2
  • 11