如何在Python中以Python语法读取Matlab保存的三维矩阵(Dicom Matrix)?
How to read a saved three-dimensional matrix (Dicom Matrix) of Matlab as Python syntax in Python?
我在 Matlab 中保存了坐标为 (row = 288, col = 288, slice(z) =266) 的 3D 矩阵。
现在我想在 Python 中加载它。不幸的是,加载后,它是 (row = 288, col = 266, slice(z) =288) in Python.
鉴于此,大小的 Matlab 语法:(第 3 维中的行、列、切片)和 Python 大小的语法:(第 3 维中的切片、行、列)。
例如,在下面的代码中,当我想将变量 A 作为数组查看时,它是 (row = 288, col = 266, slice(z) =288):
from math import sqrt
from skimage import data
import matplotlib.pyplot as plt
import cv2
import pydicom
import scipy.io as sio
import os
import numpy as np
for root, dirs, files in
os.walk('G:\PCodes\Other_Codes'):
matfiles = [_ for _ in files if _.endswith('.mat')]
for matfile in matfiles: # matfile: 'Final_Volume.mat'
Patient_All_Info = sio.loadmat(os.path.join(root, matfile)) # Patient_All_Info : {dict}
Patient_All_Info.items()
A = Patient_All_Info["Final_Volume"] # A: {ndarray} : (288, 266, 288) - it isn't as (row = 288, col = 288, slice(z) =266) coordinates.
S = np.shape(A) # S: <class 'tuple'>: (288, 288, 266) ?
dcm_image = pydicom.read_file('A')
image = dcm_image.pixel_array
plt.imshow(image, cmap='gray')
plt.show()
如何在Python中加载Matlab保存的3D矩阵(Dicom Matrix)?
在 Octave 会话中:
>> x = reshape(1:24,4,3,2);
>> save -v7 'test.mat' x
使用Python,loadmat
保留形状和F
顺序:
In [200]: data = loadmat('test.mat')
In [208]: data['x'].shape
Out[208]: (4, 3, 2)
In [209]: data['x'].ravel(order='F')
Out[209]:
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13.,
14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24.])
转置会产生一个 (2,3,4) 数组
In [210]: data['x'].T
Out[210]:
array([[[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.]],
[[13., 14., 15., 16.],
[17., 18., 19., 20.],
[21., 22., 23., 24.]]])
transpose
可以带一个顺序参数,例如data['x'].transpose(2,0,1)
.
(我不熟悉 dicom
,但希望这说明了 loadmat
如何处理来自 MATLAB 的 3d 数组。)
我在 Matlab 中保存了坐标为 (row = 288, col = 288, slice(z) =266) 的 3D 矩阵。
现在我想在 Python 中加载它。不幸的是,加载后,它是 (row = 288, col = 266, slice(z) =288) in Python.
鉴于此,大小的 Matlab 语法:(第 3 维中的行、列、切片)和 Python 大小的语法:(第 3 维中的切片、行、列)。
例如,在下面的代码中,当我想将变量 A 作为数组查看时,它是 (row = 288, col = 266, slice(z) =288):
from math import sqrt
from skimage import data
import matplotlib.pyplot as plt
import cv2
import pydicom
import scipy.io as sio
import os
import numpy as np
for root, dirs, files in
os.walk('G:\PCodes\Other_Codes'):
matfiles = [_ for _ in files if _.endswith('.mat')]
for matfile in matfiles: # matfile: 'Final_Volume.mat'
Patient_All_Info = sio.loadmat(os.path.join(root, matfile)) # Patient_All_Info : {dict}
Patient_All_Info.items()
A = Patient_All_Info["Final_Volume"] # A: {ndarray} : (288, 266, 288) - it isn't as (row = 288, col = 288, slice(z) =266) coordinates.
S = np.shape(A) # S: <class 'tuple'>: (288, 288, 266) ?
dcm_image = pydicom.read_file('A')
image = dcm_image.pixel_array
plt.imshow(image, cmap='gray')
plt.show()
如何在Python中加载Matlab保存的3D矩阵(Dicom Matrix)?
在 Octave 会话中:
>> x = reshape(1:24,4,3,2);
>> save -v7 'test.mat' x
使用Python,loadmat
保留形状和F
顺序:
In [200]: data = loadmat('test.mat')
In [208]: data['x'].shape
Out[208]: (4, 3, 2)
In [209]: data['x'].ravel(order='F')
Out[209]:
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13.,
14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24.])
转置会产生一个 (2,3,4) 数组
In [210]: data['x'].T
Out[210]:
array([[[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.]],
[[13., 14., 15., 16.],
[17., 18., 19., 20.],
[21., 22., 23., 24.]]])
transpose
可以带一个顺序参数,例如data['x'].transpose(2,0,1)
.
(我不熟悉 dicom
,但希望这说明了 loadmat
如何处理来自 MATLAB 的 3d 数组。)