0

I'm trying to make a numpy with this shape(number_of_images,32,32) all the images have the same dimensions: 32,32 here is my code:

data=np.array([])
for filename in glob.glob(path+'*.tif'): 
    im = np.array([np.array(cv2.imread(filename, cv2.IMREAD_GRAYSCALE))])
    #im = preprocess(im)
    im = NormalizeData(im)
    data=np.append(data,im)

im shape is (1,32,32). however data shape is not the one I wanted (number_of_images,32,32). the one I currently have is (113664,)

3
  • So the data shape you want is (113632, 32, 32), but it is outputting (1, 32, 32)? Just clarifying. Commented Mar 8, 2022 at 23:28
  • why is (113664,) desired? how do you figure that? the number equals 111 * 32 * 32 so you have 111 images? Commented Mar 9, 2022 at 10:11
  • I updated the description for more clarification. yes, it is 111 images and the shape I wanted is(111,32,32). The one I had was(113664,) Commented Mar 9, 2022 at 12:42

1 Answer 1

1

try using lists and at the end cast to numpy - less confusing

def main():
    cv_img_fake = np.zeros(shape=(1, 32, 32), dtype=np.uint8)
    print('image shape {}'.format(cv_img_fake.shape))

    images = []
    for filename_i in range(10):  # imagine 10 images
        print('filename {}:'.format(filename_i))
        im = cv_img_fake.copy()  # shape 1,32,32
        print('\timage shape {}'.format(im.shape))
        im = im.reshape(-1, im.shape[-1])  # shape 32,32
        print('\timage shape {}'.format(im.shape))

        # do to im whatever you want except changing the dims
        # check after every function by printing dims didn't change - e.g. still 32,32
        # im = NormalizeData(im)
        # print('\timage shape {}'.format(im.shape))
        # im = preprocess(im)
        # print('\timage shape {}'.format(im.shape))
        images.append(im)
    images = np.uint8(images)  # 10 images of 32, 32
    print('images shape {}'.format(images.shape))  # 10,32,32
    return

Output:

image shape (1, 32, 32)
filename 0:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 1:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 2:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 3:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 4:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 5:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 6:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 7:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 8:
    image shape (1, 32, 32)
    image shape (32, 32)
filename 9:
    image shape (1, 32, 32)
    image shape (32, 32)
images shape (10, 32, 32)
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.