根据圆的面积更改numpy数组中的值

时间:2013-03-15 12:19:01

标签: python algorithm numpy computational-geometry

上下文

我需要在python中测量组合圆的面积。我想出了使用numpy数组的方法。首先,我用零填充网格(numpy数组),网格中的每个位置对应0.5cm的长度。然后我将圆心放到网格上,并在网格中将此值更改为1。我知道圆的半径,所以我可以计算圆的面积,因为我知道圆的面积我改变了网格中零点的区域。然后我计算网格中的一些频率并使用它来计算组合圆的面积,因为我知道网格中每个位置的长度我可以计算面积。这是目前非常粗糙的方法,我计划在算法确定后将其更改为更精细的颗粒。

示例

如果你看一下我在下面发布的图片,它会更好地描述我的想法。我的网格上有两个圆圈(红线),圆圈的中心标有蓝色方块,圆圈占据的区域为浅橙色。我想将标有橙色的区域更改为1。我现在可以改变水平和垂直于圆心的橙色方块,但是中心的对角线方框给我带来了麻烦。

enter image description here

当前代码

class area():

    def make_grid(self):
        '''
        Each square in the grid represents 0.5 cm
        '''
        import numpy as np
        grid = np.zeros((10,10))
        square_length = 0.5
        circles = {'c1':[[4,2],1.5],'c2':[[5,6],2.0]}

        print grid
        for key,val in circles.iteritems():
            grid[val[0][0]][val[0][1]] = 1
            area = int((val[1] - square_length)/0.5)            
            for i in xrange(1,area+1):
                grid[val[0][0]][val[0][1]+i] = 1 # Change column vals in +ve direction
                grid[val[0][0]][val[0][1]-i] = 1 # Chnage column vals in -ve direction
                grid[val[0][0]+i][val[0][1]] = 1 # Chnage row vals in +ve direction 
                grid[val[0][0]-i][val[0][1]] = 1 # Chnage row vals in -ve direction

        print ''
        print grid

在上面的词典中,键是圆圈名称,值中的第一个元素是圆心坐标,第二个元素是圆的半径。

代码输出:

[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  1.  0.  0.  0.  1.  0.  0.  0.]
 [ 0.  0.  1.  0.  0.  0.  1.  0.  0.  0.]
 [ 1.  1.  1.  1.  1.  0.  1.  0.  0.  0.]
 [ 0.  0.  1.  1.  1.  1.  1.  1.  1.  1.]
 [ 0.  0.  1.  0.  0.  0.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]

4 个答案:

答案 0 :(得分:3)

根据@Jaime的评论更新。

我可能会从这样的事情开始。重点是正确计算圆内像素。

import numpy as np
import matplotlib.pyplot as plt

grid = np.zeros((10,10), dtype=np.bool)
square_length = 0.5
circles = {'c1':[[4,2],1.5],'c2':[[5,6],2.0]}

# Generate arrays of indices/coordiates so we can do the
# calculations the Numpy way, without resorting to loops
# I always get the index order wrong so double check...
xx = np.arange(grid.shape[0])
yy = np.arange(grid.shape[1])

for val in circles.itervalues():
    radius = val[1]
    # same index caveat here
    # Calling Mr Pythagoras: Find the pixels that lie inside this circle
    inside = (xx[:,None] - val[0][0]) ** 2 + (yy[None, :] - val[0][1]) ** 2 <= (radius ** 2)
    # do grid & inside and initialize grid with ones for intersection instead of union
    grid = grid | inside 

plt.imshow(grid)
plt.show()

如果您在交叉路口之后,请注意您的圆心相距sqrt(17) ~= 4.123..个单位,两个半径的总和为3.5,因此实际上没有重叠。

答案 1 :(得分:1)

您只是从中心直接左右搜索。要从中心获得正方形对角线,您可能需要使用Pythagorean theorem。根据该定理,您可以通过相对于圆的中心水平和垂直使用偏移作为边长来找到正方形的斜边。然后,您可以将斜边与半径进行比较,如果斜边是两者中较短的一个,则将平方值增加一。

半径的使用也有点奇怪,因为它不是以正方形的中间为中心。这使得半径为2的圆的直径为3.5。

答案 2 :(得分:1)

您可以使用蒙特卡罗方法查找重叠区域的区域。这样会更准确。 http://www.chem.unl.edu/zeng/joy/mclab/mcintro.html

找到正方形的边界,然后用随机数填充,并为每个随机数检查随机数是否在圆圈内。

if (np.sqrt((xx - val[0][0]) ** 2 + (yy - val[0][1]) ** 2) <= radius) :inside=inside+1

面积=圆内像素总数/生成的像素总数*正方形面积

答案 3 :(得分:0)

您可以稍微修改停止条件来使用Floodfill algorithm:考虑到圆心的颜色和距离

P.S。低尺寸区域的哈克方法:绘制实心圆圈并计算颜色像素......