lavfi/delogo: use weighted interpolation

The original delogo algorithm interpolates both horizontally and
vertically and uses the average to compute the resulting sample. This
works reasonably well when the logo area is almost square. However
when the logo area is significantly larger than high or higher than
large, the result is largely suboptimal.

The issue can be clearly seen by testing the delogo filter with a fake
logo area that is 200 pixels large and 2 pixels high. Vertical
interpolation gives a very good result in that case, horizontal
interpolation gives a very bad result, and the overall result is poor,
because both are given the same weight.

Even when the logo is roughly square, the current algorithm gives poor
results on the borders of the logo area, because it always gives
horizontal and vertical interpolations an equal weight, and this is
suboptimal on borders. For example, in the middle of the left hand
side border of the logo, you want to trust the left known point much
more than the right known point (which the current algorithm already
does) but also much more than the top and bottom known points (which
the current algorithm doesn't do.)

By properly weighting each known point when computing the value of
each interpolated pixel, the visual result is much better, especially
on borders and/or for high or large logo areas.

The algorithm I implemented guarantees that the weight of each of the
4 known points directly depends on its distance to the interpolated
point. It is largely inspired from the original algorithm, the key
difference being that it computes the relative weights globally
instead of separating the vertical and horizontal interpolations and
combining them afterward.

Signed-off-by: Jean Delvare <khali@linux-fr.org>
Signed-off-by: Stefano Sabatini <stefasab@gmail.com>
This commit is contained in:
Jean Delvare 2013-06-26 14:50:37 +02:00 committed by Stefano Sabatini
parent b42bcaef29
commit 16fd75ceec
2 changed files with 126 additions and 117 deletions

View File

@ -1,6 +1,7 @@
/*
* Copyright (c) 2002 Jindrich Makovicka <makovick@gmail.com>
* Copyright (c) 2011 Stefano Sabatini
* Copyright (c) 2013 Jean Delvare <khali@linux-fr.org>
*
* This file is part of FFmpeg.
*
@ -22,7 +23,8 @@
/**
* @file
* A very simple tv station logo remover
* Ported from MPlayer libmpcodecs/vf_delogo.c.
* Originally imported from MPlayer libmpcodecs/vf_delogo.c,
* the algorithm was later improved.
*/
#include "libavutil/common.h"
@ -58,8 +60,8 @@ static void apply_delogo(uint8_t *dst, int dst_linesize,
int logo_x, int logo_y, int logo_w, int logo_h,
int band, int show, int direct)
{
int x, y;
int interp, dist;
int x, y, dist;
uint64_t interp, weightl, weightr, weightt, weightb;
uint8_t *xdst, *xsrc;
uint8_t *topleft, *botleft, *topright;
@ -90,23 +92,30 @@ static void apply_delogo(uint8_t *dst, int dst_linesize,
for (x = logo_x1+1,
xdst = dst+logo_x1+1,
xsrc = src+logo_x1+1; x < logo_x2-1; x++, xdst++, xsrc++) {
/* Weighted interpolation based on relative distances */
weightl = (uint64_t) (logo_x2-1-x) * (y-logo_y1) * (logo_y2-1-y);
weightr = (uint64_t)(x-logo_x1) * (y-logo_y1) * (logo_y2-1-y);
weightt = (uint64_t)(x-logo_x1) * (logo_x2-1-x) * (logo_y2-1-y);
weightb = (uint64_t)(x-logo_x1) * (logo_x2-1-x) * (y-logo_y1);
interp =
(topleft[src_linesize*(y-logo_y -yclipt)] +
topleft[src_linesize*(y-logo_y-1-yclipt)] +
topleft[src_linesize*(y-logo_y+1-yclipt)]) * (logo_w-(x-logo_x))/logo_w
topleft[src_linesize*(y-logo_y+1-yclipt)]) * weightl
+
(topright[src_linesize*(y-logo_y-yclipt)] +
topright[src_linesize*(y-logo_y-1-yclipt)] +
topright[src_linesize*(y-logo_y+1-yclipt)]) * (x-logo_x)/logo_w
topright[src_linesize*(y-logo_y+1-yclipt)]) * weightr
+
(topleft[x-logo_x-xclipl] +
topleft[x-logo_x-1-xclipl] +
topleft[x-logo_x+1-xclipl]) * (logo_h-(y-logo_y))/logo_h
topleft[x-logo_x+1-xclipl]) * weightt
+
(botleft[x-logo_x-xclipl] +
botleft[x-logo_x-1-xclipl] +
botleft[x-logo_x+1-xclipl]) * (y-logo_y)/logo_h;
interp /= 6;
botleft[x-logo_x+1-xclipl]) * weightb;
interp /= (weightl + weightr + weightt + weightb) * 3U;
if (y >= logo_y+band && y < logo_y+logo_h-band &&
x >= logo_x+band && x < logo_x+logo_w-band) {

View File

@ -1,110 +1,110 @@
#tb 0: 32768/982057
0, 0, 0, 1, 126720, 0x689de87e
0, 1, 1, 1, 126720, 0x3db9e91c
0, 2, 2, 1, 126720, 0x3db9e91c
0, 3, 3, 1, 126720, 0x3db9e91c
0, 4, 4, 1, 126720, 0xfa6ae95e
0, 5, 5, 1, 126720, 0x5bcbf0e6
0, 6, 6, 1, 126720, 0x94a0f126
0, 7, 7, 1, 126720, 0x0250f106
0, 8, 8, 1, 126720, 0xcf6ab4bc
0, 9, 9, 1, 126720, 0x429eb57c
0, 10, 10, 1, 126720, 0x3bf0b5bc
0, 11, 11, 1, 126720, 0xcaedb591
0, 12, 12, 1, 126720, 0xa492b5ec
0, 13, 13, 1, 126720, 0x2431b85c
0, 14, 14, 1, 126720, 0x8283b8dc
0, 15, 15, 1, 126720, 0xd71bb871
0, 16, 16, 1, 126720, 0x698eb5cc
0, 17, 17, 1, 126720, 0x4719aa98
0, 18, 18, 1, 126720, 0x9ca1962c
0, 19, 19, 1, 126720, 0x18cda460
0, 20, 20, 1, 126720, 0xc230b716
0, 21, 21, 1, 126720, 0x8451a4e2
0, 22, 22, 1, 126720, 0x59e9a7ea
0, 23, 23, 1, 126720, 0xc77ca73d
0, 24, 24, 1, 126720, 0x725fb976
0, 25, 25, 1, 126720, 0xb30da3b3
0, 26, 26, 1, 126720, 0x7af2ea86
0, 27, 27, 1, 126720, 0x40d4b4eb
0, 28, 28, 1, 126720, 0x49d00307
0, 29, 29, 1, 126720, 0x0654849c
0, 30, 30, 1, 126720, 0xe46d0107
0, 31, 31, 1, 126720, 0xa483b963
0, 32, 32, 1, 126720, 0xd0e903f0
0, 33, 33, 1, 126720, 0x964ed592
0, 34, 34, 1, 126720, 0x23fbdb3c
0, 35, 35, 1, 126720, 0x59fdace5
0, 36, 36, 1, 126720, 0xb1e37954
0, 37, 37, 1, 126720, 0x8ed9c554
0, 38, 38, 1, 126720, 0xe3c4b39f
0, 39, 39, 1, 126720, 0xfd17e0ce
0, 40, 40, 1, 126720, 0xf26e1dcc
0, 41, 41, 1, 126720, 0x13cc783c
0, 42, 42, 1, 126720, 0x47ad47a1
0, 43, 43, 1, 126720, 0x427c8b0d
0, 44, 44, 1, 126720, 0x59d99901
0, 45, 45, 1, 126720, 0xc40707da
0, 46, 46, 1, 126720, 0xcd060dce
0, 47, 47, 1, 126720, 0xed4024f6
0, 48, 48, 1, 126720, 0x7decd2b4
0, 49, 49, 1, 126720, 0xd1d2e730
0, 50, 50, 1, 126720, 0x77cee457
0, 51, 51, 1, 126720, 0xe78d02c0
0, 52, 52, 1, 126720, 0xad0beb29
0, 53, 53, 1, 126720, 0xc414eea2
0, 54, 54, 1, 126720, 0x6a15f17d
0, 55, 55, 1, 126720, 0x516027f6
0, 56, 56, 1, 126720, 0x4eda9dce
0, 57, 57, 1, 126720, 0x7d9bdba3
0, 58, 58, 1, 126720, 0x7aa3d5c0
0, 59, 59, 1, 126720, 0x7c7a04f9
0, 60, 60, 1, 126720, 0x3e8fb6cc
0, 61, 61, 1, 126720, 0xd5474916
0, 62, 62, 1, 126720, 0xf3f62bab
0, 63, 63, 1, 126720, 0x2f054987
0, 64, 64, 1, 126720, 0x974c2e81
0, 65, 65, 1, 126720, 0xe7e28a97
0, 66, 66, 1, 126720, 0x45e38b41
0, 67, 67, 1, 126720, 0x169c7f19
0, 68, 68, 1, 126720, 0x91d90ee8
0, 69, 69, 1, 126720, 0xdd653e24
0, 70, 70, 1, 126720, 0x0da598c4
0, 71, 71, 1, 126720, 0x687e62cc
0, 72, 72, 1, 126720, 0x7631232d
0, 73, 73, 1, 126720, 0xbd1ea826
0, 74, 74, 1, 126720, 0xb55f7f4b
0, 75, 75, 1, 126720, 0x923f3fc9
0, 76, 76, 1, 126720, 0x15515301
0, 77, 77, 1, 126720, 0x9ee066e5
0, 78, 78, 1, 126720, 0x7c21664b
0, 79, 79, 1, 126720, 0x36849100
0, 80, 80, 1, 126720, 0x08b1f61a
0, 81, 81, 1, 126720, 0x5bfca6e2
0, 82, 82, 1, 126720, 0x929f60e3
0, 83, 83, 1, 126720, 0xa2b55c29
0, 84, 84, 1, 126720, 0x68bd3ff3
0, 85, 85, 1, 126720, 0x30db5b29
0, 86, 86, 1, 126720, 0x00578f9b
0, 87, 87, 1, 126720, 0x18368642
0, 88, 88, 1, 126720, 0xbcb83a80
0, 89, 89, 1, 126720, 0x90f36b72
0, 90, 90, 1, 126720, 0x85e46522
0, 91, 91, 1, 126720, 0x2429660a
0, 92, 92, 1, 126720, 0xf283dfe2
0, 93, 93, 1, 126720, 0x896b27dc
0, 94, 94, 1, 126720, 0x5af4f961
0, 95, 95, 1, 126720, 0x31897085
0, 96, 96, 1, 126720, 0x441ce33e
0, 97, 97, 1, 126720, 0x903f8009
0, 98, 98, 1, 126720, 0xbdf33dba
0, 99, 99, 1, 126720, 0x8a364f36
0, 100, 100, 1, 126720, 0xda5513f6
0, 101, 101, 1, 126720, 0xd60012b3
0, 102, 102, 1, 126720, 0x67bce7be
0, 103, 103, 1, 126720, 0x697e6174
0, 104, 104, 1, 126720, 0xbe3e3e90
0, 105, 105, 1, 126720, 0xf3e4bba6
0, 106, 106, 1, 126720, 0x8124a679
0, 107, 107, 1, 126720, 0x58d1acde
0, 108, 108, 1, 126720, 0xd8a15ba3
0, 0, 0, 1, 126720, 0x41c3ebfc
0, 1, 1, 1, 126720, 0x16dfec9a
0, 2, 2, 1, 126720, 0x16dfec9a
0, 3, 3, 1, 126720, 0x16dfec9a
0, 4, 4, 1, 126720, 0xd390ecdc
0, 5, 5, 1, 126720, 0xf2eaf468
0, 6, 6, 1, 126720, 0x2bcef4a8
0, 7, 7, 1, 126720, 0x996ff488
0, 8, 8, 1, 126720, 0x5adbb7e4
0, 9, 9, 1, 126720, 0xce00b8a4
0, 10, 10, 1, 126720, 0xc752b8e4
0, 11, 11, 1, 126720, 0xd8c9b8b0
0, 12, 12, 1, 126720, 0x3003b914
0, 13, 13, 1, 126720, 0xaf93bb84
0, 14, 14, 1, 126720, 0x0df4bc04
0, 15, 15, 1, 126720, 0xe4f7bb90
0, 16, 16, 1, 126720, 0xf4f0b8f4
0, 17, 17, 1, 126720, 0x6bbbae4c
0, 18, 18, 1, 126720, 0x189799b5
0, 19, 19, 1, 126720, 0xb3aba7b9
0, 20, 20, 1, 126720, 0xaa58ba49
0, 21, 21, 1, 126720, 0xd458a85d
0, 22, 22, 1, 126720, 0xe2f7ab39
0, 23, 23, 1, 126720, 0xda46aa66
0, 24, 24, 1, 126720, 0x5a87bca9
0, 25, 25, 1, 126720, 0x30aea71a
0, 26, 26, 1, 126720, 0xe7f2eafc
0, 27, 27, 1, 126720, 0xb181b566
0, 28, 28, 1, 126720, 0x06fc036b
0, 29, 29, 1, 126720, 0x5b1184d0
0, 30, 30, 1, 126720, 0xe0dd0159
0, 31, 31, 1, 126720, 0x984eb9c2
0, 32, 32, 1, 126720, 0x18de046e
0, 33, 33, 1, 126720, 0x7739d63b
0, 34, 34, 1, 126720, 0xa8aedbe1
0, 35, 35, 1, 126720, 0x3a8bad65
0, 36, 36, 1, 126720, 0xa0d879dd
0, 37, 37, 1, 126720, 0x9880c604
0, 38, 38, 1, 126720, 0xad34b4e2
0, 39, 39, 1, 126720, 0x62ece114
0, 40, 40, 1, 126720, 0x06321e84
0, 41, 41, 1, 126720, 0xb8a278f7
0, 42, 42, 1, 126720, 0xf3794872
0, 43, 43, 1, 126720, 0x40228bd8
0, 44, 44, 1, 126720, 0xd9eb99be
0, 45, 45, 1, 126720, 0xcd71085c
0, 46, 46, 1, 126720, 0x53b50e7d
0, 47, 47, 1, 126720, 0x115825d5
0, 48, 48, 1, 126720, 0xadb2d397
0, 49, 49, 1, 126720, 0xd8c9e7b3
0, 50, 50, 1, 126720, 0xb8b1e4c6
0, 51, 51, 1, 126720, 0xcf7102d2
0, 52, 52, 1, 126720, 0x9458eab9
0, 53, 53, 1, 126720, 0x9654ee85
0, 54, 54, 1, 126720, 0xf9c8f16b
0, 55, 55, 1, 126720, 0x37bb27c6
0, 56, 56, 1, 126720, 0x01f29e29
0, 57, 57, 1, 126720, 0x2090dc6b
0, 58, 58, 1, 126720, 0x3f6fd6cb
0, 59, 59, 1, 126720, 0x16110602
0, 60, 60, 1, 126720, 0x8f56b840
0, 61, 61, 1, 126720, 0x12f44aa7
0, 62, 62, 1, 126720, 0x8a3b2db5
0, 63, 63, 1, 126720, 0xea6e4b48
0, 64, 64, 1, 126720, 0xf72e3057
0, 65, 65, 1, 126720, 0xbcb28cb0
0, 66, 66, 1, 126720, 0xd8d08db4
0, 67, 67, 1, 126720, 0x93cd8232
0, 68, 68, 1, 126720, 0x58421200
0, 69, 69, 1, 126720, 0xb9754164
0, 70, 70, 1, 126720, 0xeb259b9c
0, 71, 71, 1, 126720, 0xaa726578
0, 72, 72, 1, 126720, 0xe66825b1
0, 73, 73, 1, 126720, 0x9742aa58
0, 74, 74, 1, 126720, 0x8fa5815e
0, 75, 75, 1, 126720, 0xbaf341c7
0, 76, 76, 1, 126720, 0x94ed5495
0, 77, 77, 1, 126720, 0x0c11685f
0, 78, 78, 1, 126720, 0x9ba46776
0, 79, 79, 1, 126720, 0x3ee0921d
0, 80, 80, 1, 126720, 0x736df713
0, 81, 81, 1, 126720, 0xed1aa7dc
0, 82, 82, 1, 126720, 0x916661df
0, 83, 83, 1, 126720, 0xa4e75cb4
0, 84, 84, 1, 126720, 0xb341408d
0, 85, 85, 1, 126720, 0x3dbf5bf7
0, 86, 86, 1, 126720, 0x03c990aa
0, 87, 87, 1, 126720, 0x4d598768
0, 88, 88, 1, 126720, 0xfd953b86
0, 89, 89, 1, 126720, 0xf9796c69
0, 90, 90, 1, 126720, 0xe125664e
0, 91, 91, 1, 126720, 0x7afc671f
0, 92, 92, 1, 126720, 0x5cb8e0d5
0, 93, 93, 1, 126720, 0x5d7d2928
0, 94, 94, 1, 126720, 0xa9f8faa8
0, 95, 95, 1, 126720, 0x2c767188
0, 96, 96, 1, 126720, 0x0188e449
0, 97, 97, 1, 126720, 0x8483812a
0, 98, 98, 1, 126720, 0x54953eb4
0, 99, 99, 1, 126720, 0x7980503e
0, 100, 100, 1, 126720, 0xc6931503
0, 101, 101, 1, 126720, 0xc15613f4
0, 102, 102, 1, 126720, 0xd59fe8fd
0, 103, 103, 1, 126720, 0x71ed62b7
0, 104, 104, 1, 126720, 0x4c154002
0, 105, 105, 1, 126720, 0x7219bca5
0, 106, 106, 1, 126720, 0x3a04a755
0, 107, 107, 1, 126720, 0x0784ad95
0, 108, 108, 1, 126720, 0x29575bd4