Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NIfTI to RTStruct - no registration with CT (rotation problem?) #110

Open
balduz94 opened this issue Apr 24, 2024 · 0 comments
Open

NIfTI to RTStruct - no registration with CT (rotation problem?) #110

balduz94 opened this issue Apr 24, 2024 · 0 comments

Comments

@balduz94
Copy link

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')

@balduz94 balduz94 changed the title NiTi to RTStruct - no registration with CT (rotation problem?) NIfTI to RTStruct - no registration with CT (rotation problem?) Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant