2012-03-29 511 views
5

我想将Python 2d数组/图像转换为极坐标,然后进行处理,然后将它们转换回笛卡儿坐标系。以下是从ImajeJ Polar Transformer插件的结果(样品上代码的同心圆使用):在Python中快速笛卡尔到极坐标到笛卡尔

enter image description here

的图像的数量和变暗是相当大的,所以我检查的OpenCV是否具有快速和简单的方法来做到这一点。

我读过关于cv。 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)工作流程,其中环状伪像如果以线条形式出现可以更容易地过滤掉。

回答

2

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

+0

非常感谢你!事实上'LinearPolar'就是这么说的。不幸的是,通过使用'import cv',它不可用,但我尝试了'从opencv导入cv'然后'cv.cvLinearPolar'并且工作。接下来的几天我会尝试它在大型数据集中的表现。谢谢! – Papado 2012-03-29 17:11:18

+0

很酷。我想知道为什么它不可见?我会尝试提交一个错误报告。 – 2012-03-29 17:18:52

+0

http://code.opencv.org/issues/1729 – 2012-03-29 17:30:23

2

下面是一个例子,数极使用SciPy的变换来实现:

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

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

+0

亲爱的Stefan,非常感谢您的回馈。我会在接下来的几天检查并测试您的实施。顺便说一句,我结束了浏览至尊,似乎很有趣。你有没有发表任何关于它的文章? – Papado 2012-04-17 09:08:43

+0

@Papado我从来没有看到您的评论,但是 - 是的 - 有关于arXiv和论文的论文。顺便说一句,现在可以使用''skimage.transform.warp''在大约5行代码的scikit-image上实现对数极坐标变换。 – 2015-03-12 07:43:46

3

最新版本的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 
+0

嗨亚历山德罗 - 我试图用这种方式解决这个问题,我写了类似的代码,虽然我用循环而不是meshgrid(我从来没有见过)。你知道这个表演有多好吗?我的VGA图像时间长达1秒 - 时间过长。 – cjm2671 2014-04-21 16:23:07

+0

好的,我已经测试了你的代码,与我的迭代解决方案相比,它非常快 - 另外我学到了一些新东西 - 非常感谢你! – cjm2671 2014-04-28 12:06:08