如何使用Python删除Nifti图像每个切片中的某些部分?

bqujaahr  于 2023-10-14  发布在  Python
关注(0)|答案(2)|浏览(132)

我有一个形状为(512,512,299)的Nifti图像(未标记),我想对这个3D图像执行无监督分割。然而,图像包含床区,这是我的集群制造麻烦。我尝试过聚类,结果是一个形状为**(512,512)**的聚类图像

djmepvbi

djmepvbi1#

试试这个:

  1. import numpy as np
  2. # Load the Nifti image
  3. nifti_file = 'your_image.nii.gz'
  4. img = nib.load(nifti_file)
  5. data = img.get_fdata()
  6. # Define a function to remove the bed area from a single 2D slice
  7. def remove_bed_area(slice_data):
  8. # Implement your logic here to remove the bed area
  9. # You might use thresholding, segmentation, or other techniques
  10. # For example, you can threshold the slice to remove dark areas
  11. threshold = 100 # Adjust this threshold as needed
  12. slice_data[slice_data < threshold] = 0
  13. return slice_data
  14. # Process each slice in the 3D image
  15. for slice_idx in range(data.shape[2]):
  16. data[:, :, slice_idx] = remove_bed_area(data[:, :, slice_idx])
  17. # Create a new Nifti image with the modified data
  18. modified_img = nib.Nifti1Image(data, img.affine)
  19. # Save the modified Nifti image to a new file
  20. modified_file = 'modified_image.nii.gz'
  21. nib.save(modified_img, modified_file)
展开查看全部
goqiplq2

goqiplq22#

也许尝试

  1. import nibabel as nib;
  2. img = nib.load ( "withbed.nii.gz" );
  3. data = img.get_fdata();
  4. removed_bed_area = data [ :200, 4:111, : ];
  5. removed_bed_img = nib.Nifti1Image( removed_bed_area, img.affine );
  6. nib.save( removed_bed_img, "removed_bed.nii.gz" );

请注意,这基本上与@BradleyDuncanJunior的答案相同(我的例子中的区域几乎肯定是不正确的,所以我给了他一个赞成票。

相关问题