convert mnist to pictures in matlab

To convert MNIST data set to images in Matlab, the following steps need to be followed:

  1. Load the MNIST data set, which includes images and labels, using the loadMNISTImages and loadMNISTLabels functions:
main.m
images = loadMNISTImages('train-images-idx3-ubyte');
labels = loadMNISTLabels('train-labels-idx1-ubyte');
106 chars
3 lines
  1. Reshape each image from a vector to a 28x28 matrix:
main.m
images = reshape(images,28,28,[]);
35 chars
2 lines
  1. Transpose the images for proper orientation:
main.m
images = permute(images,[2 1 3]);
34 chars
2 lines
  1. Save each image as a png file:
main.m
for i = 1:size(images,3)
    imwrite(images(:,:,i),strcat('MNIST_',num2str(i),'.png'))
end
91 chars
4 lines

This will create png files for each image in the current directory with names in the format "MNIST_i.png", where i is the index of the image.

gistlibby LogSnag