beg*_*er_ 3 python numpy image-processing multidimensional-array
我有一个 3D numpy 数组。这可以被认为是一个图像(准确地说是场点的值)。我想删除所有维度的边框(0 值,请注意可能存在负值)。限制是所有分子的尺寸保持不变,例如。我只想删除边界,只要该维度中的“最大”条目仍在边界内。因此需要考虑整个数据集(小,大小不是问题)。
二维示例:
0 0 0 0 0
0 1 0 0 0
0 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 1 0 0
0 0 0 1 0
0 0 0 1 0
Run Code Online (Sandbox Code Playgroud)
这里最上面一行和最左边和最右边的列应该被删除。在整个数据集中,它们只包含 0 个值。
结果如下:
1 0 0
1 1 0
0 0 0
0 0 0
0 0 0
0 1 0
0 0 1
0 0 1
Run Code Online (Sandbox Code Playgroud)
由于我不是麻木专家,因此无法定义算法来满足我的需求。我需要在每个维度中找到不是 0 的最小和最大索引,然后使用它来修剪数组。
与此类似,但在 3D 中,裁剪必须考虑整个数据集。
我怎样才能做到这一点?
2019 年 2 月 13 日更新:
所以我在这里尝试了 3 个答案(一个似乎已被删除的使用 zip 的答案),Martins 和 norok2s 的答案。输出尺寸相同,所以我假设它们都可以工作。
我选择 Martins 解决方案是因为我可以轻松提取边界框以将其应用于测试集。
2月25日更新:
如果有人仍然在观察这一点,我想提供进一步的意见。如前所述,这些实际上不是图像,而是“字段值”,意思是浮点数而不是灰度图像(uint8),这意味着我至少需要使用 float16,而这只是需要太多内存。(我有 48gb 可用,但即使对于 50% 的训练集也不够)。
试试这个: - 它是一个主要的算法。我不明白你想从你的例子中提取哪一边,但下面的算法应该很容易让你根据你的需要进行修改
注意:该算法提取所有零值边界都被“删除”的 CUBE。所以在立方体的每一边都有一些值!= 0
import numpy as np
# testing dataset
d = np.zeros(shape = [5,5,5])
# fill some values
d[3,2,1]=1
d[3,3,1]=1
d[1,3,1]=1
d[1,3,4]=1
# find indexes in all axis
xs,ys,zs = np.where(d!=0)
# for 4D object
# xs,ys,zs,as = np.where(d!=0)
# extract cube with extreme limits of where are the values != 0
result = d[min(xs):max(xs)+1,min(ys):max(ys)+1,min(zs):max(zs)+1]
# for 4D object
# result = d[min(xs):max(xs)+1,min(ys):max(ys)+1,min(zs):max(zs)+1,min(as):max(as)+1]
>>> result.shape
(3, 2, 4)
Run Code Online (Sandbox Code Playgroud)
情况1:
d = np.zeros(shape = [5,5,5])
d[3,2,1]=1
# ... just one value
>>> result.shape # works
(1,1,1)
Run Code Online (Sandbox Code Playgroud)
案例 2:# 错误案例 - 只有零 - 结果 3D 没有维度 -> 错误
d = np.zeros(shape = [5,5,5]) # no values except zeros
>>> result.shape
Traceback (most recent call last):
File "C:\Users\zzz\Desktop\py.py", line 7, in <module>
result = d[min(xs):max(xs)+1,min(ys):max(ys)+1,min(zs):max(zs)+1]
ValueError: min() arg is an empty sequence
Run Code Online (Sandbox Code Playgroud)
编辑:因为我的解决方案没有得到足够的爱和理解,我将为第 4 维身体提供示例,其中 3 维是免费的图像,第 4 维是存储图像的地方
import numpy as np
class ImageContainer(object):
def __init__(self,first_image):
self.container = np.uint8(np.expand_dims(np.array(first_image), axis=0))
def add_image(self,image):
#print(image.shape)
temp = np.uint8(np.expand_dims(np.array(image), axis=0))
#print(temp.shape)
self.container = np.concatenate((self.container,temp),axis = 0)
print('container shape',self.container.shape)
# Create image container storage
image = np.zeros(shape = [5,5,3]) # some image
image[2,2,1]=1 # put something random in it
container = ImageContainer(image)
image = np.zeros(shape = [5,5,3]) # some image
image[2,2,2]=1
container.add_image(image)
image = np.zeros(shape = [5,5,3]) # some image
image[2,3,0]=1 # if we set [2,2,0] = 1, we can expect all images will have just 1x1 pixel size
container.add_image(image)
image = np.zeros(shape = [5,5,3]) # some image
image[2,2,1]=1
container.add_image(image)
>>> container.container.shape
('container shape', (4, 5, 5, 3)) # 4 images, size 5x5, 3 channels
# remove borders to all images at once
xs,ys,zs,zzs = np.where(container.container!=0)
# for 4D object
# extract cube with extreme limits of where are the values != 0
result = container.container[min(xs):max(xs)+1,min(ys):max(ys)+1,min(zs):max(zs)+1,min(zzs):max(zzs)+1]
>>> print('Final shape:',result.shape)
('Final shape', (4, 1, 2, 3)) # 4 images, size: 1x2, 3 channels
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2528 次 |
| 最近记录: |