簡體   English   中英

如何在 4D Nifti MRI 圖像上正確循環和繪圖

[英]How to loop and plot correctly on 4D Nifti MRI image

我有不同尺寸的4D NIFTI 圖像[x,y,slices,frames] ,前兩個是空間分辨率,第三個是切片號,而最后一個是幀號,我試圖繪制特定的所有切片幀成一個圖形並使用 for 循環逐幀更新,而不是像以前那樣手動進行所有索引,但我有一個問題,我的圖像沒有更新幀(最后一個除外),如您在所附照片中所見,我該如何解決這個問題?? NIFTI 4D 圖像

#==================================
import nibabel as nib
import numpy as np
import matplotlib.pyplot as plt
#==================================
# load image (4D) [X,Y,Z_slice,time]
nii_img  = nib.load(path)
nii_data = nii_img.get_fdata()
#===================================================
fig, ax = plt.subplots(4,3,constrained_layout=True)
fig.canvas.set_window_title('4D Nifti Image')
fig.suptitle('4D_Nifti 10 slices 30 time Frames', fontsize=16)
#-------------------------------------------------------------------------------
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()
slice_counter = 0
for i in range(30):
    for j in range(3):
        for k in range(3):
            if slice_counter<9:
                ax[j,k].cla()
                ax[j,k].imshow(nii_data[:,:,slice_counter,i],cmap='gray', interpolation=None)
                ax[j,k].set_title("frame {}".format(i))
                ax[j,k].axis('off')
                slice_counter+=1
            else:
                #---------------------------------
                ax[3,0].axis('off')
                ax[3,2].axis('off')
                #---------------------------------
                ax[3,1].cla()
                ax[3,1].nii_data(nii_data[:,:,9,i],cmap='gray', interpolation=None)
                ax[3,1].set_title("frame {}".format(i))
                ax[3,1].axis('off')
                #---------------------------------
                # Note that using time.sleep does *not* work here!
                #---------------------------------
                plt.pause(.05)
            plt.close('all')

目前我不太清楚你的輸出應該是什么樣子,因為圖像中的第二列比其他列有更多的條目。 請在您的問題中更好地闡明這一點,並更新由於變量名稱不一致和縮進混亂而無法正常工作的代碼。

同時,我將嘗試第一次拍攝,您的目標是在 x 軸上打印所有切片,而每個幀都在 y 軸上。 我改編的代碼將打印前四幀的前三片。

#==================================
import nibabel as nib
import numpy as np
import matplotlib.pyplot as plt
#==================================
# load image (4D) [X,Y,Z_slice,time]
nii_img  = nib.load(path)
nii_data = nii_img.get_fdata()

#===================================================
number_of_slices = 3
number_of_frames = 4

fig, ax = plt.subplots(number_of_frames, number_of_slices,constrained_layout=True)
fig.canvas.set_window_title('4D Nifti Image')
fig.suptitle('4D_Nifti 10 slices 30 time Frames', fontsize=16)
#-------------------------------------------------------------------------------
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()

for slice in range(number_of_slices):
    for frame in range(number_of_frames):
        ax[frame, slice].imshow(nii_data[:,:,slice,frame],cmap='gray', interpolation=None)
        ax[frame, slice].set_title("layer {} / frame {}".format(slice, frame))
        ax[frame, slice].axis('off')

plt.show()         

黑色圖像的示例輸出如下所示:示例輸出

更新 - 05.04.2020
鑒於此處評論中的討論信息,更新版本:

#==================================
import nibabel as nib
import numpy as np
import matplotlib.pyplot as plt
from math import ceil
#==================================
# Load image (4D) [X,Y,Z_slice,time]
nii_img = nib.load(path)
nii_data = nii_img.get_fdata()

#===================================================
number_of_slices = nii_data.shape[2]
number_of_frames = nii_data.shape[3]

# Define subplot layout 
aspect_ratio = 16./9
number_of_colums = int(number_of_slices / aspect_ratio)
if( number_of_slices % number_of_colums > 0):
    number_of_colums += 1
number_of_rows = ceil(number_of_slices / number_of_colums)

# Setup  figure
fig, axs = plt.subplots(number_of_rows, number_of_colums,constrained_layout=True)
fig.canvas.set_window_title('4D Nifti Image')
fig.suptitle('4D_Nifti {} slices {} time Frames'.format(number_of_slices, number_of_frames), fontsize=16)
#-------------------------------------------------------------------------------
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()

for frame in range(number_of_frames):
    for slice, ax in enumerate(axs.flat):
        # For every slice print the image otherwise show empty space.
        if slice < number_of_slices:
            ax.imshow(nii_data[:,:,slice,frame],cmap='gray', interpolation=None)
            ax.set_title("layer {} / frame {}".format(slice, frame))
            ax.axis('off')
        else:
            ax.axis('off')

    plt.pause(0.05)

plt.close('all')

輸出將如下所示:第二個示例輸出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM