Question

I'm using this tutorial http://pythonvision.org/basic-tutorial

However when I pass a png image:

T = mahotas.thresholding.otsu(dna)

I get an error:

TypeError: mahotas.otsu: This function only accepts integer types (passed array of type float32)

Does anyone have exp. w/this issue? Thanks!

Was it helpful?

Solution

The error basically says that the type of the elements in your image array is 32 bit float, not integer, which is required. The docs also say that this method requires unsigned int. See here.

To convert a numpy array to unsigned 8 bit integers, do the following:

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

UPDATE: Please see comment by Mahotas' creator below on the issue of multi-channel images.

OTHER TIPS

the solution by @lightalchemist works, just remember to multiply the image by 255 first:

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

I am also following this example. After the gaussian filter, dnaf becomes a float64

print(dnaf.dtype)

You need to convert back to a 8-bit image

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

And carry on with the thresholding

T = mh.thresholding.otsu(dnaf)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top