N.zay
N.zay

Reputation: 71

Padding 3d numpy image array with zeros

I have 357 .bmp images (shape:(357,227,227))

which i read them into numpy array , then I padded them into standard size of

(4608 ,227,227 ). The problem is when I read images from the padded .npy all the

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

Upvotes: 1

Views: 1059

Answers (1)

fountainhead
fountainhead

Reputation: 3722

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.

Upvotes: 1

Related Questions