How to store images in a single array or matrix

Hello, i have a number of jpeg images stored in a folder; how can i read all the images and store them as a single matrix or array (reserving the image format).

 Accepted Answer

Straight out of the FAQ:
myFolder = 'C:\Documents and Settings\yourUserName\My Documents\My Pictures';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
end
Obviously, you can adapt as needed, because I'm not sure what you mean when you say "store them as a single matrix". There is a montage() function that will do that if that's what you want. It will stitch together all the image files into one giant image.

16 Comments

Thank you very much, this is really helpful. what i mean is that i want to store the images in a single matrix(as jpeg images not uint8) so that i can read them later for further processing. if this is not possible, how do i extract the uint8 images stored in a cell array to be processed(as normal jpeg images) for feature extraction. thank you once more.
Do you mean to store all your images in a single matrix or each matrix in each own matrix?
Bashir - that doesn't make sense. Once imported into MATLAB, your image will be uint8 - and whatever format it had while stored on disk doesn't matter anymore. Likewise, in MATLAB, your image is simply a uint8 array, and it doesn't matter how it's stored. No matter what format it's stored in, you can recall it back into MATLAB as a uint8 image. Though I recommend PNG rather than JPEG so that you don't lose any information due to JPEG's lossy compression process.
To extract anything from a cell array's cell, use braces. You will find the FAQ on cell arrays very informative.
it is now clear to me, thank you for this useful information. hope to get your response in my subsequent questions.
How can i save all the images stored in a cell array in the matlab directory path,so that i can use them further.What is the syntax of writing multiple images from cell array to the system in jpg format?please help.
for i = 1:numel(myCellArr)
fileName = sprintf('image_%04d.jpg');
imwrite( myCellArr{i}, 'fileName.jpg' );
end
I have used this code but it is not saving all of my images stored in cell but only the last image.'please help me to obtain the desired result.
They're all being saved in the file called 'fileName.jpg' because you hard coded in the string instead of passing in the variable. You need to do this:
for i = 1:numel(myCellArr)
fileName = sprintf('image_%04d.jpg');
imwrite(myCellArr{i}, fileName);
end
Thanks for correcting me image analyst but now it is giving me the error as 'unable to determine the file format from fileName'. how can i remove this error?And my cell array is 'g'.
You need to put i into the sprintf, and replace myCellArr by (the poorly-named) "g":
for i = 1:numel(g)
fileName = sprintf('image_%04d.jpg', i);
imwrite(g{i}, fileName);
end
Yes its working. Thsnk you very much sir.
Hi. I have used the same code for reading images..Saved it in imageArray The dimensions of imageArray is 243x320 but my images are 165 only. How can i use this array for clustering. I mean shouldn't be that one image represents one row of an array ? please provide some insight Thanks
What are the features that you want to identify clusters for? In other words, what are you measuring in the images? If you plot a scatterplot, what features go along the axes?
Excellent answer! Thank you!
@Image Analyst i have the same question i need to input 20 images and extract features for all of them such as entropy energy and then store these features to `use it train a classifier but when i store them i have bad results in the soms nn
If you have the same question, see my answer to the same question. And I don't know much if anything about self organizing map neural networks.
One thing to look out for with montage() is that it requires all of the pictures to be of the same dimensions. But thanks anyways!
Another thing that could be useful is imageSet() Maybe this could work.

Sign in to comment.

More Answers (5)

f=dir('*.jpg')
files={f.name}
for k=1:numel(files)
Im{k}=imread(files{k})
end

12 Comments

I very much appreciate your fast answer,but i am having an error msg: Error in ==> multireadIm at 5
Im{k} = imread(files{k}); pls what is wrong with this line code.
Thank you
Check if your files are in the current folder, if not change the code to:
f=dir('folder\*.jpg') %'folder' is the folder containing your jpg files
files={f.name}
for k=1:numel(files)
Im{k}=imread(files{k})
end
You still need to construct the full filename (as shown in the FAQ) since files{k} is only the base file name and you'll have the same problem.
folder = 'D:\My Pictures\whatever'
filePattern = fullfile(folder, '*.jpg');
f=dir(filePattern)
files={f.name}
for k=1:numel(files)
fullFileName = fullfile(folder, files{k})
cellArrayOfImages{k}=imread(fullFileName)
end
how can I output the images stored in cellArrayOfImages? I am using imshow(cellArrayOfImages); and I'm getting an error. thanks!
For example, to display the 23rd image, do
imshow(cellArrayOfImages{23});
the braces mean "Contents of". Please see the FAQ: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
thanks its so helpful :):)
folder = 'D:\My Pictures\whatever'
filePattern = fullfile(folder, '*.jpg');
f=dir(filePattern)
files={f.name}
for k=1:numel(files)
fullFileName = fullfile(folder, files{k})
cellArrayOfImages{k}=imread(fullFileName)
end
After this, I need to process a specific image. Let's say for e.g. image 23. How can I crop it, convert it to grayscale & double, add median filter to it, and subtract from some other image?
Can you give me syntax for these operations?
Try this:
thisImage = cellArrayOfImages{23};
thisImage = double(rgb2gray(thisImage));
medFiltered = medfilt2(thisImage, [3,3]);
diffImage = someOtherImage - medFiltered;
I am facing the same problem. I tried above code so my all images are now stored in matrix having size 1*6. Every cell is storing size of image (1*48000 due to resize)But I want every row should store actual bits of image and not the size. That is I want to store each image as a one row for creating training dataset. Ex my image is of 200*240 so I resized to 1*48000 . First row of the matrix should have 48000 columns like that matrix size will be 6 * 48000. I REQUEST KINDLY HELP ME ..
hey man, i have exactly the same problem. I have many images and want to make a matrix out of it. Have you solved your problem? if so can you share what you did? thanks in advance!
[MOVED from flags] UKM wrote:
Very simple and Very perfect>> Thanks alot.
@UKM: Please use flags only to inform admins and editors about inappropriate content like sopam or rudeness.
@Image Analyst After I have the array for the images can I compare those images without using loop? I need to compare two images using ssim() which was suggested by you only but I think loop is very slow in Matlab I was looking for something in arrays and vectors which could help me.

Sign in to comment.

Hey
I have a problem and will be grateful if someone help me. I am reading 200 images through this code:
folder = 'C:\Users\whatever'
filePattern = fullfile(folder, '*.pgm');
f=dir(filePattern)
files={f.name}
for k=1:numel(files)
fullFileName = fullfile(folder, files{k})
cellArrayOfImages{k}=imread(fullFileName)
imshow(cellArrayOfImages{k});
end
I get a row vector with 200 length. In each cell there's a 50x50 uint8 image. I need to have the elements value of 50x50 image as well. So I want to basically read all the images (200 images) and stored the value of all images in a matrix. Can anyone tell me how can I do that? Thanks in advance.

1 Comment

I don't understand the question. You are reading in 200 images, which are 50 x 50 uint8 images, and storing them. It seems to be doing what you are asking for, so what's the problem? Do you want a 2-D array of cells instead of a 1-D row vector of cells? If so, why? And how many rows and columns do you want?

Sign in to comment.

Store images in cell array
clc;
clear all;
close all;
arr = cell(25,77); % Create Cell array
k = 1;
% get 'zero' 25 image cell array
img_folder = ('E:\4th year\Project\Images\Chardatabase\Sample0'); % Enter name folder and its path
filenames = dir(fullfile(img_folder,'*.jpg')); % Read all image with specified extantion
Total_image = numel(filenames); % Count total image
for i=1:Total_image
j = 1;
for j=1:77
f = fullfile(img_folder,filenames(i).name); % Stroe ith image path
Output = imread(f); % read image
Output = imresize(Output,[11 7]);
Output = im2bw(Output);
Output = reshape(Output,[],77); % cell array divide by '77'
Output = im2double(Output);
arr{k,j} = Output(1,j); % get all pixel value of 'Output' image
end
k = k+1;
end
Hi, i´m using this code to make 3-D matrix of 2-D MRI images, but if I load more than 3 images that every image besides firts three and the last one is just black and all values of that matrices are zeros. The images are uint8 format. I tried to trasfer them to double but it didn´t work. Is there some solution or the image matrices can be only three for each other?
clc; clear all; close all;
[Name, Path] = uigetfile('*.*', 'All Files (*.*)','MultiSelect','on');
if ~iscell(Name)
Name = {Name};
end
%% matrix
for i = 1:1:length(Name)
Nazev = Name(1,i);
Nazev = char(Nazev);
Img_info = [Path Nazev];
Img = imread(Img_info);
Img(:,:,i) = rgb2gray(Img);
end
Shreyas S
Shreyas S on 8 Nov 2020
Edited: DGM on 12 Feb 2023
im = {'image1.png','image2.png','image3.png'};
for k = 1:length(im)
image1 = im2double(imread(im{k}));
% ......
end

Asked:

on 27 May 2013

Edited:

DGM
on 12 Feb 2023

Community Treasure Hunt

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

Start Hunting!