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

Implement Kapur's algorithm for binary thresholding (#696) #699

Merged
merged 3 commits into from
Dec 10, 2024

Conversation

o-tho
Copy link
Contributor

@o-tho o-tho commented Dec 6, 2024

Kapur's algorithm uses an entropy approach to find a good segmentation threshold for gray images.

This implementation of Kapur's algorithm sticks closely to the notation of the original article (https://doi.org/10.1016/0734-189X(85)90125-2), so it is much easier to check for correctness. We also avoid a double loop, so this only needs 4*256 computations instead of 256^2.

o-tho added 2 commits December 4, 2024 12:39
This implementation sticks closely to the notation of the original article and uses an algebraic identity which simplifies computation.
@cospectrum
Copy link
Contributor

Why not replace f64::EPSILON with 0.0?

@o-tho
Copy link
Contributor Author

o-tho commented Dec 8, 2024

That's a very good question. While playing around with different possible implementations I got concerned about summing up floats of different orders of magnitude and wanted to give myself some safety margin, but the p*ln(p) are completely stable and I never changed it back.

@theotherphil
Copy link
Contributor

Thanks! Please can you make the change suggested by clippy (not obviously an improvement, but not worse and this is easier than ad-hoc disabling of lints) and then I’ll merge.

@cospectrum
Copy link
Contributor

I will make a PR to lock the nightly channel for clippy

@o-tho
Copy link
Contributor Author

o-tho commented Dec 8, 2024

Thanks! Please can you make the change suggested by clippy (not obviously an improvement, but not worse and this is easier than ad-hoc disabling of lints) and then I’ll merge.

I can absolutely do that, but just for the record: The clippy change is about code that has nothing to do with the PR. You still want me to push it here?

@theotherphil
Copy link
Contributor

Ah, I hadn’t spotted that. Fixed by @cospectrum in #700.

Thanks for the new feature.

@theotherphil theotherphil merged commit ab9e19c into image-rs:master Dec 10, 2024
14 of 16 checks passed
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 this pull request may close these issues.

3 participants