How to convert stl to nii

Dear all:
I’m trying to convert stl file to nii for the medical image segmentation, however, when I use the method in the link :
[Converting .stl files to binary label maps in .nii format using Python - Support - 3D Slicer Community](https://), but the generated mask didn’t match the original image very well.

When I use the similar method in Mimics, the mask matched the image:

Here is the code that I use:


import os
import slicer

stl_file_name = "D:/PengChen/zhongshan/CFDPCNNmd/stls/1.stl"
output_file_name = "D:/PengChen/zhongshan/CFDPCNNmd/stl_niis/1.nii.gz"
reference_volume_path = "D:/PengChen/zhongshan/CFDPCNNmd/images/1.nii.gz"

referenceVolumeNode = slicer.util.loadVolume(reference_volume_path)
segmentationNode = slicer.util.loadSegmentation(stl_file_name)  ## stl
outputLabelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentationNode, outputLabelmapVolumeNode, referenceVolumeNode)
slicer.util.saveNode(outputLabelmapVolumeNode, output_file_name)


Using the above code, I can generated the file but it only has the background, not the binary labelmap.

I’m looking forward to get the correct method, thanks a lot.

First of all we need to know where do you get the STL from. If you produce it in Slicer, the answer will be quite different from if you get it externally.

I assume you are talking about the voxelization error, meaning that the voxels are too large to fit well the smooth STL boundaries. Do I understand correctly?