Skip to content

Commit

Permalink
use 0.0 comparisions instead of f64::epsilon for kapur
Browse files Browse the repository at this point in the history
  • Loading branch information
o-tho committed Dec 7, 2024
1 parent 0c01163 commit eccade9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-github-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
~/.cargo/git
~/.cargo/registry
**/target
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/Cargo.lock') }}
key: ${{ runner.os }}-build-${{ hashFiles('**/Cargo.lock') }}

- name: Install dependencies
run: |
Expand Down
13 changes: 8 additions & 5 deletions src/image_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ fn kapur_level(img: &GrayImage) -> u8 {
// The H_s in the article. These are the entropies attached to the
// distributions p[0],...,p[s].
let mut h = [0.0f64; 256];
if p[0] > f64::EPSILON {
if p[0] > 0.0 {
h[0] = -p[0] * p[0].ln();
}
for s in 1..=255 {
if p[s] > f64::EPSILON {
if p[s] > 0.0 {
h[s] = h[s - 1] - p[s] * p[s].ln();
} else {
h[s] = h[s - 1]
Expand All @@ -48,12 +48,15 @@ fn kapur_level(img: &GrayImage) -> u8 {
let mut best_threshold = usize::MIN;

for s in 0..=255 {
let pq = (cum_p[s]) * (1.0 - cum_p[s]);
if pq <= 0.0 {
continue;
}

// psi_s is the total entropy of foreground and background at threshold
// level s. Instead of computing them separately, equation (18) in the
// article, which simplifies this to this:
let psi_s = (cum_p[s] * (1.0 - cum_p[s])).ln()
+ h[s] / cum_p[s]
+ (h[255] - h[s]) / (1.0 - cum_p[s]);
let psi_s = pq.ln() + h[s] / cum_p[s] + (h[255] - h[s]) / (1.0 - cum_p[s]);

if psi_s > max_entropy {
max_entropy = psi_s;
Expand Down

0 comments on commit eccade9

Please sign in to comment.