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

skit saves labels sketched by 3D slicer, but the positions of labels are inconsistent #7352

Open
tanjia123456 opened this issue Mar 19, 2024 · 2 comments
Labels
🐛 Bug ❓ Needs info Needs more information or investigation

Comments

@tanjia123456
Copy link

Description:

Hello, I use 3D slicer software to outline the subject dicom sequence and save it as seg.nii, and use dicom2nifti.dicom_series_to_nifti to save these sequences as volume.nii, and then I use the following code for preprocessing, The position of opening the label with 3D slicer is misaligned, what is the reason?

On the left is the processed data, and on the right is the raw data

Way to reproduce:

import numpy as np
import os
import SimpleITK as sitk
import random
from scipy import ndimage
from os.path import join

for i in range(20):
mri_path = 'process1/'+str(i)+'.nii.gz'
seg_path1 = 'process1/seg-'+str(i)+ '.nii'
seg_path2 = 'process1/seg-'+str(i)+ '.nii.gz'

mri = sitk.ReadImage(mri_path, sitk.sitkInt16)
mri_array = sitk.GetArrayFromImage(mri)

if os.path.exists(seg_path1):
    seg = sitk.ReadImage(seg_path1, sitk.sitkInt8)
    seg_array = sitk.GetArrayFromImage(seg)
else:
    seg = sitk.ReadImage(seg_path2, sitk.sitkInt8)
    seg_array = sitk.GetArrayFromImage(seg)
print("\nprocess1: seg_array=", seg_array.shape, "mri_array=", mri_array.shape)




# 找到肝脏区域开始和结束的slice,并各向外扩张
z = np.any(seg_array, axis=(1, 2))
print("z=", z.shape)
# np.where(z)[0] 输出满足条件,即true的元素位置,返回为一个列表,用0,-1取出第一个位置和最后一个位置的值
start_slice, end_slice = np.where(z)[0][[0, -1]]
print("process3: start_slice = ", start_slice, "end_slice = ", end_slice)


# 截取保留区域
mri_array = mri_array[start_slice:end_slice + 1, :, :]
seg_array = seg_array[start_slice:end_slice + 1, :, :]
print("Preprocessed shape:", mri_array.shape, seg_array.shape)


# 保存为对应的格式
new_mri = sitk.GetImageFromArray(mri_array)
new_mri.SetDirection(mri.GetDirection())
new_mri.SetOrigin(mri.GetOrigin())
#new_mri.SetSpacing((mri.GetSpacing()[0] * int(1 / self.xy_down_scale),ct.GetSpacing()[1] * int(1 / self.xy_down_scale), self.slice_down_scale))

new_seg = sitk.GetImageFromArray(seg_array)
new_seg.SetDirection(seg.GetDirection())
new_seg.SetOrigin(seg.GetOrigin())
# new_seg.SetSpacing((ct.GetSpacing()[0] * int(1 / self.xy_down_scale),ct.GetSpacing()[1] * int(1 / self.xy_down_scale), self.slice_down_scale))

if new_mri != None and new_seg != None:
    # sitk.WriteImage(new_ct, os.path.join(self.fixed_path, 'ct', ct_file))
    sitk.WriteImage(new_mri, "process2/"+str(i)+".nii.gz")
    sitk.WriteImage(new_seg, "process2/seg-"+str(i)+".nii.gz")

Version information:

No response

@tanjia123456
Copy link
Author

Uploading 1111111111111.png…

@lagru
Copy link
Member

lagru commented Mar 19, 2024

@tanjia123456, from your provided information it is hard to determine if this is a bug. In this case, could you try to simplify the code to an minimal example that uses only scikit-image and its dependencies to make it reproducable? That way we can make sure that the error lies with scikit-image.

By the way, I've noticed that your issues have some formatting issues. If you paste python code, please try to use a block like so?

```python
# python code goes here
```

@lagru lagru added the ❓ Needs info Needs more information or investigation label Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug ❓ Needs info Needs more information or investigation
Projects
None yet
Development

No branches or pull requests

2 participants