How many ways to flatten 3 channels image and reconstruct

Hey all,

I just want to get a list of bidirectional approaches to

  1. flatten 3 channel image
  2. reconstruct from the flattened array

Can you input it, thank you.

Here is one example from me, I am not sure if it is correct or not:

    image        = PIL.......

    image        = np.array(image)
    origin_shape = image.shape

    image_flatten = image.ravel() 
    image_flatten = tf.cast(image_flatten, dtype=tf.float32)
    image_flatten = image_flatten[np.newaxis][:]


    .....
    ............


    rebuilt_image= np.reshape(image_flatten, origin_shape)  

Here’s a thread which describes at least 2 correct approaches and one incorrect approach for flattening 3 channel images. But it does not discuss reconstructing the 4D arrays from the flattened arrays.

Thanks, however I do appreciate the interest more in the reconstruction.

Try some experiments with numpy reshape. Should be pretty straightforward. It’s thermodynamically reversible, right? :laughing: Oh, sorry, I see you edited your original post to add such a strategy.

Ja, but I worry if it was right or not.
WDYT on that?

There’s an easy way to find out, right? I can name that tune in one line of python:

assert(np.array_equal(A, B))

What I worry whether the "bit"s are positing in the right places after they were flattened and returning to the right shape :grin:

Yes, and I just showed you how to get that answer at least in numpy. You’re a mentor for TF, so I’m sure you can figure out how to express the same idea in TF.

1 Like

If they don’t provide the equivalent of the array_equal function, you can just do:

tf.reduce_sum(A == B)

and then compare that to the total number of elements in either array.