在一个广场上投射一个圆圈?

时间:2015-06-15 08:44:31

标签: python numpy scipy

我有一个photograph of a retina存储为numpy数组。我用这个2D numpy数组表示这个图像(我的实际数组要大得多,有3个颜色通道,值是浮点数,不是全部01):

array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 0, 0, 0, 0],
       [0, 0, 1, 1, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 1, 1, 0, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 0, 1, 1, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 1, 1, 0, 0],
       [0, 0, 0, 0, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0]])

如何投影圆圈(以某种方式拉伸边缘?),使其成为正方形而不会被裁剪?所以它看起来像这样:

array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 1, 1, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0]])

基本上,我正在寻找可以在此图像中执行保形映射的Python库

conformal mapping from circle to square

2 个答案:

答案 0 :(得分:0)

您可以使用行和列中非零元素的最大和最小索引来获取1的范围,然后根据此范围填充数组索引:

>>> a=np.array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
...        [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.],
...        [ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.],
...        [ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.],
...        [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.],
...        [ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.],
...        [ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.],
...        [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.],
...        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
>>> 
>>> (min_row,max_row),(min_col,max_col)=map(lambda x :(np.min(x),np.max(x)),np.nonzero(a))
>>> a[min_row:max_row+1,min_col:max_col+1]=1
>>> a
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

答案 1 :(得分:0)

为此使用squircle(免责声明:我写的)

import squircle
import PIL
import numpy as np

square = np.asarray(Image.open('some-square-image.jpg'))  # or create the array some other way

circle = squircle.to_circle(square, method="fgs")
and_back_to_square = squircle.to_square(circle, method="fgs")

有3种不同的圆形/正方形变换方法:"fgs""stretch""elliptical"

它不处理椭圆形和矩形,但是您可以通过对图像进行升采样,将其天真地压成一个正方形,在其上运行成半圆形然后将其解压成一个矩形来实现。

您可以使用

进行安装
pip install squircle
相关问题