Python module Mahotas thresholding issue
Asked Answered
F

3

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!

Frederickson answered 19/11, 2013 at 2:0 Comment(0)
S
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.

Sadness answered 19/11, 2013 at 2:29 Comment(1)
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.Barner
I
1

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

img = (img*255).astype('uint8')
Illdefined answered 11/7, 2014 at 12:32 Comment(2)
can you just explain, why is it so??Bohannon
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.Illdefined
P
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)
Parch answered 22/8, 2019 at 3:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.