我正在尝试运行以下代码:
def load_scan(path):
slices = [dicom.read_file(path + '/' + s) for s in os.listdir(path)]
slices.sort(key = lambda x: int(x.InstanceNumber))
try:
slice_thickness = np.abs(slices[0].ImagePositionPatient[2] - slices[1].ImagePositionPatient[2])
except StopIteration:
slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation)
for s in slices:
s.SliceThickness = slice_thickness
return slices
id=0
patient = load_scan(data_path)
但是我遇到错误:
RuntimeError:生成器引发了StopIteration
我使用python 3.7
答案 0 :(得分:0)
错误是由于(py)dicom的版本可能与python3不兼容,在kaggle内核中,修复很简单:
只需从 dicom 切换到pydicom
,例如
def load_scan(path):
slices = [pydicom.read_file(path + '/' + s) for s in os.listdir(path)]
slices.sort(key = lambda x: float(x.ImagePositionPatient[2]))
try:
slice_thickness = np.abs(slices[0].ImagePositionPatient[2] - slices[1].ImagePositionPatient[2])
except:
slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation)
for s in slices:
s.SliceThickness = slice_thickness
return slices
,以下代码将起作用:
train_patient_ids = os.listdir(os.path.join(MAIN_DIR,'train'))
test_patient_ids = os.listdir(os.path.join(MAIN_DIR,'test'))
first_patient = load_scan(os.path.join(MAIN_DIR,'train',train_patient_ids[0]))
first_patient_pixels = get_pixels_hu(first_patient)
plt.hist(first_patient_pixels.flatten(), bins=80, color='c')
plt.xlabel("Hounsfield Units (HU)")
plt.ylabel("Frequency")
plt.show()
plt.imshow(first_patient_pixels[80], cmap=plt.cm.gray)
plt.show()