2017-07-30 97 views
1

我有以下图像(掩模),覆盖不规则区域与圈

enter image description here

,我想以填补,像这样的圆形区域,

enter image description here

在这个特定的例如我手动使用X & Y方向上的步骤中创建的圈子并且仅保持一个圆如果它95%的白色区域下降。不过,我想最大化地图的覆盖范围。我认为这可以通过随机生成圈子的中心并旨在最大化覆盖范围来实现。我不介意的区域重叠一点,如果我还可以最大限度的使用面积

是否有任何建议,我怎么能解决这个问题,或者有可以为我做任何算法?

+0

填充或覆盖? –

回答

1

这就是所谓的circle packing问题,是非常困难的。

如果您不关心确切的边界并希望最大化覆盖率,则应该使用hexagonal tiling,它可以为您提供〜90.69%的覆盖率。

你可以做这样的事情:

import numpy as np 
from scipy import misc 
import matplotlib.pyplot as plt 

image = misc.imread('xZoI6.png') 

ys, xs, _ = image.shape 

x, y = np.meshgrid(np.arange(xs), np.arange(ys), sparse=True) 

radius = 20.0 

for i in range(int(-0.5 * ys/radius), int(xs/radius)): 
    for j in range(int(ys/radius)): 
     x0 = 2 * radius * (i + 0.5 * j) 
     y0 = 2 * radius * np.sqrt(3)/2 * j 

     r = np.sqrt((x - x0) ** 2 + (y - y0) ** 2) 

     indicator = r < radius 

     if np.any(image[indicator, 0] != 0): 
      image[r < radius] = [255, 0, 0, 255] 

plt.imshow(image) 

这给:

enter image description here