【问题标题】:Threshold Otsu: AttributeError: 'AxesSubplot' object has no attribute 'ravel'阈值 Otsu:AttributeError:“AxesSubplot”对象没有属性“ravel”
【发布时间】:2019-03-12 11:28:44
【问题描述】:

我加载了漂亮的文件(这些文件也是从 .pack CT 扫描转换而来的)。我的目标是使用阈值 otsu 算法从背景中屏蔽它并比较两个图像。当我尝试绘图时,我得到了错误

AttributeError: 'AxesSubplot' object has no attribute 'ravel'

以下是代码,并附有屏幕截图。

import SimpleITK as sitk
import matplotlib.pyplot as plt
import numpy as np
from skimage.filters import threshold_otsu

#THRESHOLD OTSU
img = sitk.GetArrayFromImage(sitk.ReadImage("\\\\x.x.x.x/users/ddff/python/nifts/prr_ipsi.nii"))
print(img.shape)
thresh = threshold_otsu(img.flatten())
#thresh = thresh.reshape(img.shape)
binary = img <= thresh

#I can plot this image slice fine
plt.imshow(img[20,:,:])

fig, axes = plt.subplots(ncols=1)
ax = axes.ravel()
ax[0] = plt.subplot(1, 3, 1)
ax[1] = plt.subplot(1, 3, 2)
ax[2] = plt.subplot(1, 3, 3, sharex=ax[0], sharey=ax[0])

ax[0].imshow(img[20,:,:], cmap=plt.cm.gray)
ax[0].set_title('Original Breast Delineation')
ax[0].axis('off')

ax[1].hist(thresh, bins=256)
ax[1].set_title('Histogram ')
ax[1].axvline(thresh, color='r')

ax[2].imshow(binary[20,:,:], cmap=plt.cm.gray)
ax[2].set_title('Thresholded')
ax[2].axis('off')

plt.show()[enter image description here][1]

【问题讨论】:

  • 如果您想要一个完整的轴二维矩阵,您可以将squeeze=False 添加到您对plt.subplots 的调用中。

标签: matplotlib plot dicom simpleitk niftynet


【解决方案1】:

axes 只是一个有 1 列的数字,因此 ravel 或 flatten 没有任何内容。如果您有多个子图,它将起作用。不过,如果您只有一行或一列,您可以在没有 ravel 的情况下执行以下操作。

fig, ax = plt.subplots(ncols=3, sharex=True, sharey=True)

ax[0].imshow(img[20,:,:], cmap=plt.cm.gray)
ax[0].set_title('Original Breast Delineation')
ax[0].axis('off')

ax[1].hist(thresh, bins=256)
ax[1].set_title('Histogram ')
ax[1].axvline(thresh, color='r')

ax[2].imshow(binary[20,:,:], cmap=plt.cm.gray)
ax[2].set_title('Thresholded')
ax[2].axis('off')

如果您想要子图实例的二维矩阵,您可以使用Thomas Kühn 的建议。

fig, ax = plt.subplots(ncols=3, sharex=True, sharey=True, squeeze=False)

然后你可以访问子图

ax[0][0].imshow()
ax[0][1].imshow()
......

【讨论】:

  • 查看我对问题的评论,了解如何使axes.ravel() 工作,独立于ncols 和nrows。
猜你喜欢
  • 1970-01-01
  • 2015-02-07
  • 2014-04-15
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 2019-04-22
  • 1970-01-01
相关资源
最近更新 更多