Python中的快速笛卡尔到极坐标到笛卡尔

2024-04-28 09:58:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在Python 2d数组/图像中转换成极坐标,然后进行处理,然后再将它们转换回笛卡尔坐标。以下是ImajeJPolar Transformer插件(用于示例代码的同心圆)的结果:

enter image description here

图像的数量和大小相当大,所以我在检查openCV是否有一个快速简单的方法来完成这项工作。

我读过简历。CartToPolarPolarToCart但是我没有使用它。我更理解输入和输出是数组的LogPolar,以及可以设置中心、插值和反转(即CV_WARP_INVERSE_MAP)的地方。有没有类似的方式使用CartToPolar/PolarToCart?

    import numpy as np
    import cv

    #sample 2D array that featues concentric circles
    circlesArr = np.ndarray((512,512),dtype=np.float32)
    for i in range(10,600,10): cv.Circle(circlesArr,(256,256),i-10,np.random.randint(60,500),thickness=4)

    #logpolar
    lp = np.ndarray((512,512),dtype=np.float32)
    cv.LogPolar(circlesArr,lp,(256,256),100,cv.CV_WARP_FILL_OUTLIERS)

    #logpolar Inverse
    lpinv = np.ndarray((512,512),dtype=np.float32)
    cv.LogPolar(lp,lpinv,(256,256),100, cv.CV_WARP_INVERSE_MAP + cv.CV_WARP_FILL_OUTLIERS)

    #display images
    from scipy.misc import toimage
    toimage(lp, mode="L").show()
    toimage(lpinv, mode="L").show()

这是一个断层扫描(CT)的工作流程,环伪影可以更容易地过滤掉,如果他们出现的线。


Tags: 图像importnp数组cvndarraylpdtype
3条回答

下面是使用SciPy实现的对数极坐标变换的示例:

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

考虑到这只是一个坐标变换,因此比OpenCV版本更容易适应您的问题。

最新版本的opencv支持一个函数cv2.linearPolar。 这可能是另一个不涉及使用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

CV源代码提到了一个LinearPolar。它似乎没有文档记录,但似乎与LogPolar类似。你试过了吗?

相关问题 更多 >