将图像数组 [n, width, height] 重塑为 [n, height, width, channel]

reshaping image array [n, width, height] to [n, height, width, channel]

我的图像由单个数字组成,宽度为 32,高度为 60。我试图将它们连接起来以获得最多 5 位数的图像。

我正在生成如下所示的个位数图像;

def gen(n=10, max_digs=1, width=32):
    capgen = ImageCaptcha(width=width, height=60)
    data = []
    target = []
    for i in range(n):
        x = np.random.randint(0, 10 ** max_digs)
        img = misc.imread(capgen.generate(str(x)))
        img = np.mean(img, axis=2)[:, :width]
        data.append(img.flatten())
        target.append(x)
    return np.array(data), np.array(target)

然后我尝试用下面的方法连接它们

def generate_dataset(X, y):
    X_len = X.shape[0]
    X_gen = np.zeros((X_len, height, width * n_len, 1), dtype=np.uint8)
    y_gen = [np.zeros((X_len, n_class), dtype=np.uint8) for i in range(n_len)]
    # generate random numbers of digits
    n_digit = random.randint(1, 6)
    for j in range(X_len):
        n_digit = random.randint(1, 5)
        for i in range(n_digit):
            index = random.randint(0, X_len - 1)
            image = X[index]
            label = y[index]
            X_gen[j][:, i*height:width + i * width, 0] = image
            y_gen[i][j][label] = 1
        for i in range(n_digit, n_len):
            y_gen[i][j][10] = 1
    return X_gen, y_gen

但是,它给了我错误 "could not broadcast input array from shape (32,60) into shape (60,0)"

好的,左右的形状好像不符

X_gen[j][:, i*height:width + i * width, 0] 

形状为 (60, 32) 而图像的形状为 (32,60)

也许试试下面的方法看看是否有效?

X_gen[j][:, i*height:width + i * width, 0] = image.T

更新

最终,此更改解决了问题:

X_gen[j][:, i*width: (i+1)*width, 0] = image.T