How to slice 3D image and save all the 2D image in one folder

Hi, I’m doing image segmentation for 3D image. I would like to slice my 3D image and save it to a file so that I could construct a dataloader.
There is some way in Pytorch to save the tensors in a folder?
Thank you in advance
Is this code below will solve my problem?

torchvision.utils.save_image(tensor, filename, nrow=8, padding=2, normalize=False, range=None, scale_each=False, pad_value=0)

In this case, all the tensors will be saved as PIL images.

Depending on your data you might want to use another lib, as e.g. slicing and saving medical images might be easier using some other libraries.
Could you explain your dataset a bit so that we could check which approach might be the easiest?

Hi ptrblck, yes I’m dealing with medical images, there is nibabel for the nifti image, I have sliced them to images and save them in the folder in my computer. Because I think if I could do the segmentation of the image slices seperately and then combine them together, it’s ok. Maybe in the last step, I will reestablish the nifti image again using the nibabel package, I’m not sure if it’s a good idea.

You should save them to numpy instead of png or jpg. Because numpy will mantain for image type (i.e. float, uint16…) and information.

Should be generally a valid approach.
The last step of combining the segmented pieces might be a bit tricky, since the borders might not be smooth.
So I understand you’ve already been successful in slicing the images?

Yes, the slicing is already done.
What do you mean the borders might not be smooth? could you explain?

1 Like

Hi, If I save them to numpy, I don’t know which type of file i should save to, do you have some suggestions?

I’m not sure if that will be a problem at all in your use case, but I’ve had some issues with stitching smaller image patches into a larger image, since the borders of the small patches did not smoothly fit together.
Anyway, that’s probably an issue to worry about a bit later and your approach still seems valid to me. :slight_smile:

Depend on your image. For example. if your image type of float then save it with type of np.float32…It will keep your image information. YOu can not save it to png because it convert float to uint8 and many information will be lost

Hi, I’m dealing with MRI image, so the original type is nifti, if the original image value is in uint8, I think it won’t influence the quality of my image since I have 182 sliced images in axial plane, coronal plane and sagittal plane respectively.

Problem is that the slice is gray image, when you save to png, you have to convert to RGB by copy three time the first channel. You can select numpy as array or png. Just try it

Hi, according to the paper I read quickNAT
they hey are using a probability representation to select the final largest proba value to represent the predicted probability vector for axial, coronal and sagittal views respectively. And l think I will use the same strategy. :yum:

1 Like

My original 3d image is 3 dimensional, so I suppose they are all gray images when I do the slices, and I have saved them to .jpg image. Is this a problem too?

I often save it to numpy because I process normalization before

Hi john, I would like to try your suggestion, but could you tell me how to save my 182 images to numpy and put it to dataloader, because I need to load it. So I don’t know if save them to numpy, do I need some SQL ? could you show me one example?

Very simple'slice1.npy', data)   
data = np.load('slice1.npy')

for dataloader, you can write custom dataloader to read all file with endwith .npy

1 Like

Thank you. I’ll try this.:smile: