首页 > 解决方案 > Nib.load() 错误 - 尝试加载要为 FCNN 调整大小的 PNG 和 DICOM 图像

问题描述

有 40 个 DICOM 和 40 个 PNG 图像(数据及其掩码)用于加载到我的 Google Drive 的完全 CNN,并已通过 print(os.listdir(...)) 由笔记本找到,如下所示第一个代码块,其中列出了上述集合中 80 个数据的所有名称。

在下面的第二个代码块中,还将所有 DICOM 和 PNG 通配到 img_path 和 mask_path 中,长度均为 40。

现在尝试将所有图像的大小调整为 256 x 256,然后再将它们输入到类似 U-net 的架构中进行分割。但是,无法通过 nib.load() 调用加载它们,因为它无法计算出 DCM 和 PNG 文件的文件类型,即使对于后者它不会出错但仍会产生一个空数据集,如最后一个块代码产量。

假设,一旦第三块代码中 for 循环中的前几行被纠正,预处理应该完成,我可以进入 U-net 实现。

让当前的 pydicom 在 Colab 笔记本中运行并尝试使用它来代替 nib.load() 调用,这会产生与当前相同的错误。


#import data as data
import pydicom
from PIL import Image
import numpy as np
import glob
import imageio
print(os.listdir("/content/drive/My Drive/Images"))
print(os.listdir("/content/drive/My Drive/Masks"))

pixel_data = []
images = glob.glob("/content/drive/My Drive/Images/IMG*.dcm");
for image in images:
    dataset = pydicom.dcmread(image)
    pixel_data.append(dataset.pixel_array)
#print(len(images))
#print(pixel_data)

pixel_data1 = [] ----------------> this section is the trouble area <-------
masks = glob.glob("content/drive/My Drive/Masks/IMG*.png");
for mask in masks:
    dataset1 = imageio.imread(mask)
    pixel_data1.append(dataset1.pixel_array)
print(len(masks))
print(pixel_data1)

['IMG-0004-00040.dcm'、'IMG-0002-00018.dcm'、'IMG-0046-00034.dcm'、'IMG-0043-00014.dcm'、'IMG-0064-00016.dcm' ,....] ['IMG-0004-00040.png', 'IMG-0002-00018.png', 'IMG-0046-00034.png', 'IMG-0043-00014.png', 'IMG- 0064-00016.png',....]

0 ----------------> 故障区的输出 <--------------

[]

import glob
img_path = glob.glob("/content/drive/My Drive/Images/IMG*.dcm")
mask_path = glob.glob("/content/drive/My Drive/Masks/IMG*.png")
print(len(img_path))
print(len(mask_path))

40

40

images=[]
a=[]
for a in pixel_data:
    a=resize(a,(a.shape[0],256,256))
    a=a[:,:,:]
    for j in range(a.shape[0]):
        images.append((a[j,:,:]))

没有输出,这部分工作正常。

images=np.asarray(images)
print(len(images))

10880

masks=[]               -------------------> the other trouble area <-------
b=[]
for b in masks:
    b=resize(b,(b.shape[0],256,256))
    b=b[:,:,:]
    for j in range(b.shape[0]):
        masks.append((b[j,:,:]))

没有输出,试图解决如何修复此部分的问题。

masks=np.asarray(masks) ------------> fix the above section and this
print(len(masks))                     should have no issues

[]

标签: pngimage-resizingnibpydicommedical-imaging

解决方案


您正在尝试使用 再次加载 DICOM 文件nib.load,但这不起作用,因为您已经发现:

for name in img_path:
    a=nib.load(name)  # does not work with DICOM files
    a=a.get_data()
    a=resize(a,(a.shape[0],256,256))

您已经拥有pixel_data列表中 DICOM 文件的数据,因此您应该使用这些:

for a in pixel_data:
    a=resize(a,(a.shape[0],256,256))  # or something similar, depending on the shape of pixel_data
    ...

推荐阅读