1

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
4
  • Something has gone very wrong here! Do you have 357 separate images? Are your input images all greyscale and all sized at 227px by 227px? Do you want to resize them and reeve them to disk? If so, what size, in pixels should they be afterwards? Commented Feb 28, 2019 at 12:24
  • Are you sure that the padding is messing things up? Did you try commenting out the padding part of your code and see if the images are read back unchanged? Commented Feb 28, 2019 at 12:28
  • @ Mark Setchell , I need them to be the same size (227,227) just pad with zero until I get (4608,227,227) Commented Feb 28, 2019 at 14:26
  • @fountainhead , yes I read the images back from the padding .npy file and all the images are black even the original 357 images Commented Feb 28, 2019 at 14:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.