1

I used python's list to add multiple numpy.array images read by opencv:

[array([[[167, 145, 121],
        [164, 142, 118],
        [167, 145, 121],
        ...,          
        [248, 243, 214],
        [246, 242, 213],
        [249, 245, 216]],
       [[172, 150, 126],
        [168, 146, 122],
        [163, 141, 117],
        ...,
        [249, 244, 214],
        [246, 242, 213],
        [248, 244, 215]],
       ...,]

I want to turn the outermost list into a numpy array, that is, a 4-axis tensor np.array:

array([[[[167, 145, 121],
        [164, 142, 118],
        [167, 145, 121],
        ...,
        [248, 243, 214],
        [246, 242, 213],
        [249, 245, 216]],

       [[168, 146, 122],
        [164, 142, 118],
        [164, 142, 118],
        ...,
        [248, 243, 214],
        [246, 242, 213],
        [249, 245, 216]],

       [[172, 150, 126],
        [168, 146, 122],
        [163, 141, 117],
        ...,
        [249, 244, 214],
        [246, 242, 213],
        [248, 244, 215]],

       ...,]

However, if I use np.array(mylist) directly, it becomes:

array([array([[[167, 145, 121],
        [164, 142, 118],
        [167, 145, 121],
        ...,
        [248, 243, 214],
        [246, 242, 213],
        [249, 245, 216]],

       [[168, 146, 122],
        [164, 142, 118],
        [164, 142, 118],
        ...,
        [248, 243, 214],
        [246, 242, 213],
        [249, 245, 216]],
        ...,
        [249, 244, 214],
        [246, 242, 213],
        [248, 244, 215]],
        ....]

Is there a way to convert this?

1 Answer 1

1

Does all the images have the same shape (width, heigh and number of channels)? If so, doing a np.array(mylist) should have worked just fine. For example, here I created 10 random images:

my_list = [np.random.randint(0, 255, size=(1920, 1080, 3), dtype=np.uint8) for i in range(10)]
converted = np.array(my_list)

Which results in what you expects:

 array([[[[213,  60,  51],
         [229, 125, 207],
         [104, 139, 243],
         ...,
         [166, 219,  32],
         [116,  27, 108],
         [ 99,  79,  21]],

        [[176, 141, 170],
         [107, 131,  83],
         [ 23, 210, 126],
         ...,
         [147,  41, 167],
         [203, 118,  86],
         [175,   5,  88]]]], dtype=uint8)

Now, if there are images with different shapes, you need to manually define the resulting shape. Otherwise it will fail and give you a warning (VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes is deprecated.)

For instance, I created a random list of images in different sizes and selected the biggest dimension, padding the results with zeros.

my_list = [np.random.randint(0, 255, size=(1920-i, 1080-i, 3), dtype=np.uint8) for i in range(10)]
largest_shape = np.max(np.array([m.shape for m in my_list]), axis=0)
result = np.zeros([len(my_list)]+largest_shape.tolist())
for i, m in enumerate(my_list):
    result[i, :m.shape[0], :m.shape[1], :m.shape[2]] = m
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.