How to store images in a single array or matrix
Show older comments
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
More Answers (5)
Azzi Abdelmalek
on 27 May 2013
f=dir('*.jpg')
files={f.name}
for k=1:numel(files)
Im{k}=imread(files{k})
end
12 Comments
Bashir
on 27 May 2013
Edited: Azzi Abdelmalek
on 27 May 2013
Azzi Abdelmalek
on 28 May 2013
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
Image Analyst
on 28 May 2013
Edited: Image Analyst
on 28 May 2013
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
Karen Bugeja
on 13 Jan 2015
how can I output the images stored in cellArrayOfImages? I am using imshow(cellArrayOfImages); and I'm getting an error. thanks!
Image Analyst
on 13 Jan 2015
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
preeti
on 1 May 2015
thanks its so helpful :):)
Venkatesh chari
on 14 Feb 2017
Edited: Image Analyst
on 14 Feb 2017
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?
Image Analyst
on 14 Feb 2017
Try this:
thisImage = cellArrayOfImages{23};
thisImage = double(rgb2gray(thisImage));
medFiltered = medfilt2(thisImage, [3,3]);
diffImage = someOtherImage - medFiltered;
Anjali S
on 27 Oct 2017
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 ..
habtamu miheretie
on 3 Oct 2018
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!
Abhishek Singh
on 2 Jul 2019
@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.
Shelly Smith
on 15 Feb 2017
Edited: Image Analyst
on 15 Feb 2017
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
Image Analyst
on 15 Feb 2017
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?
Lav Palve
on 21 Jan 2019
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
Vojtech Raska
on 13 Mar 2020
Edited: Vojtech Raska
on 13 Mar 2020
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
im = {'image1.png','image2.png','image3.png'};
for k = 1:length(im)
image1 = im2double(imread(im{k}));
% ......
end
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!