Skip to content

Corrected energy calculation in chan vese - #6902

Merged
lagru merged 4 commits into
scikit-image:mainfrom
GGoussar:correct-cv-energy
Apr 24, 2023
Merged

lagru merged 4 commits into
scikit-image:mainfrom
GGoussar:correct-cv-energy

Conversation

@GGoussar

Copy link
Copy Markdown
Contributor

Description

Closes #6889

The previous implementation used an integral over curvature instead of an integral over length. The present change intends to remedy this. While integrating over absolute curvature would also give a useful indication, the current implementation fits more closely to the function that needs to be minimized.

The new length-term energy is now computed as μ·δ(Φ)·|∇Φ| instead of μ·κ(Φ). Only the reported "energy" is impacted by this change, as the level set calculation appears to have been implemented by closely following the code in the paper by P. Getreuer. As such the behavior of the algorithm should not be severely affected.

Checklist

  • 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
  • Descriptive commit messages (see below)

For reviewers

  • Check that the PR title is short, concise, and will make sense 1 year
    later.
  • Check that new functions are imported in corresponding __init__.py.
  • Check that new features, API changes, and deprecations are mentioned in
    doc/release/release_dev.rst.
  • There is a bot to help automate backporting a PR to an older branch. For
    example, to backport to v0.19.x after merging, add the following in a PR
    comment: @meeseeksdev backport to v0.19.x
  • To run benchmarks on a PR, add the run-benchmark label. To rerun, the label
    can be removed and then added again. The benchmark output can be checked in
    the "Actions" tab.

The previous implementation used an integral over curvature instead
of an integral over length. The present change intends to remedy
this. While integrating over absolute curvature would also give a
useful indication, the current implementation fits more closely to
the minimized value.
@stefanv

stefanv commented Apr 21, 2023

Copy link
Copy Markdown
Member

Thank you so much for investigating this issue @GGoussar. Would you mind adding a comment to the code that indicates which equation from the paper is implemented, and perhaps give some of the mathematical context, as in the description of this PR? Subtle details like this can be hard to uncover in the future when reading the code.

@GGoussar

GGoussar commented Apr 22, 2023 •

Copy link
Copy Markdown
Contributor Author

Thank you so much for investigating this issue @GGoussar. Would you mind adding a comment to the code that indicates which equation from the paper is implemented, and perhaps give some of the mathematical context, as in the description of this PR? Subtle details like this can be hard to uncover in the future when reading the code.

Would replacing the docstring of _cv_evergy as follows be sufficient?

EDIT: Should I also maybe add similar comment to _cv_calculate_variation?

def _cv_energy(image, phi, mu, lambda1, lambda2):
    """Returns the total 'energy' of the current level set function.
    
    This corresponds to equation (7) of the paper by Pascal Getreuer,
    which is the weighted sum of the following:
    (A) the length of the contour produced by the intersection of the
    zero values of the level set,
    (B) the area of the "foreground" (area of the image where the 
    level set is positive)
    (C) the variance of the image inside the foreground
    (D) the variance of the image outside of the foreground
    
    Each value is computed for each pixel, and then summed. The weight
    of (B) is set to 0 in this implementation.
    """
    H = _cv_heavyside(phi)
    avgenergy = _cv_difference_from_average_term(image, H, lambda1, lambda2)
    lenenergy = _cv_edge_length_term(phi, mu)
    return np.sum(avgenergy) + np.sum(lenenergy)

@stefanv

stefanv commented Apr 22, 2023 •

Copy link
Copy Markdown
Member

Perfect on both counts, thanks!

The functions `_cv_calculate_variation` and `_cv_energy` now contain
a more in-depth explanation that enables the reader to undertand
where they can find the orginal equations that these functions
implement.
@stefanv

stefanv commented Apr 24, 2023

Copy link
Copy Markdown
Member

@lagru Mind taking a look? I think this is good to go.

@lagru lagru left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can follow this looks good. Thank you for the documentation effort. :)

Comment thread skimage/segmentation/_chan_vese.py Resolved
@lagru
lagru merged commit c30ffa2 into scikit-image:main Apr 24, 2023
@lagru lagru added the 🩹 type: Bug fix Fixes unexpected or incorrect behavior label Apr 24, 2023
@lagru lagru added this to the 0.21 milestone Apr 24, 2023
@stefanv

stefanv commented Apr 24, 2023

Copy link
Copy Markdown
Member

Thanks very much @GGoussar!

@lagru lagru changed the title Corrected energy calculation in chan vese Corrected energy calculation in chan vese May 31, 2023
rapids-bot Bot pushed a commit to rapidsai/cucim that referenced this pull request Jul 11, 2023
# Description

This MR ports several recent changes implemented upstream in scikit-image 0.21rc0. Some are just minor refactoring or documentation fixes, but I have commented on the more significant changes below so we can reuse that for the release note.

If preferred, I can split the items below into separate small PRs.

## API Changes / Deprecations
- The `random_state` argument of `medial_axis` and `unsupervised_wiener` is now deprecated and will be removed in the future. The new argument name, `seed`, should be used instead.

- The formerly private function `_invariant_denoise` has been renamed `denoise_invariant` and is now part of the public `cucim.skimage.restoration` API.

- The existing function `cucim.skimage.color.get_xyz_coords` has been renamed `cucim.skimage.color.xyz_tristimulus_values`. The former function name is deprecated and will be removed in the future.

## Enhancements
- A new `return_mapping` option was added to `join_segmentations`. This provides an additional output with a mapping between the labels in the joined segmentation and the original ones.

- Added support for y-axis shear to the [2D AffineTransform](scikit-image/scikit-image#6752)

## Backwards Incompatible Changes
- A bug fix to the energy calculation in `chan_vese` may result in different output from previous versions.

## Bug Fixes
- Fixed [bug in energy calculation in Chan-Vese](scikit-image/scikit-image#6902)

- Fixed a [bug in 2D shear calculations for `AffineTransform`](scikit-image/scikit-image#6717)

Authors:
  - Gregory Lee (https://github.com/grlee77)
  - https://github.com/jakirkham

Approvers:
  - Gigon Bae (https://github.com/gigony)

URL: #573
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🩹 type: Bug fix Fixes unexpected or incorrect behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chan-Vese Segmentation length term energy negative

3 participants