简体   繁体   English

用零填充 3d numpy 图像数组

[英]Padding 3d numpy image array with zeros

I have 357 .bmp images (shape:(357,227,227))我有 357 张 .bmp 图像(形状:(357,227,227))

which i read them into numpy array , then I padded them into standard size of我将它们读入 numpy 数组,然后将它们填充为标准大小

(4608 ,227,227 ). (4608,227,227)。 The problem is when I read images from the padded .npy all the问题是当我从填充的 .npy 中读取图像时

images are showing as black , means all the images are padded with zeros .图像显示为黑色,意味着所有图像都用零填充。

I don't know why is padding all the images zeros , I need to keep the images.我不知道为什么要填充所有图像零,我需要保留图像。 below is what I tried :以下是我尝试过的:

allfiles = os.listdir(pth_upd)
files = []
columns = ['data']
for file in allfiles:
    files.append(file) if ('.bmp' in file) else None
    samples = np.empty((1,227,227))

for file in files:
    img = cv2.imread(os.path.join(pth_upd,file),0)
    img = img.reshape(1,227,227)
    img=img.astype(np.float32)
    samples = np.append(samples, img, axis=0)

    if (len(samples)< 4608) :

        pad_size=4608-len(samples)       

        samples = np.pad(samples,(( pad_size,0),(0,0),(0,0)),mode='constant', constant_values=0) 

        f_name=format(folder)
        np.save(f_name, samples)
        print('saved')
        print(samples.shape)

    else:
        None

The reason why this is happening is that you're doing the padding inside your loop over all image files.发生这种情况的原因是您在循环内对所有图像文件进行填充。

So, whenever you do the padding, you're overwriting whatever images you loaded in the previous iteration.因此,无论何时进行填充,都会覆盖上一次迭代中加载的任何图像。

You should be doing the padding after you finish looping over all the image files.完成对所有图像文件的循环后,您应该进行填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM