Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upRefactoring: threshold methods with histogram parameters #5006
Conversation
added method threshold_otsu_from_histogram as shortcut in case the histogram is known in advance
Hi @haesleinhuepf thanks for starting this discussion! I'll let other devs chime in, but my gut feeling is that rather than adding one function to the API we could just add one keyword argument @sciunto you've worked extensively on thresholding functions what do you think of these additions? |
I think the same as @emmanuelle: let's add |
Hey @emmanuelle and @jni , at the beginning I thought so, too. But then I saw that we need the histogram and the bin_centers array. Otherwise, we can only return the bin_index and not the corresponding grey value. I didn't know how force the user to provide both together or both none. Assert statements came to my mind. Any better idea how to achieve this? Thanks for the feedback! |
A great point that I had overlooked! My preferred option given this would be to provide a tuple of |
Just to make sure I understand correctly: That would mean the threshold_otsu method returns a conceptually different thing (histogram index versus grey value) depending on input parameters. If that's ok, I'm happy to implement it. |
I'm +1 with you @emmanuelle and this can be applied to several of our functions as well. |
It's not, the point is that for integer-valued images they are one and the same thing! So it is a shortcut for users of those images. If someone decides to misuse the shortcut, that's on them! At any rate, you can implement the tuple input version first and later we can decide if we want to provide this shortcut. |
Hello @haesleinhuepf! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:
Comment last updated at 2020-10-05 15:03:51 UTC |
Alright @emmanuelle and @jni , I just implemented it like you suggested. The function header is now
If prefers the hist over the image as @jni suggested and returns the bin index in case no bin_centers are given. The only thing I didn't manage is the tuple-thingy @jni suggested. I assume one needs to add some more brakets to the function defintion, but I'm not sure how this works with bin_centers being optional. You know, I just recently arrived in snake county. ;-) Thanks again for your feedback. I appreciate it :-) Let me know what you think! Cheers, |
@haesleinhuepf I think you were expecting more magic than Python provides here. def threshold_otsu(image=None, nbins=256, *, histogram=None):
if histogram is not None:
counts, bin_centers = histogram
else:
counts, bin_centers = exposure.histogram(image, nbins) or def threshold_otsu(image=None, nbins=256, *, histogram=None):
if histogram is not None:
if isinstance(histogram, (tuple, list)):
counts, bin_centers = histogram
else:
counts = histogram
bin_centers = np.arange(counts.size)
else:
counts, bin_centers = exposure.histogram(image, nbins) |
added method threshold_otsu_from_histogram as shortcut in case the histogram is known in advance
Description
Hi all, hi @jni ,
this PR-draft refactors thresholding_otsu method so that one can call it also with a histogram instead of an image. I would like to use it in a GPU-accelerated image processing library where we can determine the histogram on the GPU, but lack threshold-method implementations.
I would wonder what you think about this implementation. If it makes sense, I would offer to implement the same startegy for other threshold methods.
Best,
Robert
Checklist
[x] Docstrings for all functions
[ ] Gallery example in
./doc/examples
(new features only)[ ] Benchmark in
./benchmarks
, if your changes aren't covered by an existing benchmark[ ] Unit tests
[ ] Clean style in the spirit of PEP8
For reviewers
later.
__init__.py
.doc/release/release_dev.rst
.