I’ve read through multiple online forums to figure out, how to stack XYZ 2D stack images into 3D volume. Hardly was able to find an answer. I went through this stackoverflow post: How to create a 3D image with series of 2D Image
Which was constructive and provided some ideas, unfortunately when I tried the library
I was unable to receive the adequate answer I was looking for. That’s why, after reading through documentations and forums, I decided to post this question on here.
I have multiple XYZ dimension 2D images and I want to utilise them to create a 3D Volume. And I want to use meshlib
library for that (if some other library works, that’s fine too)
this is how my images look
enter image description here
this is not a full stack of the mice brain scans I’m working with rather a very small subset of it. It’ll be helpful if someone can help me with some examples + guiding me to the right direction.
Initially, I used SimpleITK library and it looks like this:
import os
import SimpleITK as sitk
import cv2
import numpy as np
folder_path = "sample"
file_names = os.listdir(folder_path)
file_names.sort()
image_list = []
for file_name in file_names:
image_path = os.path.join(folder_path, file_name)
# Use OpenCV to read the TIFF image
image_cv2 = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
image = sitk.GetImageFromArray(image_cv2)
image_list.append(image)
volume = sitk.JoinSeries(image_list)
output_filename = "stacked_volume.nii.gz"
output_path = os.path.join(os.getcwd(), output_filename)
sitk.WriteImage(volume, output_path)
this code resulted in a merged 2D image, when I visualised the resulted file using Pyvista
library. I used Pyvista
library to visualise the file, generated from the meshlib library as well. Both resulted in similar results.
I want to stack those 2D images in a way it creates a 3D Volume. (I’m aware I have to register those images as well. But, right now, I’m not looking help in that)
this is the code for meshlib library I used
import meshlib.mrmeshpy as mr
settings = mr.LoadingTiffSettings()
# load images from specified directory
settings.dir = "C:/slices"
# specifiy size of 3D image element
settings.voxelSize = mr.Vector3f(1, 1, 5)
#create voxel object from the series of images
volume = mr.loadTiffDir(settings)
#define ISO value to build surface
iso=127.0
#convert voxel object to mesh
mesh=mr.gridToMesh(volume, iso)
#save mesh to .stl file
mr.saveMesh(mesh, mr.Path("C:/slices/mesh.stl"))
How to create a 3D image with series of 2D Image
Absurd Cat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.