Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 具有周期性边界条件的numpy数组的边界框(环绕)_Python_Arrays_Numpy_Bounding Box - Fatal编程技术网

Python 具有周期性边界条件的numpy数组的边界框(环绕)

Python 具有周期性边界条件的numpy数组的边界框(环绕),python,arrays,numpy,bounding-box,Python,Arrays,Numpy,Bounding Box,我想做一些类似于问题或其他问题的事情,但使用周期性边界条件(包装)。我来举个简单的例子 假设我有以下numpy数组: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 1 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0

我想做一些类似于问题或其他问题的事情,但使用周期性边界条件(包装)。我来举个简单的例子

假设我有以下numpy数组:

0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 
0 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 1 0 0 0 0 0 
0 0 1 1 1 1 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 
然后,通过使用两个链接问题中提出的方法之一,我能够提取非零值的边界框:

0 0 0 1 1 1 1 1 
0 0 0 1 0 0 0 0 
0 0 0 1 0 0 0 0 
1 1 1 1 0 0 0 0
但是,如果非零元素“穿过”边界并返回到另一侧,如下图所示:

0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 
1 1 0 0 0 0 0 0 1 1 1 
0 0 0 0 0 0 0 0 1 0 0 
0 0 0 0 0 0 0 0 1 0 0 
0 0 0 0 0 1 1 1 1 0 0 
0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 
结果是:

1 1 0 0 0 0 0 0 1 1 1 
0 0 0 0 0 0 0 0 1 0 0 
0 0 0 0 0 0 0 0 1 0 0 
0 0 0 0 0 1 1 1 1 0 0 
这不是我想要的。我希望结果与前一个案例相同。我正试图找出一个聪明的方法来做这件事,但我被卡住了。有人有想法吗?

我们可以这样修改答案:

import numpy as np

def wrapped_bbox(a):
    dims = [*range(1,a.ndim)]
    bb = np.empty((a.ndim,2),int)
    i = 0
    while True:
        n = a.shape[i]
        r = np.arange(1,2*n+1)
        ai = np.any(a,axis=tuple(dims))
        r1_a = np.where(ai,r.reshape(2,n),0).ravel()
        aux = np.maximum.accumulate(r1_a)
        aux = r-aux
        idx = aux.argmax()
        mx = aux[idx]
        if mx > n:
            bb[i] = 0,n
        else:
            bb[i] = idx+1, idx+1 - mx
            if bb[i,0] >= n:
                bb[i,0] -= n
            elif bb[i,1] == 0:
                bb[i,1] = n
        if i == len(dims):
            return bb
        dims[i] -= 1
        i += 1

# example
x = """
......
.x...-
..x...
.....x
"""

x = np.array(x.strip().split())[:,None].view("U1")
x = (x == 'x').view('u1')

print(x)
for r in range(x.shape[1]):
    print(wrapped_bbox(np.roll(x,r,axis=1)))
运行:

[[0 0 0 0 0 0]   # x
 [0 1 0 0 0 0]
 [0 0 1 0 0 0]
 [0 0 0 0 0 1]]
[[1 4]           # bbox vert
 [5 3]]          # bbox horz, note wraparound (left > right)
[[1 4]
 [0 4]]          # roll by 1
[[1 4]
 [1 5]]          # roll by 2
[[1 4]
 [2 6]]          # etc.
[[1 4]
 [3 1]]
[[1 4]
 [4 2]]