Reprojecting polar to cartesian grid

前端 未结 5 1067
不思量自难忘°
不思量自难忘° 2020-12-24 14:22

I have a polar (r,theta) grid (which means that each cell is an annulus section) containing values of some physical quantity (e.g. temperature), and I would like to re-grid

5条回答
  •  醉话见心
    2020-12-24 14:50

    Are there any Python packages that can do this?

    Yes! There is now – at least one – Python package that has a function to re-map a matrix from cartesian to polar coordinates: abel.tools.polar.reproject_image_into_polar(), which is part of the PyAbel package.

    (Iñigo Hernáez Corres is correct, scipy.ndimage.interpolation.map_coordinates is the fastest way that we have found so far to reproject from cartesian to polar coordinates.)

    PyAbel can be installed from PyPi by entering the following on the command line:

    pip install pyabel
    

    Then, in python, you can use the following code to re-project an image into polar coordinates:

    import abel
    abel.tools.polar.reproject_image_into_polar(MyImage)
    

    [Depending on the application, you might consider passing the jacobian=True argument, which re-scales the intensities of the matrix to take into the account the stretching of the grid (changing "bin sizes") that takes place when you transform from Cartesian to polar coodinates.]

    Here is a complete example:

    import numpy as np
    import matplotlib.pyplot as plt
    import abel
    
    CartImage = abel.tools.analytical.sample_image(501)[201:-200, 201:-200]
    
    PolarImage, r_grid, theta_grid = abel.tools.polar.reproject_image_into_polar(CartImage)
    
    fig, axs = plt.subplots(1,2, figsize=(7,3.5))
    axs[0].imshow(CartImage , aspect='auto', origin='lower')
    axs[1].imshow(PolarImage, aspect='auto', origin='lower', 
                  extent=(np.min(theta_grid), np.max(theta_grid), np.min(r_grid), np.max(r_grid)))
    
    axs[0].set_title('Cartesian')
    axs[0].set_xlabel('x')
    axs[0].set_ylabel('y')
    
    axs[1].set_title('Polar')
    axs[1].set_xlabel('Theta')
    axs[1].set_ylabel('r')
    
    plt.tight_layout()
    plt.show()
    

    Note: there is another good discussion (about re-mapping color images to polar coordinates) on SO: image information along a polar coordinate system

提交回复
热议问题