qurit / qurit/rt-utils

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

Open
#110 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
256
Forks
75
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the example using RTStructBuilder.create_new, add_roi, and save, then inspect how the NIfTI data loaded by nibabel is transformed before being passed as a mask. Compare the NIfTI orientation and CT reference geometry, using the 3D Slicer overlap as the expected result. Done means the generated RTStruct aligns with the original CT without ad hoc rotations.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
computer-vision
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.