필터 지우기
필터 지우기

How can I extract and save images from 3D stack images (512x1000x100 double) from a .mat files?

조회 수: 5 (최근 30일)
I am trying to extract images and save individual images from a .mat file. The .mat file also consist of a strauct with three filed [images(512x1000x100 double), layerMaps(100x1000x3 double) and age (78)].
At present, I can only show the 100 images by using the following codes:
s=load ("D:\Matlab\1.mat");
D=s.images;
vol = squeeze(D);
[x,y,z] = size(D);
for i=1:z
sliceZ = vol(:,:,i);
cla; % Prevent stuffing too many images into the axes.
imshow(sliceZ, []);
drawnow;
pause(0.25); % Pause for 1/2 second before next frame blows it away.
end
But I can not save those images as individual files and also fail to relate the layerMaps and age with those images. Please share your knowledge in this regards.
  댓글 수: 3
Chinmay Bepery
Chinmay Bepery 2023년 10월 22일
The .mat file contains a srtuct. There are 100 images. Only show those images using above code.
I fail to save those extracted images.
I am also in dark, how to relate the layerMaps with those images of the struct.
Walter Roberson
Walter Roberson 2023년 10월 22일
The code in my Answer extracts into separate files.
As you have not given any context, I do not have any guess about what the layerMaps might be for.

댓글을 달려면 로그인하십시오.

채택된 답변

Chinmay Bepery
Chinmay Bepery 2023년 10월 23일
편집: Chinmay Bepery 2023년 10월 23일
Thank you @Walter Roberson for your kind support. I saved all 100 images as separate files as 0001.jpeg, 0002.jpeg. . . . .0100.jpeg by using imwrite(). Th The code as below.
s=load ("D:\Matlab\1.mat");
vol = squeeze(images); %The .mat file consist a struct with element name images (512*1000*100)
[x,y,z] = size(images);
for i=1:z
sliceZ = vol(:,:,i);
% map = lmp(:,:,i);
cla; % Prevent stuffing too many images into the axes.
imshow(sliceZ, []);
drawnow;
tName= num2str(i, '%04d');
startingFolder = "D:\Matlab\mat\"; % Or "pwd" or wherever you want.
defaultFileName = strcat(startingFolder, tName,".jpg");
imwrite(sliceZ, colormap('gray'), defaultFileName);
pause(0.25); % Pause for 1/2 second before next frame blows it away.
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 10월 23일
Could you show us
[smallestimg, largestimg] = bounds(vol, 'all')
[smallestdiff, largestdiff] = bounds(diff(unique(vol)))
[smallestlm, largeslm] = bounds(s.LayerMaps, 'all')

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 10월 22일
s=load ("D:\Matlab\1.mat");
outdir = 'D:\Matlab';
D = s.images;
LM = s.layerMaps;
A = s.age;
z = size(D,3);
for i=1:z
outfile = fullfile(outdir, "image_" + i + ".mat");
savestruct.image = D(:,:,i);
savestruct.layerMap = squeeze(LM(i,:,:));
savestruct.age = A;
save(outfile, "-struct", "savestruct");
end
This will create image_1.mat image_2.mat and so on in the directory named in outdir . Each of the .mat will contain three variables -- "image", "layerMap", and "age" .
  댓글 수: 2
Chinmay Bepery
Chinmay Bepery 2023년 10월 23일
Thank you for kind suggestion. Separate .mat files are saved. Actually, age is single value and it is common for all images in the input .mat file. Only images and layerMaps are related. Is it possble to save the seperated files in any imgae format using images and layerMap.
Walter Roberson
Walter Roberson 2023년 10월 23일
You can imwrite() but they are individually 512 x 1000 . Is that intensity information to be written in grayscale? Or should the layerMaps(100x1000x3 double) be understood to be a 1000 x 3 colormap specific to each image? If it is a per-image colormap then is each image integer values in the range 0 to 999 (or 1 to 1000) that should be understood as the color index?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Import, Export, and Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by