Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong threshold value in threshold_otsu when passing a precomputed histogram #5496

Closed
maurosilber opened this issue Aug 2, 2021 · 0 comments · Fixed by #5497
Closed

Wrong threshold value in threshold_otsu when passing a precomputed histogram #5496

maurosilber opened this issue Aug 2, 2021 · 0 comments · Fixed by #5497

Comments

@maurosilber
Copy link
Contributor

Description

When the histogram has bins with zero counts at the beginning, it returns zero as threshold.

Way to reproduce

import numpy as np
from skimage.filters import threshold_otsu

x = np.array([1, 2])

>>> threshold_otsu(x)
1
>>> threshold_otsu(hist=np.bincount(x))
0

Version information

# Paste the output of the following python commands
from __future__ import print_function
import sys; print(sys.version)
import platform; print(platform.platform())
import skimage; print("scikit-image version: {}".format(skimage.__version__))
import numpy; print("numpy version: {}".format(numpy.__version__))
3.9.6 | packaged by conda-forge | (default, Jul 11 2021, 03:39:48) [GCC 9.3.0]
Linux-5.8.0-63-generic-x86_64-with-glibc2.31
scikit-image version: 0.19.0.dev0
numpy version: 1.21.1
maurosilber added a commit to maurosilber/scikit-image that referenced this issue Aug 2, 2021
When the histogram started with zero count bins, a divide by zero
happened at thresholding.py:351, yielding np.nan values.
Then, at line 359, np.argmax returned 0 as index, producing a wrong
threshold value.

Closes scikit-image#5496.
maurosilber added a commit to maurosilber/scikit-image that referenced this issue Aug 2, 2021
maurosilber added a commit to maurosilber/scikit-image that referenced this issue Aug 3, 2021
maurosilber added a commit to maurosilber/scikit-image that referenced this issue Aug 3, 2021
`thresholding.validate image_histogram` computes histograms with
`exposure.histogram(..., source_range='image')`, which determines
the histogram range from the input.

Instead, an histogram precomputed with `numpy.bincount` might return
an array that start with zeroes. Supplying such histogram cause issues
in `thresholding.threshold_otsu` (issue scikit-image#5496).

Trimming user-supplied histograms if the array of counts starts or ends
with zeros, provides the same output histogram as if the image itsef was
provided (in case of integer dtypes).
maurosilber added a commit to maurosilber/scikit-image that referenced this issue Aug 3, 2021
`thresholding.validate image_histogram` computes histograms with
`exposure.histogram(..., source_range='image')`, which determines
the histogram range from the input.

Instead, an histogram precomputed with `numpy.bincount` might return
an array that start with zeroes. Supplying such histogram cause issues
in `thresholding.threshold_otsu` (issue scikit-image#5496).

Trimming user-supplied histograms if the array of counts starts or ends
with zeros, provides the same output histogram as if the image itsef was
provided (in case of integer dtypes).
rfezzani pushed a commit that referenced this issue Aug 4, 2021
* Added threshold_otsu test for #5496

* Fix validate_image_histogram when hist starts/ends with zeroes

`thresholding.validate image_histogram` computes histograms with
`exposure.histogram(..., source_range='image')`, which determines
the histogram range from the input.

Instead, an histogram precomputed with `numpy.bincount` might return
an array that start with zeroes. Supplying such histogram cause issues
in `thresholding.threshold_otsu` (issue #5496).

Trimming user-supplied histograms if the array of counts starts or ends
with zeros, provides the same output histogram as if the image itsef was
provided (in case of integer dtypes).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants