diff mbox

[FFmpeg-devel,1/2] avfilter/vf_nlmeans: better weighting of centered pixel

Message ID 20180512202435.6479-1-onemda@gmail.com
State New
Headers show

Commit Message

Paul B Mahol May 12, 2018, 8:24 p.m. UTC
Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavfilter/vf_nlmeans.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Comments

Clément Bœsch May 18, 2018, 5:18 p.m. UTC | #1
On Sat, May 12, 2018 at 10:24:34PM +0200, Paul B Mahol wrote:
> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> ---
>  libavfilter/vf_nlmeans.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/libavfilter/vf_nlmeans.c b/libavfilter/vf_nlmeans.c
> index 82e779ce85..6c9c9d312d 100644
> --- a/libavfilter/vf_nlmeans.c
> +++ b/libavfilter/vf_nlmeans.c
> @@ -39,6 +39,7 @@
>  #include "video.h"
>  
>  struct weighted_avg {
> +    float max_weight;
>      float total_weight;
>      float sum;
>  };
> @@ -403,6 +404,7 @@ static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs
>              if (patch_diff_sq < s->max_meaningful_diff) {
>                  const unsigned weight_lut_idx = patch_diff_sq * s->pdiff_lut_scale;
>                  const float weight = s->weight_lut[weight_lut_idx]; // exp(-patch_diff_sq * s->pdiff_scale)
> +                wa[x].max_weight = FFMAX(weight, wa[x].max_weight);
>                  wa[x].total_weight += weight;
>                  wa[x].sum += weight * src[x];
>              }
> @@ -422,8 +424,10 @@ static void weight_averages(uint8_t *dst, ptrdiff_t dst_linesize,
>      for (y = 0; y < h; y++) {
>          for (x = 0; x < w; x++) {
>              // Also weight the centered pixel
> -            wa[x].total_weight += 1.f;
> -            wa[x].sum += 1.f * src[x];
> +            if (!isnormal(wa[x].max_weight))
> +                wa[x].max_weight = 1.f;
> +            wa[x].total_weight += wa[x].max_weight;
> +            wa[x].sum += src[x] * wa[x].max_weight;
>              dst[x] = av_clip_uint8(wa[x].sum / wa[x].total_weight);
>          }
>          dst += dst_linesize;

Do you mind adding a cpw/center-pixel-weight option with multiple modes?
"one" and "max" for a start would be nice, then eventually advanced modes
can be added. Please also mention https://arxiv.org/pdf/1211.1656 at least
in the commit description.
diff mbox

Patch

diff --git a/libavfilter/vf_nlmeans.c b/libavfilter/vf_nlmeans.c
index 82e779ce85..6c9c9d312d 100644
--- a/libavfilter/vf_nlmeans.c
+++ b/libavfilter/vf_nlmeans.c
@@ -39,6 +39,7 @@ 
 #include "video.h"
 
 struct weighted_avg {
+    float max_weight;
     float total_weight;
     float sum;
 };
@@ -403,6 +404,7 @@  static int nlmeans_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs
             if (patch_diff_sq < s->max_meaningful_diff) {
                 const unsigned weight_lut_idx = patch_diff_sq * s->pdiff_lut_scale;
                 const float weight = s->weight_lut[weight_lut_idx]; // exp(-patch_diff_sq * s->pdiff_scale)
+                wa[x].max_weight = FFMAX(weight, wa[x].max_weight);
                 wa[x].total_weight += weight;
                 wa[x].sum += weight * src[x];
             }
@@ -422,8 +424,10 @@  static void weight_averages(uint8_t *dst, ptrdiff_t dst_linesize,
     for (y = 0; y < h; y++) {
         for (x = 0; x < w; x++) {
             // Also weight the centered pixel
-            wa[x].total_weight += 1.f;
-            wa[x].sum += 1.f * src[x];
+            if (!isnormal(wa[x].max_weight))
+                wa[x].max_weight = 1.f;
+            wa[x].total_weight += wa[x].max_weight;
+            wa[x].sum += src[x] * wa[x].max_weight;
             dst[x] = av_clip_uint8(wa[x].sum / wa[x].total_weight);
         }
         dst += dst_linesize;