# Create an image stack (3-D matrix)

321 views (last 30 days)
Jack on 4 Jun 2013
Commented: Image Analyst on 15 Apr 2020
Hello. I'm importing a series of images from my working directory using the following code:
for i=139:141
% {} = cell array
I{i} = rgb2gray(images{i});
end
I have a large number of images, but here I'm importing just a sample. I'm converting each color image to grayscale. I must also create an image stack, a 3D matrix of grayscale images. I've been browsing the web for an answer, but I'm having difficulty understanding the process. size(I) yields '1 141', and I'm wondering how I can create a 3-D matrix from this. Your help would be greatly appreciated. Thank you!
joseph robinson on 19 Apr 2017
Adding to the feedback of others, your code would be more efficient without looping, e.g.,
idx = 139:141;
# image file paths
ipaths =cellfun(@(x) sprintf('IMG_%04d.JPG',x),idx,'uni',false);
# read images into cell array
# Convert to gray-scale
Igray = cellfun(@rgb2gray,I,'uni',false);
# Concatenate 3 gray-scale images into single 3D matrix
myImage = cat(3,Igray{:});
Hope this helps!

Kye Taylor on 4 Jun 2013
Edited: Kye Taylor on 4 Jun 2013
Execute this command after your code above to get the three gray-scale images you create into one 3D matrix of grayscale images;
myImage = cat(3,I{139:141});

Image Analyst on 4 Jun 2013
Edited: Image Analyst on 4 Jun 2013
The (badly-named) variable I is a one dimensional array of cells. Inside each cell it a 2D array. So, in effect, this is a 3D array though you can't reference it like I(row, column, slice). If that is what you need to do then you need to do
I = []; % Before the loop
Then in the loop:
I = cat(3, I, images{i}); % Tack on slice i into a new cell.
In the meantime, please read the FAQ on cells to get a better idea of how they work: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
Image Analyst on 22 May 2018
I'd assumed that you had gray scale images. They seem to be color and so creating a 4-D image like Walter showed should work. Or else convert the images to gray scale before using cat()

hf fh on 21 May 2018
Edited: Walter Roberson on 21 May 2018
Hi
I have a question
I used the same method but I want to extract it in size format volume like figure 3D
how it would be???
clear all
clc
folder = '/Users/mac/Desktop/fiel2/data';
D = '/Users/mac/Desktop/fiel2/data';
for i=1:8
% {} = cell array
I{i} = rgb2gray(images{i});
I = cat(3, I, images{i}); % Tack on slice i into a new cell.
end
Image Analyst on 15 Apr 2020
This works for me:
folder = pwd; % 'C:\Users\Suraj\Desktop\images\Results';
% for k = 109 : 433
% fullFileName = fullfile(folder, sprintf('000%d.jpg', k));
for k = 1 : 10
fullFileName = fullfile(folder, sprintf('%d.jpg', k));
if ~isfile(fullFileName)
continue;
end
fprintf('Storing image file #%d : %s\n', k, fullFileName);
% Store the original image, regardless of whether it's RGB or gray scale.
% Store the gray scale version of the image. Convert (if necessary) from RGB to gray scale.
if ndims(originalImages{k}) > 2
grayScaleImages{k} = rgb2gray(originalImages{k});
else
grayScaleImages{k} = originalImages{k};
end
end
Change the comments to have it use your images in your folder, and in the range 109 to 433. Better yet, see if you can just use dir() to get a list of only those images that definitely exist.

hf fh on 21 May 2018
Please help me .. I searched a lot for convert 2Dimages into 3D but I still have the same problem !!! ''I can not get the images in volume in three dimensions" I have a search in 20 image dimensions [773x896]pixel, I want to convert it to 3D-three dimensions in volume form to study the depth, I following steps this: First:
Must be converted to a size of [300 * 300] pixels Second:
Converted to stack Third:
Converted to format==> 3D
like :
folder = '/Users/mac/Desktop/fiel2/data/';
for i=1:20
images{i} = imread( fullfile(folder, sprintf('%d.tif',i) ));
Q{i}=imresize(images{i} ,[300 , 300]);
end
I = cat(3, Q{i});
imshow(Q{i});
view(3);
Thank you for help me ...
Image Analyst on 21 May 2018
Why do you want the images or Q cell arrays anyway? Chances are you don't. I never do that. I never create a cell array of a bunch of images and I think you most likely don't need to either. Why do you think you need to???

hf fh on 21 May 2018
Thank you for answering me... I'm a beginner in using MATLAB and I just want to convert 20 images from 2D-two to 3D-three dimensions only. The aim is to convert from 20 images to an only volume 3D Look for of a method to apply the images.
I following steps like this:
First: Must be converted to a size of [300 * 300] pixels
Second: Converted to stack==>2D[300*300*20]
Third: Converted to format==> 3D volume