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

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

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

djmepvbi

djmepvbi1#

试试这个:

import numpy as np

# Load the Nifti image
nifti_file = 'your_image.nii.gz'
img = nib.load(nifti_file)
data = img.get_fdata()

# Define a function to remove the bed area from a single 2D slice
def remove_bed_area(slice_data):
    # Implement your logic here to remove the bed area
    # You might use thresholding, segmentation, or other techniques

    # For example, you can threshold the slice to remove dark areas
    threshold = 100  # Adjust this threshold as needed
    slice_data[slice_data < threshold] = 0

    return slice_data

# Process each slice in the 3D image
for slice_idx in range(data.shape[2]):
    data[:, :, slice_idx] = remove_bed_area(data[:, :, slice_idx])

# Create a new Nifti image with the modified data
modified_img = nib.Nifti1Image(data, img.affine)

# Save the modified Nifti image to a new file
modified_file = 'modified_image.nii.gz'
nib.save(modified_img, modified_file)
goqiplq2

goqiplq22#

也许尝试

import nibabel as nib;
img = nib.load ( "withbed.nii.gz" );
data = img.get_fdata();

removed_bed_area = data [ :200, 4:111, : ];
removed_bed_img  = nib.Nifti1Image( removed_bed_area, img.affine );
nib.save( removed_bed_img, "removed_bed.nii.gz" );

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

相关问题