阈值Otsu:AttributeError:'AxesSubplot'对象没有属性'ravel'

我加载了漂亮的文件(这些文件也是从.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]
谢尔多雷

axes只是一个有1列的单一数字,所以没有任何要ravelflatten如果您有多个子图,它将起作用。但是,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')

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

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

然后您可以按以下方式访问子图

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

“ AttributeError:'list'对象没有属性'ravel'”

AttributeError:'AxesSubplot'对象没有属性'Circle'在tkinter中嵌入matplotlib

matplotlib tkinter AttributeError:'AxesSubplot'对象没有属性'canvas'并且matplotlib图在更新tkinter窗口大小之前不会更新

Tight_Layout:属性错误。'AxesSubplot'对象没有属性'tight_layout'

转换目标变量时,“ DataFrame”对象没有属性“ ravel”吗?

xticks的AxesSubplot对象的等效函数

xticks的AxesSubplot对象的等效函数

AttributeError: '' 对象没有属性 ''

AttributeError: '...' 对象没有属性 '...'

AttributeError 对象没有属性

OpenCV Otsu 的阈值:计算多个 Mat 对象的单个阈值

AttributeError:“列表”对象没有属性“对象”

Matplotlib:TypeError:'AxesSubplot'对象不可下标

如何删除 AxesSubplot 对象上的额外图?

AttributeError:“系列”对象没有属性“重塑”

AttributeError:“ str”对象没有属性“ name”

AttributeError:“ float”对象没有属性“ max”

AttributeError:'str'对象没有属性'errno'

AttributeError:“函数”对象没有属性“摘要”

AttributeError:'NoneType'对象没有属性'tbody'

AttributeError:“模块”对象没有属性“ TestCase”

AttributeError:对象没有属性“ tk”

AttributeError:“函数”对象没有属性“索引”

AttributeError: 'module' 对象没有属性 'home'

AttributeError:'MyMainWindow'对象没有属性'pushButton'

AttributeError:“图像”对象没有属性“保存”

AttributeError: 'tuple' 对象没有属性 '_committed'

AttributeError: 'TensorSliceDataset' 对象没有属性 'dtype'

AttributeError:'str'对象没有属性'union'