Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python module Mahotas thresholding issue

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!

like image 574
Angel Cloudwalker Avatar asked Nov 19 '13 02:11

Angel Cloudwalker


3 Answers

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.

like image 175
lightalchemist Avatar answered Oct 18 '22 03:10

lightalchemist


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

img = (img*255).astype('uint8')
like image 44
Mohamed A. Maksoud Avatar answered Oct 18 '22 01:10

Mohamed A. Maksoud


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)
like image 1
Tom Avatar answered Oct 18 '22 01:10

Tom