NIfTI to RTStruct - no registration with CT (rotation problem?)
balduz94 opened this issue · 0 comments
balduz94 commented
Hello, I am having problems with this code: I have many structures in NIfTI format that, once I create the RTStruct file, do not correctly overlap with the original CT. Even applying rotations as suggested by other users, I cannot achieve a good overlap with the CT. However, if I open these structures directly with 3D Slicer, they overlap perfectly, so it is not a segmentation issue. I am leaving the code here hoping for some help. Thank you, Emanuele.
from rt_utils import RTStructBuilder
import nibabel as nib
import numpy as np
import os
import colorsys
def generate_unique_colors(n):
colors = []
for i in range(n):
hue = i / n
saturation = 0.7 # Scegli un valore di saturazione medio-alto
value = 0.9 # Scegli un valore di luminosità medio-alto
rgb = colorsys.hsv_to_rgb(hue, saturation, value)
colors.append([int(x * 255 + 0.5) for x in rgb])
return colors
folder_path = r"\path_folder_with_nifti_segmentation"
dicom_series_path = r"\path_CT_reference"
rtstruct = RTStructBuilder.create_new(dicom_series_path)
nii_files = [f for f in os.listdir(folder_path) if f.endswith('.nii.gz')]
unique_colors = generate_unique_colors(len(nii_files))
for file, color in zip(nii_files, unique_colors):
nii_path = os.path.join(folder_path, file)
nii_image = nib.load(nii_path)
data = nii_image.get_fdata() > 0
mask_dataflip = np.flipud(data)
mask_dataflip2 = np.flip(mask_dataflip)
mask_rtdata = np.rot90(mask_dataflip2)
roi_name = os.path.splitext(file)[0]
rtstruct.add_roi(mask=mask_rtdata, color=color, name=roi_name)
rtstruct.save(r'\path_RTStruct_output\new-rt-struct.dcm')