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

Fix wrong implementation of unsharpen filter #2079

Merged
merged 1 commit into from
Dec 24, 2023
Merged

Fix wrong implementation of unsharpen filter #2079

merged 1 commit into from
Dec 24, 2023

Conversation

Juliapixel
Copy link
Contributor

This PR fixes the crate's current implementation of the unsharpen filter, which currently does not work in the intended way. Normally, in order to calculate the unsharpen filter, the following steps are taken:

  • a blurred version of the image is generated
  • that version is subtracted from the original, giving us their difference, that can be either negative or positive.
  • the difference is filtered by a minimum threshold parameter, so that absolute difference values that lie below a certain limit are not used in the filter
  • the filtered difference is multiplied by an amount parameter, which controls the strength of the filter (this PR does not add that parameter)
  • the result of that is added to the original image

The current way this is done doesn't take into account that the difference that has to be added to the original image can be both positive and negative, returning less sharp images than the originals. That's what this PR aims to fix.

It should also be noted that:

  • The current public API for the imageops::unsharpen() function is not great, using a datatype-dependent threshold value, and lacking the very common amount or strength value that is present in almost all applications that have this filter.
  • Neither the current nor this PR's implementation work properly with high dynamic range float-based images.

Image examples:

Current:
An image showing the current implementation of the unsharpen filter

This PR:
An image showing this PR's implementation of the unsharpen filter

Original:
The original image

Other examples of the desired effect can also be found on the unsharp masking Wikipedia page

I license past and future contributions under the dual MIT/Apache-2.0 license, allowing licensees to choose either at their option.

@fintelia fintelia merged commit 7197841 into image-rs:master Dec 24, 2023
35 checks passed
@fintelia
Copy link
Contributor

Thanks!

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.

2 participants