Python模块Mahotas阈值问题



我正在使用本教程http://pythonvision.org/basic-tutorial

然而,当我通过png图像时:

T = mahotas.thresholding.otsu(dna)

我得到一个错误:

TypeError:mahomtas.otsu:此函数只接受整数类型(传递的float32类型数组)

有人解释过这个问题吗?谢谢

这个错误基本上说图像数组中元素的类型是32位浮点,而不是整数,这是必需的。文档还说这个方法需要无符号的int。请参阅此处。

要将numpy数组转换为无符号的8位整数,请执行以下操作:

# Assuming I is your image. Convert to 8 bit unsigned integers.
I_uint8 = I.astype('uint8')

更新:请参阅下面Mahotas创建者对多通道图像问题的评论。

@lightalchemist的解决方案有效,只需记住先将图像乘以255即可:

img = (img*255).astype('uint8')

我也在遵循这个例子。经过高斯滤波器后,dnaf变成float64

print(dnaf.dtype)

您需要转换回8位图像

dnaf = dnaf.astype('uint8')
print(dnaf.dtype)

并进行阈值

T = mh.thresholding.otsu(dnaf)

最新更新