0
DICOM 파일로 저장된 CT 스캔 이미지에서 폐를 분류하는 방법을 안내했습니다. 그런 다음 세그먼트 화 된 이미지를 .npy 확장자로 저장하려고했습니다. 그러나 .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)
은 누군가가 힌트가 데이터를 생성하는 기능이 plot_3d
라는 사실이 될 수 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
안녕하세요, 답장을 보내 주셔서 감사합니다. 두 가지 방법을 게시했습니다. 이것이 어떻게 해결 될 수 있는지 알려주시겠습니까? – user3789200
imshow가 아니라 배열에서 plot_3d 함수를 사용해보십시오. – user1620443