PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, May 6, 2022

[FIXED] How to append images in numpy array?

 May 06, 2022     glob, image, numpy, opencv, python     No comments   

Issue

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,)


Solution

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)


Answered By - gilad eini
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing