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

Segmentation.to_label_image() issue when specifying ROI name #200

Open
JoshuaSiraj opened this issue Jan 28, 2025 · 3 comments
Open

Segmentation.to_label_image() issue when specifying ROI name #200

JoshuaSiraj opened this issue Jan 28, 2025 · 3 comments

Comments

@JoshuaSiraj
Copy link
Collaborator

JoshuaSiraj commented Jan 28, 2025

When I try something like:

mask_scan = read_dicom_auto(mask_path)
mask = mask_scan.to_segmentation(example_image, ["GTVp"]).to_label_image()

I get the following error:

{
	"name": "RuntimeError",
	"message": "Exception thrown in SimpleITK Image_CopyInformation: /tmp/SimpleITK/Code/Common/src/sitkImage.cxx:348:
sitk::ERROR: Source Image for information does not match this image's dimension.",
	"stack": "---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_34497/2353237833.py in ?()
      1 mask_scan = read_dicom_auto(mask_path)
----> 2 mask = mask_scan.to_segmentation(example_image, [\"GTVp\",]).to_label_image()
      3 
      4 visualize_3d_slices_adaptive(mask, axis=0, step=5, cmap=None)

~/Documents/BHKLAB/med-imagetools/src/imgtools/modules/segmentation.py in ?(self)
    324             The label image with each voxel assigned the label of the ROI with the highest value.
    325         \"\"\"
    326         arr, *_ = image_to_array(self)
    327         label_arr = np.where(arr.sum(-1) != 0, arr.argmax(-1) + 1, 0)
--> 328         label_img = array_to_image(label_arr, reference_image=self)
    329         return label_img

~/Documents/BHKLAB/med-imagetools/src/imgtools/utils/imageutils.py in ?(array, origin, direction, spacing, reference_image)
     36         The resulting SimpleITK image.
     37     \"\"\"
     38     image = sitk.GetImageFromArray(array)
     39     if reference_image is not None:
---> 40         image.CopyInformation(reference_image)
     41     else:
     42         image.SetOrigin(origin)
     43         image.SetDirection(direction)

~/Documents/BHKLAB/med-imagetools/.pixi/envs/dev/lib/python3.12/site-packages/SimpleITK/SimpleITK.py in ?(self, srcImage)
   3462         exception will be generated.
   3463 
   3464 
   3465         \"\"\"
-> 3466         return _SimpleITK.Image_CopyInformation(self, srcImage)

RuntimeError: Exception thrown in SimpleITK Image_CopyInformation: /tmp/SimpleITK/Code/Common/src/sitkImage.cxx:348:
sitk::ERROR: Source Image for information does not match this image's dimension."
}

When I specify multiple roi_names like:

mask = mask_scan.to_segmentation(example_image, ["GTVp", "Mandible_Bone"]).to_label_image()

I get an empty label image.

I do get a proper mask when I don't specify rois:

mask = mask_scan.to_segmentation(example_image).to_label_image()
@JoshuaSiraj
Copy link
Collaborator Author

Interestingly, this produces an empty mask:

mask = mask_scan.to_segmentation(example_image, roi_names=['Mandible_Bone'])

And this does not:

mask = mask_scan.to_segmentation(example_image, roi_names={"Man":"Mandible_Bone.*"})

@jjjermiah
Copy link
Contributor

if i was to refactor to_label_image what exactly should it do ?

would be helpful to create some test cases to make sure

@JoshuaSiraj
Copy link
Collaborator Author

if i was to refactor to_label_image what exactly should it do ?

would be helpful to create some test cases to make sure

Currently mask = mask_scan.to_segmentation(example_image) returns a mask with a channels dimension.
to_label_image returns that without a channels dimensions and all the rois in one 3d image.

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

2 participants