Why conceptual and coding representing of Numpy image different?

Hi Ml Enthusiast,
I have a very naive question regarding creating numpy multi-dimentional array. If I were to create a random noise image of size 28,28 in RGB channel, which of the following representation will I use?

image = np.random.rand(28,28,3) or np.random.rand(3,28,28) ? And why the given option?
The reason for asking this question is as in week-2 coding assignment the image used is of size (64,64,3) but while printing we get representation as 64 rows, 3 columns and 64 in depth.
So I was a bit confused with image actually getting represented is different from theoretical way it’s explained in the workbook.

1 Like

Hi @VivekKapoor

(height, width, channels) is the standard representation for images. So, to create a random noise, you should use:

image = np.random.rand(28, 28, 3)

Using np.random.rand(3, 28, 28) is less common!

Hope this clears up your confusion!

1 Like

Hi @Alireza_Saei - Thanks for your reply. But just a quick follow up question on this, when I try to print the output why doesn’t it print the way we want image it to be represented (i.e., 28,28 of 2-d array for each of the three dimension?

Also if you say we create image using np.random.rand(28, 28, 3), then to access the R channel of data we would have to write image[:,:,0] ?

1 Like

Yes, you are right. To access Red channel you can use the following code:

import numpy as np

image = np.random.rand(28, 28, 3)

R_channel = image[:, :, 0]

print("R channel Shape:")
print("R channel (28x28):")

Also, the code above prints the Red channel array as expected, displaying it as a list of 28 lists, each containing 28 entries.

Hope it helps!

1 Like

Hi @Alireza_Saei - Yes when I print channel wise I get the right way of representation but when I print the whole image as image [:,:,:] or image I get each 2-d array comprising of size 28*3 spread across 28 channels why such discrepancy?

1 Like

The discrepancy arises because when you print the whole image array, you see all the channels flattened together, which can appear as if each 28x3 section is spread across 28 channels.