查看.npy文件时出错

时间:2017-10-28 06:32:42

标签: python dicom

我按照教程从CT扫描图像分割肺部,保存为DICOM文件。然后我尝试使用.npy扩展名保存分段图像。但是,当我尝试再次加载并查看保存为.npy文件的文件时,我收到以下错误。

TypeError: Image data can not convert to float

这是我使用的代码。

    import numpy as np
    from matplotlib import pyplot as plt

    img_array = np.load('../../PROCESSED_DATA/maskedimages_0.npy')
    plt.imshow(img_array, cmap='gray')
    plt.show()

我无法发布整个代码。但这显示了我将图像保存为.npy的方式。

for folder_index in range(folder_count):
    patient = load_scan(INPUT_FOLDER + patients[1])
    patient_pixels = get_pixels_hu(patient)
    plt.hist(patient_pixels.flatten(), bins=80, color='c')
    plt.xlabel("Hounsfield Units (HU)")
    plt.ylabel("Frequency")
    plt.show()
    pix_resampled, spacing = resample(patient_pixels, patient, [1,1,1])
    print("Shape before resampling\t", patient_pixels.shape)
    print("Shape after resampling\t", pix_resampled.shape)
    plot_3d(pix_resampled, 400)
    segmented_lungs = segment_lung_mask(pix_resampled, False)
    segmented_lungs_fill = segment_lung_mask(pix_resampled, True)
    plot_3d(segmented_lungs_fill, 0)
    imgs=plot_3d(segmented_lungs_fill - segmented_lungs, 0)
    np.save(output_path + "maskedimages_%d.npy" % (folder_index), imgs)

有人可以告诉我需要做些什么来解决错误

P.S

def plot_3d(image, threshold=-300):

    # Position the scan upright, 
    # so the head of the patient would be at the top facing the camera
    p = image.transpose(2,1,0)
   # p = p[:,:,::-1]

    verts, faces ,_,_= measure.marching_cubes(p, threshold)

    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111, projection='3d')

    # Fancy indexing: `verts[faces]` to generate a collection of triangles
    mesh = Poly3DCollection(verts[faces], alpha=0.70)
    face_color = [0.45, 0.45, 0.75]
    mesh.set_facecolor(face_color)
    ax.add_collection3d(mesh)

    ax.set_xlim(0, p.shape[0])
    ax.set_ylim(0, p.shape[1])
    ax.set_zlim(0, p.shape[2])

    plt.show()


def segment_lung_mask(image, fill_lung_structures=True):

    # not actually binary, but 1 and 2. 
    # 0 is treated as background, which we do not want
    binary_image = np.array(image > -320, dtype=np.int8)+1
    labels = measure.label(binary_image)

    # Pick the pixel in the very corner to determine which label is air.
    #   Improvement: Pick multiple background labels from around the patient
    #   More resistant to "trays" on which the patient lays cutting the air 
    #   around the person in half
    background_label = labels[0,0,0]

    #Fill the air around the person
    binary_image[background_label == labels] = 2


    # Method of filling the lung structures (that is superior to something like 
    # morphological closing)
    if fill_lung_structures:
        # For every slice we determine the largest solid structure
        for i, axial_slice in enumerate(binary_image):
            axial_slice = axial_slice - 1
            labeling = measure.label(axial_slice)
            l_max = largest_label_volume(labeling, bg=0)

            if l_max is not None: #This slice contains some lung
                binary_image[i][labeling != l_max] = 1


    binary_image -= 1 #Make the image actual binary
    binary_image = 1-binary_image # Invert it, lungs are now 1

    # Remove other air pockets insided body
    labels = measure.label(binary_image, background=0)
    l_max = largest_label_volume(labels, bg=0)
    if l_max is not None: # There are air pockets
        binary_image[labels != l_max] = 0

    return binary_image

1 个答案:

答案 0 :(得分:0)

提示可以是生成数据的函数名为plot_3d的事实。尝试检查加载的numpy数组的形状。如果是3d图,pyplot不知道如何将其可视化为2d图像。