如何解决ConvNet中IndexError: arrays used as indices must be of integer (or boolean) type报错
问题原因与解决方案
这个错误的触发有两个核心原因:
- 输入数据类型不符合算法要求:
marching_cubes_classic要求输入的体数据为浮点型,你传入的segmented_lungs是肺部分割得到的二值int8数组,直接传入会导致函数内部计算异常,生成的面索引不满足整数要求。 - scikit-image版本兼容性问题:0.18及以上版本的scikit-image中,
marching_cubes_classic接口返回值为4个(顶点、面、法向量、顶点对应值),旧版本仅返回2个,返回值解包不匹配也会引发后续索引错误。
修复方案
直接修改plot_3d函数的对应代码即可:
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.astype(np.float32) # 修改:适配新版本返回值,忽略多余返回结果 verts, faces, _, _ = measure.marching_cubes_classic(p, threshold) # 若你使用的是最新版skimage,也可以替换为官方推荐的新接口 # 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()
调用绘图函数时,因为输入是二值肺掩码(1代表肺区域、0代表背景),阈值设置为0.5更合理:
plot_3d(segmented_lungs, 0.5)
内容的提问来源于stack exchange,提问作者Lala
相关产品推荐
相关产品推荐

