Store image into array for classification training (SOM)

1 view (last 30 days)
Hi there, i currently have over 7000 images that i want to train for classifcation of certain characters. I tried loading them into a matrix but it ends up as a 3D array like this:
arrayImage 128x128x7112 double
clear,clc;
files = dir('*.png');
num = length(files);
arrayImage = zeros(128,128,num);
i = 1;
for file = files'
img = imread(file.name);
arrayImage(:,:,i) = img;
i = i + 1;
end
I think matlab's neural net clustering only reads in a 2D matrix. How can i store them as a 2D array so i can load them into SOM for training. I attached some examples of images i want to store and use for training.

Answers (1)

Umeshraja
Umeshraja on 11 Oct 2024
I understand you're preparing a large dataset of images for training a Self-Organizing Map (SOM) and need to convert your array of images into a 2D matrix.
Neural network clustering algorithms typically require input in a 2D array format (M x N), where M is the number of observations and N is the number of features. In your case, M would be 7112 (the number of images) and N would be 16384 (the result of reshaping each 128x128 image into a 1D array).
Here's how you can modify your code:
files = dir('*.png');
num = length(files);
% Initialize the 2D matrix
% Each row will represent one image, each column a pixel
arrayImage2D = zeros(num, 128*128);
for i = 1:num
% Read the image
img = imread(files(i).name);
% Ensure the image is in double format
img = im2double(img);
% Reshape the 2D image into a 1D vector and store it as a row in arrayImage2D
arrayImage2D(i, :) = reshape(img, 1, []);
end
% Now arrayImage2D is a 2D matrix where each row represents an image
% The size should be [7112, 16384] for your dataset
size(arrayImage2D)
This 2D format should be compatible with MATLAB's Neural Network Toolbox for training a Self-Organizing Map.
For more information on the reshape function, you can refer to the MATLAB documentation:

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!