[HELP] 2d images to 3d
4 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Geoff Hayes
2014-10-14
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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!