import SimpleITK as sitk
import matplotlib.pyplot as plt
import numpy as np
def worldToVoxelCoord(worldCoord, origin, spacing,node_size):
"""
医生标的世界坐标转成像素坐标
"""
stretchedVoxelCoord = np.absolute(worldCoord - origin)
voxelCoord = stretchedVoxelCoord // spacing # x,y,z
radius = (node_size / spacing[0]/2)
return voxelCoord, radius
if __name__ == '__main__':
file_path = "D:/download\subset1\subset1/1.3.6.1.4.1.14519.5.2.1.6279.6001.134370886216012873213579659366.mhd"
image = sitk.ReadImage(file_path)
ORd = image.GetOrigin()
print('原点坐标:',ORd)
SPc = image.GetSpacing()
print('像素间隔:',SPc) # x,y,z
# numpy array
img = sitk.GetArrayFromImage(image)
print('img.shape:',img.shape)
# 查annotation.csv
node_loc = (-58.2494371,-98.05475684,-227.604282)
node_size = 4.402746921
# 得到结节的图像位置
voxelCoord, radius = worldToVoxelCoord(np.array(node_loc),
np.array(ORd),np.array(SPc),node_size)
print('结节的图像位置:',voxelCoord,'像素半径:',radius)
x,y,z = int(voxelCoord[0]),int(voxelCoord[1]),int(voxelCoord[2])
r = np.math.ceil(radius)
# plt作图
img_node = img[z]
print('结节中点位置像素值为:',img_node[y,x])
img_node[y-r,x-r:x+r] = 1000
img_node[y-r:y+r,x-r] = 1000
img_node[y+r,x-r:x+r] = 1000
img_node[y-r:y+r,x+r] = 1000
plt.imshow(img_node,cmap='gray')
plt.show()