diff --git a/.github/workflows/deploy-github-pages.yml b/.github/workflows/deploy-github-pages.yml index f1a47ce..b75bb72 100644 --- a/.github/workflows/deploy-github-pages.yml +++ b/.github/workflows/deploy-github-pages.yml @@ -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: | diff --git a/src/image_helpers.rs b/src/image_helpers.rs index b86ab8b..abb05a9 100644 --- a/src/image_helpers.rs +++ b/src/image_helpers.rs @@ -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] @@ -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;