fast Cartesian to Polar to Cartesian in Python

前端 未结 3 1858
余生分开走
余生分开走 2020-12-28 23:44

I want to transform in Python 2d arrays/images to polar, process then, and subsequently transform them back to cartesian. The following is the result from ImajeJ Polar Trans

相关标签:
3条回答
  • 2020-12-29 00:15

    the CV source code mentions a LinearPolar. it doesn't seem to be documented, but appears to be similar to LogPolar. have you tried that?

    0 讨论(0)
  • 2020-12-29 00:18

    Latest versions of opencv supports a function cv2.linearPolar. This may be another solution that does not involve the use of opencv:

    def polar2cart(r, theta, center):
    
        x = r  * np.cos(theta) + center[0]
        y = r  * np.sin(theta) + center[1]
        return x, y
    
    def img2polar(img, center, final_radius, initial_radius = None, phase_width = 3000):
    
        if initial_radius is None:
            initial_radius = 0
    
        theta , R = np.meshgrid(np.linspace(0, 2*np.pi, phase_width), 
                                np.arange(initial_radius, final_radius))
    
        Xcart, Ycart = polar2cart(R, theta, center)
    
        Xcart = Xcart.astype(int)
        Ycart = Ycart.astype(int)
    
        if img.ndim ==3:
            polar_img = img[Ycart,Xcart,:]
            polar_img = np.reshape(polar_img,(final_radius-initial_radius,phase_width,3))
        else:
            polar_img = img[Ycart,Xcart]
            polar_img = np.reshape(polar_img,(final_radius-initial_radius,phase_width))
    
        return polar_img
    
    0 讨论(0)
  • 2020-12-29 00:21

    Here's an example of the log-polar transform implemented using SciPy:

    https://github.com/stefanv/supreme/blob/master/supreme/transform/transform.py#L51

    Given that this is only a coordinate transformation, it should be easier to adapt to your problem than the OpenCV version.

    0 讨论(0)
提交回复
热议问题