Create Numpy array of images

前端 未结 4 1623
臣服心动
臣服心动 2021-01-31 06:01

I have some (950) 150x150x3 .jpg image files that I want to read into an Numpy array.

Following is my code:

X_data = []
files = glob.glob (\"*.jpg\")
fo         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-31 06:28

    I tested your code. It works fine for me with output

    ('X_data shape:', (4, 617, 1021, 3))

    however, all images were exactly the same dimension.

    When I add another image with different extents I have this output:

    ('X_data shape:', (5,))

    So I'd recommend checking the sizes and the same number of channels (as in are really all images coloured images)? Also you should check if either all images (or none) have alpha channels (see @Gughan Ravikumar's comment)

    If only the number of channels vary (i.e. some images are grey), then force loading all into the color format with:

    image = cv2.imread (myFile, cv2.IMREAD_COLOR)
    

    EDIT: I used the very code from the question, only replaced with a directory of mine (and "*.PNG"):

    import cv2
    import glob
    import numpy as np
    
    X_data = []
    files = glob.glob ("C:/Users/xxx/Desktop/asdf/*.PNG")
    for myFile in files:
        print(myFile)
        image = cv2.imread (myFile)
        X_data.append (image)
    
    print('X_data shape:', np.array(X_data).shape)
    

提交回复
热议问题