import nibabel as nib
import numpy as np
import time
# 读取nifti文件
img = nib.load('d:/test.nii')
start_time = time.time()
# 获取图像数据和头信息
data = img.get_fdata()
header = img.header
# 获取图像维度信息
dim_x, dim_y, dim_z = data.shape
# 计算中心位置坐标
center_x = dim_x // 2
center_y = dim_y // 2
center_z = dim_z // 2
# 获取中心位置的标签值
label = data[center_x, center_y, center_z]
# 打印结果
print("中心位置的标签值:", label)
end_time = time.time()
import nibabel as nib
import numpy as np
import time
from scipy.ndimage import distance_transform_edt
from joblib import Parallel, delayed
# 读取NIfTI文件
img = nib.load('D:/test.nii')
# 获取图像数据和头信息
data = img.get_fdata()
start_time = time.time()
# 计算距离变换
def compute_distance_transform(label):
binary_mask = (data == label).astype(np.uint8)
return distance_transform_edt(binary_mask)
dist_transforms = Parallel(n_jobs=-1)(delayed(compute_distance_transform)(label) for label in np.unique(data[data != 0]))
dist_transform = np.stack(dist_transforms, axis=0).min(axis=0)
end_time = time.time()
# 获取图像维度信息
dim_x, dim_y, dim_z = data.shape
# 计算中心位置坐标
center_x = dim_x // 2
center_y = dim_y // 2
center_z = dim_z // 2
# 获取中心位置的标签值
center_label = int(data[center_x, center_y, center_z])
# 初始化最小距离和最小距离对应的标签值
min_distance = np.inf
min_distance_label = None
# 遍历所有非零标签值
for label in np.unique(data[data != 0]):
# 计算距离中心位置的欧几里得距离
distance = dist_transform[center_x, center_y, center_z]
# 更新最小距离和对应的标签值
if distance < min_distance:
min_distance = distance
min_distance_label = int(label)
# 打印结果
print("距离中心最近的非零标签值:", min_distance_label)
print("时间:", end_time - start_time)