Boris
Boris

Reputation: 213

How to read image from pixel data?

no use

I am trying to read image of cifar10 dataset in MATLAB. The data is given in 10000x3072 format in which one row contains corresponding RGB value. I used:

img= reshape(data(1, 1:1024), [32,32]);
image(img)

to convert the image into meaningful because it is showing garbage image. How can I read the image from this .mat file? from this dataset https://www.cs.toronto.edu/~kriz/cifar-10-matlab.tar.gz

Upvotes: 1

Views: 464

Answers (1)

beaker
beaker

Reputation: 16791

According to this page, the format of data is:

  • data -- a 10000x3072 numpy array of uint8s. Each row of the array stores a 32x32 colour image. The first 1024 entries contain the red channel values, the next 1024 the green, and the final 1024 the blue. The image is stored in row-major order, so that the first 32 entries of the array are the red channel values of the first row of the image.

Using your code:

img= reshape(data(1, 1:1024), [32,32]);

you should get the red channel of the first image in column-major order (i.e. transposed). To get a full RGB image with the correct orientation, you'll want to use:

img = reshape(data(1, 1:3072), [32,32,3]);   % get 3-channel RGB image
img = permute(img, [2 1 3]);   % exchange rows and columns

Upvotes: 2

Related Questions