5

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

但是,当我传递 png 图像时:

T = mahotas.thresholding.otsu(dna)

我收到一个错误:

TypeError: mahotas.otsu: 这个函数只接受整数类型(传递的 float32 类型的数组)

有人有经验吗?w/这个问题?谢谢!

4

3 回答 3

7

该错误基本上表明图像数组中元素的类型是 32 位浮点数,而不是整数,这是必需的。文档还说这种方法需要无符号整数。见这里

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

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

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

于 2013-11-19T02:29:36.633 回答
1

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

img = (img*255).astype('uint8')
于 2014-07-11T12:32:07.400 回答
1

我也在关注这个例子。经过高斯滤波后,dnaf变成了float64

print(dnaf.dtype)

您需要转换回 8 位图像

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

并继续阈值

T = mh.thresholding.otsu(dnaf)
于 2019-08-22T03:35:14.193 回答