luispedro / mahotas

Computer Vision in Python
https://mahotas.rtfd.io
Other
847 stars 148 forks source link

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

Closed jonaslund closed 10 years ago

jonaslund commented 10 years ago

When running the example code

import numpy as np
import mahotas
import pylab

img = mahotas.imread('test.jpeg')
T_otsu = mahotas.thresholding.otsu(img)
seeds,_ = mahotas.label(img > T_otsu)
labeled = mahotas.cwatershed(img.max() - img, seeds)

pylab.imshow(labeled)

This error is returned

Traceback (most recent call last):
  File "ex.py", line 6, in <module>
    T_otsu = mahotas.thresholding.otsu(img)
  File "/mahotas-1.1.0-py2.7-macosx-10.9-intel.egg/mahotas/thresholding.py", line 95, in otsu
    _verify_is_integer_type(img, 'otsu')
  File "/mahotas-1.1.0-py2.7-macosx-10.9-intel.egg/mahotas/internal.py", line 140, in _verify_is_integer_type
    raise TypeError('mahotas.%s: This function only accepts integer types (passed array of type %s)' % (function_name, A.dtype))
TypeError: mahotas.otsu: This function only accepts integer types (passed array of type float32)

Any ideas?

luispedro commented 10 years ago

My guess is that this is from the image. Can you post it?

I will change the example in the README to work with the provided demo images, though. I think this is an old example from when there were no demo images.

jonaslund commented 10 years ago

Don't think so based on this: Here's a similar error running the demo with the supplied images

demos 💪  python morphology.py
Traceback (most recent call last):
  File "morphology.py", line 8, in <module>
    T = mahotas.otsu(luispedro)
  File "/mahotas-1.1.0-py2.7-macosx-10.9-intel.egg/mahotas/thresholding.py", line 95, in otsu
    _verify_is_integer_type(img, 'otsu')
  File "/mahotas-1.1.0-py2.7-macosx-10.9-intel.egg/mahotas/internal.py", line 140, in _verify_is_integer_type
    raise TypeError('mahotas.%s: This function only accepts integer types (passed array of type %s)' % (function_name, A.dtype))
TypeError: mahotas.otsu: This function only accepts integer types (passed array of type float32)

Any ideas what the error might come from?

luispedro commented 10 years ago

The proximal cause is that you are passing an array of floats instead of an array of integer values.

* Are you running the code in:

https://github.com/luispedro/mahotas/blob/master/mahotas/demos/morphology.py

? I find it a bit odd that you'd get these results.

(I just updated that old code too; the previous one would only work if it was run in the right directory, but otherwise it's the same computation).

I don't understand the error you are seeing, but I'd try looking at the type of the array luispedro just before passing it to otsu (luispedro.dtype) and eventually convert it (luispedro.astype(np.uint8)).

HTH Luis

prelegalwonder commented 10 years ago

Just to add a quick comment. I had this same issue after trying to convert an image from one format to another. After installing PIL I was able to use the initial format type.