4

语境

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

例子

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

在此处输入图像描述

当前代码

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

4 回答 4

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因此实际上没有重叠。

于 2013-03-15T12:42:28.650 回答
1

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

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

于 2013-03-15T12:33:42.847 回答
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

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

于 2013-03-15T13:02:13.850 回答
0

您可以使用Floodfill 算法,稍微修改停止条件:同时考虑颜色和到圆心的距离

小面积区域的 PS Hack 方法:绘制实心圆并计算颜色像素...

于 2013-03-15T12:58:41.623 回答