opencv_contrib icon indicating copy to clipboard operation
opencv_contrib copied to clipboard

Implement fast guided filter

Open w43322 opened this issue 1 year ago • 3 comments

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

  • [x] I agree to contribute to the project under Apache 2 License.
  • [x] 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
  • [x] The PR is proposed to the proper branch ~~- [ ] There is a reference to the original bug report and related work~~
  • [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name.
  • [x] The feature is well documented and sample code can be built with the project CMake

w43322 avatar Jan 19 '24 09:01 w43322

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

w43322 avatar Feb 02 '24 03:02 w43322

@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.

vpisarev avatar Feb 09 '24 08:02 vpisarev

@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.

w43322 avatar Feb 11 '24 16:02 w43322