First, I created a segment and converted it into a model in slicer.
Second, I have written an external script purely based on VTK, which does not rely on Slicer classes. The script reads DICOM files using vtkDICOMImageReader, applies a threshold using vtkImageThreshold, converts it to a model using VTK’s marching cubes, and finally exports it using vtkSTLWriter.
Now, I imported the STL generated by the script, into Slicer to compare it against the model generated earlier from the segment. However, I am unable to compare their contours in 2D because my external STL is not aligned with the model generated by Slicer. In the 3D scene, both models are far apart and do not lie on the same origin. I don’t want to use the Transform module because of its manual nature. I want to simulate, how slicer internally does transformations, within my script so that when I export any STL from my script and import it into Slicer, it has the correct transformations and aligns with the 3D Slicer-generated model.
I have gone through it, and I was wondering how I can utilize the exact transformation matrix of Slicer. I am setting the labelmap’s origin and spacing to be the same as the one given in the CreateClosedSurface function of the vtkBinaryLabelmapToClosedSurfaceConversionRule class.
I’ve been stuck on this problem for many days. I would appreciate further help.
Here’s a picture of the models for reference (the green model is from Slicer, and the yellow one is generated from the script):
I tried loading the STL by selecting both coordinate systems, but it didn’t work, as the result is still the same. I also modified my script to include the coordinate tag, and when I loaded the STL again, there was no luck.
vtkDICOMImageReader only works for special cases. I would not recommend spending any time with trying to debug why the image is not loaded correctly with vtkDICOMImageReader. If you are looking for a pure VTK-based solution then your best option is GitHub - dgobbi/vtk-dicom: A set of classes for using DICOM in VTK.. It is still very limited but works for majority of basic image acquisitions. Make sure you take into account the PatientMatrix provided by the reader.
If you want to reconstruct volumes from more complex acquisitions then you can use dcm2niix or Slicer. If you want an open-source solution to read not just images but other important DICOM objects then probably your only choice is Slicer (or implement yourself from pydicom level).