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 fast guided filter #3623

Merged
merged 1 commit into from May 22, 2024
Merged

Conversation

w43322
Copy link
Contributor

@w43322 w43322 commented Jan 19, 2024

This PR implements fast guided filter. The concept is explained in this paper.

TLDR:
Added a 'scale' parameter to the current guided filter interface, allowing resampling inside the algorithm, to speed up computation. The resampling has to be done inside guided filter because the intermediate result has to be combined with original input to generate the final result.
The performance and behavior of the default code path (scale == 1.0) is unaltered. before vs after

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
    - [ ] There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@w43322 w43322 changed the title [WIP] Implement fast guided filter Implement fast guided filter Jan 19, 2024
@w43322 w43322 marked this pull request as ready for review January 19, 2024 10:19
@w43322 w43322 marked this pull request as draft January 22, 2024 08:45
@w43322 w43322 force-pushed the fast-guided-filter branch 3 times, most recently from 62b1038 to 195184e Compare January 22, 2024 09:43
@w43322 w43322 marked this pull request as ready for review January 22, 2024 14:11
@w43322
Copy link
Contributor Author

w43322 commented Feb 2, 2024

last github workflow was timed out during execution, kindly asking for a re-run :) @opencv-alalek

@vpisarev
Copy link
Contributor

vpisarev commented Feb 9, 2024

@w43322, thanks for the contribution!

We would like to ask you to do a few things:

  1. use reciprocal for the scale: 1 => 1, 2 => 0.5, 3 => 0.33333333333... etc. This is to match cv::resize() semantics.
  2. replace INTER_NEAREST with INTER_LINEAR (also, please, try INTER_AREA) when downsampling image. I believe, it should improve the quality at the cost of some decrease of speed. We are going to optimize resize() even further, so the overhead should reduce with time.
  3. Looks like the tests have been modified to check NORM_INF only when scale==1. Can we offer a stricter test for scale != 1?
  4. Please, add CV_Assert(scale <= 1); to the function (once you convert scale to 1./scale). 'scale' notion can be confusing sometimes, we need to add protective checks in order to provide proper diagnostic to user.

@w43322
Copy link
Contributor Author

w43322 commented Feb 11, 2024

@vpisarev Hi, I've updated the PR, please take a look :)

  1. use reciprocal for the scale: 1 => 1, 2 => 0.5, 3 => 0.33333333333... etc. This is to match cv::resize() semantics.

  1. replace INTER_NEAREST with INTER_LINEAR (also, please, try INTER_AREA) when downsampling image. I believe, it should improve the quality at the cost of some decrease of speed. We are going to optimize resize() even further, so the overhead should reduce with time.

Did some tests, seems like the runtime is mostly similar across three methods - although INTER_AREA is noticeably slower with scale == 0.33, so I opted for INTER_LINEAR for now.
perf_test.txt

  1. Looks like the tests have been modified to check NORM_INF only when scale==1. Can we offer a stricter test for scale != 1?

Checking for NORM_INF only works for scale == 1 because it only works when the two images are exactly the same. It was used to check against reference implementation. Since some information is lost during the resampling process, this check won't work on scaled images.

I've added a new check for scaled images, making sure that the FastGuidedFilter image is less blurry than a naive resampling. Please let me know if you think there are any other checks that are viable.

  1. Please, add CV_Assert(scale <= 1); to the function (once you convert scale to 1./scale). 'scale' notion can be confusing sometimes, we need to add protective checks in order to provide proper diagnostic to user.

@asmorkalov asmorkalov self-requested a review May 22, 2024 08:48
@asmorkalov asmorkalov self-assigned this May 22, 2024
@asmorkalov asmorkalov merged commit 345371e into opencv:4.x May 22, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants