create mnist base from my 10 pictures in matlab

To create an MNIST dataset from 10 pictures in MATLAB, we need to follow these steps:

  1. Load the images from the directory using the "imread" function.
  2. Convert the images to grayscale using the "rgb2gray" function.
  3. Resize the images to 28x28 pixels using the "imresize" function.
  4. Normalize the image pixel values between 0 and 1.
  5. Save the images in the MNIST format with labels 0 to 9.

Here is a sample code that you can use:

main.m
% Load the images from the directory
image_dir = 'path/to/image/directory';
image_files = dir(fullfile(image_dir, '*.jpg'));
num_images = numel(image_files);

% Initialize the MNIST data and label arrays
mnist_data = zeros(28, 28, num_images);
mnist_labels = zeros(num_images, 1);

% Iterate over each image and preprocess it
for i = 1:num_images
    % Read the image
    image = imread(fullfile(image_dir, image_files(i).name));
    
    % Convert the image to grayscale
    image = rgb2gray(image);
    
    % Resize the image to 28x28 pixels
    image = imresize(image, [28 28]);
    
    % Normalize the image pixel values between 0 and 1
    image = double(image) / 255;
    
    % Save the preprocessed image in the MNIST data array
    mnist_data(:,:,i) = image;
    
    % Save the label for the image (0 to 9)
    mnist_labels(i) = rem(i-1, 10);
end

% Save the preprocessed images and labels in MNIST format
mnist_data = permute(mnist_data, [2 1 3]);
mnist_labels = categorical(mnist_labels);
mnist = struct('data', mnist_data, 'labels', mnist_labels);
save('mnist_base.mat', 'mnist');
1096 chars
36 lines

This code will preprocess your 10 images and save the resulting dataset in MNIST format as a .mat file named "mnist_base.mat". You can use this dataset to train and test machine learning models for image classification tasks.

gistlibby LogSnag