0
$\begingroup$

I have a set of images which are loaded from an h5 file. I checked their dimensions and I get (209, 64, 64, 3).

read = h5py.File('datasets/train_catvnoncat.h5', 'r')
read['train_set_x'].shape

(209, 64, 64, 3)

it means that there are 209 images but the point that I cannot understand is that, what is (64, 64, 3)?
I have used the following code for plotting:

import matplotlib.pyplot as plt

plt.imshow(read['train_set_x'][1])
plt.show()

and I get a colored image which is 64 by 64. before this, I thought for (., ., .) shapes, the second number specifies the number of lines and the third one specifies the number of rows. also the first one specifies the number of the mentioned (row and column) arrays.
My question is that in numpy if you have a three dimensional array, for accessing rows and columns you have to change the second and third entries in the indexing operator; Why this is different in images and rows and columns are arranged differently in images. Shouldn't it be (3, 64, 64)?

$\endgroup$
4
  • $\begingroup$ Assuming these are colour images, it seems likely that the 64x64 is row and column index, and the remaining three values are the RGB channels for the image. $\endgroup$
    – R Hill
    Sep 21, 2017 at 15:42
  • $\begingroup$ @RHill This is exactly my point. Shouldn't it be (3, 64, 64)? $\endgroup$ Sep 21, 2017 at 15:49
  • $\begingroup$ @NeilSlater you are telling me this ordering is different from what we have in other containers such as list or tuples? $\endgroup$ Sep 21, 2017 at 16:24
  • $\begingroup$ @NeilSlater actually I want to change my example, instead of tuples or list think about arrays in numpy or matrices in numpy. whenever we want to access the rows and columns of matrices (actually tensors) with 3-d shape we use second and third entries to access the corresponding elements. $\endgroup$ Sep 21, 2017 at 16:28

1 Answer 1

3
$\begingroup$

The [64, 64, 3] shape you have found is a common convention to represent a colour image in (x, y, colour_channel) dimensions.

The key word here is convention - there is no inherently preferred way to represent a colour image in terms of fundamental maths or computing needs, and even within Python you will find multiple conventions, varying in the ordering within the dimension - e.g. OpenCV uses (x, y, channel) convention for the shape, but has channels in order BGR - so channel 0 is blue - whilst most other libraries will use RGB ordering (ignoring for now the alternative colour spaces).

My question is that in numpy if you have a three dimensional array, for accessing rows and columns you have to change the second and third entries in the indexing operator

When you have a 3-dimensional array, what you decide to call "rows" and "columns" is also a convention. It depends partly on what that array represents, and there is no single way to visualise the contents.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.