.nii.gz文件以正确定向灰度图像

2024-05-19 07:41:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在做一些深入的学习,通常是使用普通的图像数据集,如jpeg或png。最近,我遇到了一个数据集,其中提供了以png/jpeg格式显示的测试图像,并在.nii.gz文件中显示了地面真相。如果打开.nii.gz文件,它将包含一个.nii文件。现在,作为这类文件的初学者,我仍然能够在Colab中绘制测试图像和地面真相。然而,它也有一些问题

我使用的代码

# Reading Image
import nibabel as nib
imagePath=testImageDir+'/141549_83.png'
testImage=cv2.imread(imagePath);
GTPath=testGTDir+'/141549_83.nii.gz'
niiObject=nib.load(GTPath)
print(niiObject)
testGT_extradim=niiObject.get_data() #testGTImage is nXmXchannel image
testGT_rot=cv2.resize(testGT_extradim,(512,512)) #By default imshow only acceptson(n,m) or (n,m,3) or (n,m,4) & this image was (n,m,1)
# Displaying figures
plt.figure(figsize=(12,8), dpi= 100)
plt.subplot(121)
plt.imshow(testImage)
plt.subplot(122)
plt.imshow(testGT_rot)

打印输出(niiObject):

<class 'nibabel.nifti1.Nifti1Image'>
data shape (512, 512, 1)
affine: 
[[ 1.  0. -0. -0.]
 [ 0.  1. -0. -0.]
 [ 0.  0.  1.  0.]
 [ 0.  0.  0.  1.]]
metadata:
<class 'nibabel.nifti1.Nifti1Header'> object, endian='<'
sizeof_hdr      : 348
data_type       : b''
db_name         : b''
extents         : 0
session_error   : 0
regular         : b'r'
dim_info        : 0
dim             : [  3 512 512   1   1   1   1   1]
intent_p1       : 0.0
intent_p2       : 0.0
intent_p3       : 0.0
intent_code     : none
datatype        : uint16
bitpix          : 16
slice_start     : 0
pixdim          : [1. 1. 1. 1. 0. 0. 0. 0.]
vox_offset      : 0.0
scl_slope       : nan
scl_inter       : nan
slice_end       : 0
slice_code      : unknown
xyzt_units      : 2
cal_max         : 0.0
cal_min         : 0.0
slice_duration  : 0.0
toffset         : 0.0
glmax           : 0
glmin           : 0
descrip         : b''
aux_file        : b''
qform_code      : aligned
sform_code      : scanner
quatern_b       : 0.0
quatern_c       : 0.0
quatern_d       : 0.0
qoffset_x       : -0.0
qoffset_y       : -0.0
qoffset_z       : 0.0
srow_x          : [ 1.  0. -0. -0.]
srow_y          : [ 0.  1. -0. -0.]
srow_z          : [0. 0. 1. 0.]
intent_name     : b''
magic           : b'n+1'
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:7: DeprecationWarning: get_data() is deprecated in favor of get_fdata(), which has a more predictable return type. To obtain get_data() behavior going forward, use numpy.asanyarray(img.dataobj).

* deprecated from version: 3.0
* Will raise <class 'nibabel.deprecator.ExpiredDeprecationError'> as of version: 5.0
  import sys

我已经通过了this answer,但是它在对象中有一个img属性,我在上面似乎缺少了这个属性(我的意思不是确切的,而是一个类似的对象)

图像输出:

Colab Output

在这之前,我曾尝试在ImageJ应用程序中打开.nii文件,它看起来像这样:

ImageJ Output

您可以看到ImageJ one在方向和颜色方面与Colab地面真实图像不同。ImageJ one在Colab输出的左侧组织图像中正确显示细胞核位置(如下所示)

正确的测试图像与地面真实度:

Correct GT

正确的基本事实与不正确的基本事实:

Incorrect GT

因此,问题分为多个子问题(如标题所示):

i)如何读取nii图像,使其与图像J处于正确的方向

编辑:我似乎已经纠正了方向问题。图像似乎是沿y=x线镜像的,因此我使用以下代码反转镜像:

   testGT=cv2.rotate(testGT_rot,cv2.cv2.ROTATE_90_CLOCKWISE)
   r,c=testGT.shape
   for i in range(0,r):  
    for j in range(0,c):
        temp=testGT[i,j]
        testGT[i,j]=testGT[j,i]
        testGT[j,i]=temp
   testGT=cv2.flip(testGT,1)

但是,我仍然愿意使用更好的方法来更深入地理解为什么会在nibabel中发生这种情况,并且可以使用我不知道的任何nibabel对象参数来解决它。

ii)ImageJ和nibabel加载的图像都不是灰度图像,因为一些突出的细胞核在这些图像中不可见(它们的强度非常低,ImageJ图像中的最大强度为40[对于最亮的细胞核])。如何将其转换为灰度(0到255的比例,以便低强度原子核缩小并更可见)


Tags: 文件图像datagetcodeslicepltcv2

热门问题