5

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!

zero323
  • 322,348
  • 103
  • 959
  • 935
Angel Cloudwalker
  • 2,015
  • 5
  • 32
  • 54

3 Answers3

7

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.

lightalchemist
  • 10,031
  • 4
  • 47
  • 55
  • author of mahotas here: mahotas' otsu will work with multi-channel images directly (Otsu is just computed on the histogram). This makes sense for many scientific uses and, as a bonus, automatically does the right thing if the image is RGB where all the channels have the same values. – luispedro Nov 20 '13 at 12:24
1

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

img = (img*255).astype('uint8')
  • can you just explain, why is it so?? – U.Swap Jan 29 '18 at 15:47
  • 1
    It has been some time, so I hope my answer still applies: image pixels are commonly represented using one of two notions: - either as floats in the range [0.0, 1.0], where 0.0 represents black and 1.0 represents white, or - as integers in the range [0, 255], 0 is black and 255 is white The OP needs to convert an image from notion 1 to notion 2, so the pixel values should be scaled to 255, hence the multiplication. – Mohamed A. Maksoud Jan 29 '18 at 21:41
  • 1
    Thanks for the explanation, i appreciate it! – U.Swap Jan 31 '18 at 17:57
1

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)
Tom
  • 131
  • 7