Comment
(DICOM은 이제 좀 알겠는데...) NIfTI, nii.gz에 대해 이제부터 알아보자
[+] NIfTI : Neuroimaging Informatics Technology Initiative의 약자로, 주로 뇌, 신경 영상 데이터의 표준 포멧
.nii : 일반 NIfTI 파일.nii.gz : 압축된 NIfTI 파일3차원 배열(3D-MRI) 또는 4차원 배열(4D-fMRI)의 데이터가 저장가능하며, Python에서는 nibabel 라이브러리를 통하여 읽고 쓴다.
[+] NIfTI 파일 읽기를 위한 라이브러리 뿐만아니라 gif 시각화에 필요한 라이브러리도 세팅
# 기본 라이브러리
import os
import numpy as np
# NIfTI 라이브러리
import nibabel as nib
# 시각화 라이브러리
import matplotlib.pyplot as plt
from IPython.display import displya, clear_output
(NIfTI 파일, 즉 .nii.gz 파일의 경로가 다음과 같다면... /tf/dataset/20240101.nii.gz
# NIfTI file path
nii_path = '/tf/dataset/20240101.nii.gz'
# NIfTI Read
t_nii = nib.load(nii_path)
t_nii_data = t_nii.get_fdata()
print("Data shape:", t_nii_data.shape)
# 출력 : Data shape: (180, 224, 224)
num_x_slices = t_nii_data.shape[0] # x축 slice 갯수
num_y_slices = t_nii_data.shape[1] # y축 slice 갯수
num_z_slices = t_nii_data.shape[2] # z축 slice 갯수
[+] 문자열 포메팅 - (print(f"ABCD {F}")
for i in range(n) # 각 Axis별로 몇장까지 gif 시각화 할건지를 n값에 기입
# NIfTI 3D array 데이터에서 한개의 Axis값은 고정, 나머지 Axis값은 All, 즉 2D array값 가져오기
slice_data = t_nii_data[i, :, :]
# 가져온 2D array를 시각화
# 보통 x축은 좌우, y축은 상하, z축은 앞뒤인데
# x축 1개값 기준으로 Slice하면 y,z 2D array이므로 행<->열을 Transpose하여 시각화함
plt.imshow(slice_data.T, cmap = 'gray)
# 전체 Slice중 몇번째 Slice인지 출력
plt.title(f"Slice {x+1} / {num_x_slices}")
# Axis와 눈금 없애기
plt.axis('off')
# Current, 즉 최근 생성된 Figure를 출력
# gcf : get current figure
# Figure : matplitlib에서의 생성된, 출력할 이미지같은 느낌
display(plt.gcf())
# 움짤(GIF)를 위해 이전 출력물 삭제
clear_output(wait=True)
for x in range(100):
slice_data = t_nii_data[x, :, :] # x축은 1~100, y,z축 전체 Array
plt.imshow(slice_data.T, cmap = 'gray')
plt.title(f"Slice {x+1} / {num_x_slices}")
plt.axis('off')
display(plt.gcf())
clear_output(wait=True)
for y in range(100):
slice_data = t_nii_data[:, y, :]
plt.imshow(slice_data.T, cmap = 'gray')
plt.title("Slice {y+1} / {num_y_slices}")
plt.axis('off')
display(plt.gcf())
clear_output(wait=True)
for z in range(100):
slice_data = t_nii_data[:, :, z]
plt.imshow(slice_data.T, cmap = 'gray')
plt.title("Slice {z+1} / {num_z_slices}")
plt.axis('off')
display(plt.gcf())
clear_output(wait=True)