[HELP] 2d images to 3d

2 views (last 30 days)
YJ
YJ on 14 Oct 2014
Commented: Geoff Hayes on 15 Oct 2014
I need help with constructing the multiple of 2d images into 3d.
So I got the 2D matrix (768 by 768)- image
and have 500 of them.
And I want to line them up so that I have 768 by 768 by 500 3D image
Its like line the 2D images in a deep-wise to create the 3D images.
So is there way to do this?
I think I need to load all images and don't know how to process them.
cd 'D:\data\Mean process shot\1250\ '; %2D images location
files = dir('*.mat');
for i=1:500 %number of 2D image file
load((['Process_shot_f',num2str(i),'.mat']));
end
so it will load all images, but.. how do I line them up to become a single 3D image?
Process_shot_f1
Process_shot_f2
Process_shot_f3
......
Process_shot_f500

Accepted Answer

Geoff Hayes
Geoff Hayes on 14 Oct 2014
YJ - if all images are of the same dimension, 768x768, then try the following
% pre-allocate (or pre-size) a matrix to store all 500 2D images
numImages = 500;
allImages = uint8(zeros(768,768,numImages));
% change directory to the folder where there files are
cd 'D:\data\Mean process shot\1250\ '; %2D images location
% load the images
for k=1:numImages
% construct the file name
filename = ['Process_shot_f' num2str(k) '.mat'];
% load the data from file
dataStruct = load(filename);
% save the data to the 3D array
allImages(:,:,k) = dataStruct.(char(fields(dataStruct)));
end
Since the load function returns a struct, then data is a struct whose fields are all the variables that have been saved to the mat file. If we assume that there is just one variable per file, the 768x768 image, then we can use the fields function to return the fields of data, convert it to a string via char, and then use it to access that image from data as
data.(char(fields(data)))
We then save that 2D image to the 3D array, in the kth slot.
Note that the code also assumes that each image is of type uint8. If this is not the case, then you can just replace the line
allImages = uint8(zeros(768,768,numImages));
with the equivalent "cast" in place of uint8.
  2 Comments
YJ
YJ on 15 Oct 2014
wow, it works nicely, I just changed unit8 to double :) well explained and really easy to follow Thanks again
Geoff Hayes
Geoff Hayes on 15 Oct 2014
Glad it worked out!

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!