[英]Error when viewing the .npy file
I followed a tutorial to segment the lungs from CT scan images which saved as DICOM files. 我遵循了一个教程,从保存为DICOM文件的CT扫描图像中分割出肺部。 Then I tried to save the segmented images with .npy extension.
然后,我尝试使用.npy扩展名保存分段图像。 But when I'm trying to load and view the files again which were saved as .npy files, I'm getting the following error.
但是,当我尝试再次加载并查看另存为.npy文件的文件时,出现以下错误。
TypeError: Image data can not convert to float
This is the code I used. 这是我使用的代码。
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()
I'm unable to post the whole code. 我无法发布整个代码。 But this shows the way how I saved the image as .npy.
但这显示了我将图像另存为.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)
Can someone suggest me what need to be done to solve the error 有人可以建议我解决该错误需要做什么
PS 聚苯乙烯
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
A hint can be the fact that the function generating the data is named plot_3d
. 提示可能是生成数据的函数名为
plot_3d
。 Try to check the shape of the loaded numpy array. 尝试检查已加载的numpy数组的形状。 If it is a 3d plot, pyplot doesn't know how to visualize it as a 2d image.
如果它是3d图,则pyplot不知道如何将其可视化为2d图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.