将3D图像堆叠到4D

问题描述 投票:0回答:2

我正在尝试堆叠3D图像以具有4D阵列。我的代码为:

def stack():
   x=None
   dim=(299,299)
   for file in os.listdir(path_to_folder):
        if file.endswith('.jpg'):
            img = cv2.imread(path_to_folder+ file)
            image_a = cv2.resize(img,dim, interpolation = cv2.INTER_AREA)

            img2 = cv2.imread(path_to_folder+ file)
            image_p = cv2.resize(img2,dim, interpolation = cv2.INTER_AREA)

            img3 = cv2.imread(path_to_folder+ file)
            image_n = cv2.resize(img3,dim, interpolation = cv2.INTER_AREA)

            if (x is None):
                x=[(image_a),(image_p),(image_n)]
            else:
                x[0]=np.stack((x[0], (image_a)))
                x[1]= np.stack((x[1],(image_p)))
                x[2]=np.stack((x[2],(image_n)))

    return x

我期望形状为:

stack=stack()
stack[0].shape
>>out: (5,299,299,3)

len(stack)
>>out: 3

但是我得到了(1495,299,3)。注意:为了只关注实际问题,我将所有3个图像文件都保持不变。我已经从here中使用了[[generate_triplets函数,但就我而言,我的图像是从文件夹中读取的。

python numpy
2个回答
2
投票
有很多方法可以做到这一点,我将探索其中的一些方法。首先,np.stack要求所有数组的形状相同。您不能像这样反复调用np.stack。一个简单的解决方法是将所有图像存储在一个列表中,然后在最后调用堆栈。让我整理一些伪代码:

import numpy as np def stack(): x = [[],[],[]] dim = (299,299) for i in range(5): img_a = np.random.randn(dim[0],dim[1],3) img_p = np.random.randn(dim[0],dim[1],3) img_n = np.random.randn(dim[0],dim[1],3) x[0].append(img_a) x[1].append(img_p) x[2].append(img_n) x = [np.stack(im) for im in x] return x stack = stack() print(out[0].shape)

输出:

(5, 299, 299, 3) 3

但是,如果出于某种原因要在每次迭代中进行堆栈,您仍然可以使用vstack进行操作。您只需要通过重塑使每个3d图像成为4d图像即可。

import numpy as np def stack(): x = None dim = (299,299) for i in range(5): img_a = np.random.randn(dim[0],dim[1],3) img_p = np.random.randn(dim[0],dim[1],3) img_n = np.random.randn(dim[0],dim[1],3) if (x is None): x=[img_a,img_p,img_n] else: s = (-1,dim[0],dim[1],3) x[0]=np.vstack((x[0].reshape(s), img_a.reshape(s))) x[1]=np.vstack((x[1].reshape(s), img_p.reshape(s))) x[2]=np.vstack((x[2].reshape(s), img_n.reshape(s))) return x stack = stack() print(stack[0].shape) print(len(stack))

输出:

(5, 299, 299, 3) 3


0
投票
修改@Mercury的功能:

In [398]: def stack(): ...: alist = [] ...: dim = (299,299) ...: for i in range(5): ...: img_a = np.random.randn(dim[0],dim[1],3) ...: img_p = np.random.randn(dim[0],dim[1],3) ...: img_n = np.random.randn(dim[0],dim[1],3) ...: alist.append([img_a, img_p, img_n]) ...: return np.array(alist) In [399]: stack().shape Out[399]: (5, 3, 299, 299, 3)

alist是包含(299,299,3)数组的嵌套(5,3)列表。变成数组变成5d。

我们可以换位,stack().transpose(1,0,2,3,4)。但是np.stack是连接的版本,可让我们指定new轴:

In [400]: def stack(): ...: alist = [] ...: dim = (299,299) ...: for i in range(5): ...: img_a = np.random.randn(dim[0],dim[1],3) ...: img_p = np.random.randn(dim[0],dim[1],3) ...: img_n = np.random.randn(dim[0],dim[1],3) ...: alist.append([img_a, img_p, img_n]) ...: return np.stack(alist, axis=1) ...: In [401]: stack().shape Out[401]: (3, 5, 299, 299, 3)

© www.soinside.com 2019 - 2024. All rights reserved.