How to get 2d slices from a 3D image.
30 views (last 30 days)
Show older comments
How to get 2D slices from a 3D image? The image is grayscale, it is a .mhd file and the dimensions of the Image matrix after loading into matlab are 179x200x159 single.
Answers (3)
Chunru
on 20 Jun 2021
From 'doc slice':
[X,Y,Z] = meshgrid(-2:.2:2);
V = X.*exp(-X.^2-Y.^2-Z.^2); % replace this with your 3D data
xslice = [-1.2,0.8,2]; % slices along x
yslice = [];
zslice = 0; % slices along y
slice(X,Y,Z,V,xslice,yslice,zslice)
% if you don't hnow X, Y, you can use index value along x and y axes.
0 Comments
r r
on 21 Nov 2021
%https://www.mathworks.com/matlabcentral/answers/403570-how-to-make-a-3d-volume-out-of-a-2d-tiff-stack
%Rafael S.T. Vieira on 1 Jun 2020::How to make a 3D Volume out of a 2D Tiff stack?
fileFolder = '/Users/Desktop/Slice3D';
filePattern = fullfile(fileFolder, '*.jpg');
all_jpg = dir(filePattern);
first_image = imread(all_jpg(1).name);
[W,H] = size(first_image);
D = numel(all_jpg);
stack = zeros(W,H,D);
stack(:,:,1) = first_image;
for i = 1:D
stack(:,:,i) = imread(all_jpg(i).name);
% uncomment next line for seeing the reading progress
% disp(string(i*100.0/D) + "%");
end
% The app volumeViewer will handle the visualization
volumeViewer(stack);
1 Comment
Walter Roberson
on 21 Nov 2021
I would suggest the for loop should be 2:D to avoid re-reading the first image.
r r
on 21 Nov 2021
Edited: Walter Roberson
on 27 Nov 2021
can you help me
What do you suggest to me , plaeas help me .
I have 30 images converted from 2D to 3D
I used in a methode
and using sliceViewer for convert images to save by *gif
s = sliceViewer(stack); %View the data in the slice viewer.
hAx = getAxesHandle(s); %Get the handle of the axes containing the displayed slice.
filename = 'Slice.gif';%Specify the name of the GIF file you want to create.
sliceNums = 1:24;
for idx = sliceNums
% Update slice number
s.SliceNumber = idx;
% Use getframe to capture image
I = getframe(hAx);
[indI,cm] = rgb2ind(I.cdata,256);
% Write frame to the GIF file
if idx == 1
imwrite(indI,cm,filename,'gif','Loopcount',inf,'DelayTime', 0.05);
else
imwrite(indI,cm,filename,'gif','WriteMode','append','DelayTime', 0.05);
end
end
Now I want to insert it in 3D to compare it with another 3D model in Matlab? I find it difficult
1 Comment
Walter Roberson
on 27 Nov 2021
%gif animations are pseudocolored, not RGB.
[slices, cmap] = imread(filename, 'index', 'all');
rgbslices = zeros(size(slices,1), size(slices,2), 3, size(slices,4), 'like', slices);
for K = 1 : size(slices,4)
rgbslices(:,:,:,K) = ind2rgb(slices(:,:,:,K), cmap);
end
Now rgbslices will be rows x columns x 3 x slices .
I would suggest to you that there are easier ways to do this then to use sliceViewer() and write to a file and read back from the file.
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!