2016-11-10 1 views
1

나는 3D 그레이 스케일 .nii 파일을 nibabel로 작성하고 NIfTI 뷰어 (Mango, MCIcron)에서 열어도 문제가 없었습니다. 그러나 각 RGB 평면이 다른 볼륨으로 해석되므로 3D 컬러를 쓸 수 없었습니다. 예 : 이 출력은 다음과 같습니다.NiBabel을 사용하여 3D NIfTI를 작성하는 방법은 무엇입니까?

import nibabel as nib 
import numpy as np 
nifti_path = "/my/local/path" 
test_stack = (255.0 * np.random.rand(20, 201, 202, 3)).astype(np.uint8) 
ni_img = nib.Nifti1Image(test_stack, np.eye(4)) 
nib.save(ni_img, nifti_path) 

은 3 개의 별도 20x201x202 볼륨으로 표시됩니다. 또한 첫 번째 축 (예 : np.random.rand (3, 20, 201, 202))에 색상 평면을 넣으려고했지만 동일한 문제가 발생합니다. 조금 주위를 보면 24 비트 RGB 평면 이미지의 경우 128로 설정해야하는 "데이터 세트"필드가있는 것 같습니다. nibabel에 대한 좋은 점 중 하나는 헤더가 공급되는 numpy 배열을 기반으로 헤더를 자동으로 설정하는 방법입니다. 그러나 이것은 모호한 경우이며 헤더 정보를 출력하면 데이터 유형을 2 (uint8)로 설정하는 것을 볼 수 있습니다. 이는 아마도 시청자가 RGB24가 아닌 별도의 볼륨으로 해석하는 이유 일 수 있습니다. API에 데이터 형식 설정에 대한 공식적인 지원이 표시되지 않지만 the documentation은 "큰 용기"가있는 원시 필드에 대한 액세스를 언급합니다. 헤더 값

print(hdr) 

변화에

hdr = ni_img.header 
raw = hdr.structarr 
raw['datatype'] = 128 

작품 즉, 이렇게하는 것은 "데이터 형식 : RGB"제공 :

File "<python path>\lib\site-packages\nibabel\arraywriters.py", line 126, in scaling_needed 
raise WriterError('Cannot cast to or from non-numeric types') 
nibabel.arraywriters.WriterError: Cannot cast to or from non-numeric types 
하지만 작성

nib.save(ni_img, nifti_path) 

오류가 발생합니다

다음과 같은 경우 예외가 발생합니다. 일부 arr_dtype! = out_dtype, 원시 헤더의 해킹으로 인해 일부 불일치가 발생합니다.

이렇게하려면 적절한 방법이 있습니까? 뇌 영상 분석 메일 링리스트에 matthew.brett하는

답변

0

감사합니다, 정말 같은 3 차원 컬러 NIfTI을 쓸 수 있어요 :

# ras_pos is a 4-d numpy array, with the last dim holding RGB 
shape_3d = ras_pos.shape[0:3] 
rgb_dtype = np.dtype([('R', 'u1'), ('G', 'u1'), ('B', 'u1')]) 
ras_pos = ras_pos.copy().view(dtype=rgb_dtype).reshape(shape_3d) # copy used to force fresh internal structure 
ni_img = nib.Nifti1Image(ras_pos, np.eye(4)) 
nib.save(ni_img, output_path) 
관련 문제