2

我一直在做一些深度学习,通常使用普通的图像数据集,如 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

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

图像输出:

Colab 输出

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

ImageJ 输出

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

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

正确的GT

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

不正确的 GT

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

i)如何读取 nii 图像,使其与 ImageJ 图像具有正确的方向?

编辑:我似乎已经纠正了方向问题。看起来图像是沿着 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 级,以便低强度核向外扩展并更明显)?

4

0 回答 0