數(shù)據(jù)是醫(yī)學(xué)圖像中的MRI圖像狞尔。要求完成圖像中RAS坐標(biāo)到tensor矩陣坐標(biāo)的轉(zhuǎn)化撒轮。
RAS坐標(biāo):原始坐標(biāo),單位為mm醇锚。
像素坐標(biāo):nii文件作為矩陣數(shù)據(jù)的坐標(biāo)哼御,單位為像素坯临。
轉(zhuǎn)換所需的數(shù)據(jù):
使用Volume Information中的“Image Spacing”,“Image Origin”恋昼,“IJK to RAS Direction Matrix”三部分信息看靠。
以467859號(hào)的fixT1W1.nii為例。
三部分信息如下圖所示:
將Volume Information中的“Image Spacing”液肌,“Image Origin”挟炬,“IJK to RAS Direction Matrix”三部分信息分別賦值為python中的space,origin,matrix三個(gè)全局變量。
轉(zhuǎn)化公式如下:
注:如果是nii格式數(shù)據(jù)矩屁,可以使用python讀取這三個(gè)數(shù)據(jù):
import SimpleITK
file = ....
img = SimpleITK.ReadImage(file)
origin = img.GetOrigin()
direction = np.array(img.GetDirection())
space = np.array(img.GetSpacing())
注意辟宗,用SimpleITK讀取的數(shù)據(jù)和3D Slicer看到的數(shù)據(jù)可能有部分正負(fù)號(hào)的區(qū)別,即他們的RAS坐標(biāo)有些坐標(biāo)軸正負(fù)相反吝秕。最好自己確認(rèn)一下泊脐。
例子:
在slicer軟件中任意取一個(gè)RAS坐標(biāo)點(diǎn),如下圖所示
截圖時(shí)鼠標(biāo)在圖中左上方紅色五角星位置烁峭,右下方綠線表示此時(shí)鼠標(biāo)位置的RAS坐標(biāo)為(29.6,37.6,6.1)容客。對(duì)應(yīng)的矩陣中的坐標(biāo)為(86,218约郁,119)缩挑。
python代碼:
# 坐標(biāo)轉(zhuǎn)換:RAS(單位:mm)平移,旋轉(zhuǎn)鬓梅,縮放供置,得到像素坐標(biāo)。
import numpy as np
origin = np.array([53.0123, 79.2142, 66.5104]).T
space = 0.293
# IJK to RAS Direction Matrix
matrix = np.array([
[-0.9990, 0.0026, 0.0436],
[-0.0436, -0.0920, -0.9948],
[0.0014, -0.9958, 0.0920]
])
def ras_to_coordinate(ras, origin, matrix, space):
"""RAS到像素坐標(biāo)coordinate的轉(zhuǎn)化
"""
ras = ras - origin
ras = np.matmul(np.linalg.inv(matrix), ras)
coordinate = ras / space
return coordinate
def coordinate_to_ras(coordinate, origin, matrix, space):
"""像素坐標(biāo)coordinate到RAS的轉(zhuǎn)化
"""
ras = coordinate * space
ras = np.matmul(matrix, ras)
ras = ras + origin
return ras
if __name__ == "__main__":
# ras = np.array([29.6, 37.6, 6.1]).T
# coordinate = [86, 218, 119]
print("RAS to coordinate:")
ras = np.array(
[29.6, 37.6, 6.1]
)
print("input RAS : ", ras)
coordinate = ras_to_coordinate(
ras, origin=origin, matrix=matrix, space=space)
print("output coordinate : ", coordinate)
print("coordinate to RAS:")
coordinate = np.array(
[86., 218., 119.]
)
print("input coordinate : ", coordinate)
ras = coordinate_to_ras(
coordinate, origin=origin, matrix=matrix, space=space)
print("output RAS : ", ras)
輸出為:
RAS to coordinate:
input RAS : [29.6 37.6 6.1]
output coordinate : [ 85.73972512 218.14819897 118.83805793]
coordinate to RAS:
input coordinate : [ 86. 218. 119.]
output RAS : [29.5257716 37.5534676 6.147712 ]
雖然不是完全相同绽快,但是大概能用吧芥丧。