Issue with dispaly of 3D images created from multiple 2D slices

13 views (last 30 days)
Hi,
I have multiple 2D grayscale image slices (all of them are of same dimensions). I need to stack them to dispaly 3D image. I tried cat function and then implay. But this just plays video, I am not able to see 3D image.
I came across many threads relevant to this. But I am not sure which one will serve the purpose.
I have attached few 2D grayscale slices here. Could someone please suggest the possible method to get 3D reconstruction from these images?

Accepted Answer

Anjaneyulu Bairi
Anjaneyulu Bairi on 22 Jan 2025
To effectively stack multiple 2D images and visualize them as a 3D image, follow these steps:
Image Acquisition:
  • Sequentially read each 2D image and store them within a 3D matrix. This matrix acts as a volumetric representation where each slice corresponds to an individual image.
Volume Visualization:
  • Utilize the "volshow" function on the constructed 3D matrix. This function facilitates the rendering and interactive visualization of the volumetric data, allowing for comprehensive analysis and exploration of the 3D image.
  • Alternatively you can use "sliceViewer" for visualization.
Refer the below code for reference:
imageFolder = 'images_dir'; % Specify your image directory
imageFiles = dir(fullfile(imageFolder, '*.png'));
numSlices = length(imageFiles);
% Read the first image to get dimensions
sampleImage = imread(fullfile(imageFolder, imageFiles(1).name));
% Check if the image is RGB and convert to grayscale if necessary
if size(sampleImage, 3) == 3
sampleImage = rgb2gray(sampleImage);
end
[rows, cols] = size(sampleImage);
% Initialize a 3D matrix
volumeData = zeros(rows, cols, numSlices, 'like', sampleImage);
for k = 1:numSlices
imageName = fullfile(imageFolder, imageFiles(k).name);
img = imread(imageName);
% Convert to grayscale if the image is RGB
if size(img, 3) == 3
img = rgb2gray(img);
end
volumeData(:, :, k) = img;
end
% Visualize the 3D volume
volshow(volumeData);
% Using sliceview to visualize
sliceViewer(volumeData);
Output of "volshow" function looks like:
Hope it helps!
  1 Comment
MechenG
MechenG on 23 Jan 2025
Thank you very much!. Actually these are focal stack images. I guess I need to follow different strategy?

Sign in to comment.

More Answers (1)

Mathieu NOE
Mathieu NOE on 22 Jan 2025
For a 3D rendering (like MRI) I suggest you try this : vol3d v2 - File Exchange - MATLAB Central
%% create a 3D rendering
out = [];
for k=1:5
img = rgb2gray(imread(['0' num2str(k) '.png']));
out = cat(3,out,img);
end
h = vol3d('cdata',out,'texture','3D');
view(3);
axis tight; daspect([1 1 .4])
alphamap('rampup');
alphamap(.06 .* alphamap);
  5 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!