├── test.py
├── LICENSE
├── README.md
└── bj_delta.py
/test.py:
--------------------------------------------------------------------------------
1 | from bj_delta import *
2 |
3 |
4 | print("Test 1")
5 | Rate1 = np.array([686.76, 309.58, 157.11, 85.95])
6 | PSNR1 = np.array([40.28, 37.18, 34.24, 31.42])
7 | Rate2 = np.array([893.34, 407.8, 204.93, 112.75])
8 | PSNR2 = np.array([40.39, 37.21, 34.17, 31.24])
9 |
10 | print("BD-PSNR: ", bj_delta(Rate1, PSNR1, Rate2, PSNR2, mode=0))
11 | print("BD-RATE: ", bj_delta(Rate1, PSNR1, Rate2, PSNR2, mode=1))
12 |
13 | print("Test 2")
14 | Rate1 = np.array([0.001, 0.005, 0.020, 0.100, 0.7500])
15 | PSNR1 = np.array([18.02, 20.57, 23.09, 27.71, 35.74])
16 | Rate2 = np.array([0.00103, 0.00507, 0.02098, 0.09638, 0.73051])
17 | PSNR2 = np.array([24.81, 29.08, 33.09, 37.27, 43.12])
18 |
19 | print("BD-PSNR: ", bj_delta(Rate1, PSNR1, Rate2, PSNR2, mode=0))
20 | print("BD-RATE: ", bj_delta(Rate1, PSNR1, Rate2, PSNR2, mode=1))
21 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 João Ascenso
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Bjontegaard metric computation
2 |
3 | ## Introduction
4 | Bjontegaard's metric computes the average gain in PSNR or the average saving in bitrate (in %)
5 | between two rate-distortion curves [1][2].
6 |
7 | Similar to other packages [3] this function allows to compute the metric for more than 4 RD points,
8 | but in this case for the python language.
9 |
10 | ## Usage
11 | Check the test.py for some simple tests.
12 | ```
13 | from bj_delta import *
14 | bd_psnr = bj_delta(Rate1, PSNR1, Rate2, PSNR2, mode=0))
15 | bd_rate = bj_delta(Rate1, PSNR1, Rate2, PSNR2, mode=1))
16 | ```
17 | * Rate1,PSNR1: RD points for curve 1.
18 | * Rate2,PSNR2: RD points for curve 2.
19 | * Mode: 0 for average PSNR difference and 1 for average bitrate savings (%).
20 | * Returns the calculated Bjontegaard metric (BD-Rate or BD-PSNR).
21 |
22 | ## References
23 | [1] G. Bjontegaard, "Calculation of average PSNR differences between RD-curves", VCEG-M33, Austin, TX, USA, April 2001.
24 | [2] S. Pateux, J. Jung, "An excel add-in for computing Bjontegaard metric and its evolution", VCEG-AE07, Marrakech, MA, January 2007.
25 | [3] G. Valenzise, https://www.mathworks.com/matlabcentral/fileexchange/27798-bjontegaard-metric.
26 |
--------------------------------------------------------------------------------
/bj_delta.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | # BD-Rate and BD-PNSR computation
3 | # (c) Joao Ascenso (joao.ascenso@lx.it.pt)
4 |
5 |
6 | def bj_delta(R1, PSNR1, R2, PSNR2, mode=0):
7 | lR1 = np.log(R1)
8 | lR2 = np.log(R2)
9 |
10 | # find integral
11 | if mode == 0:
12 | # least squares polynomial fit
13 | p1 = np.polyfit(lR1, PSNR1, 3)
14 | p2 = np.polyfit(lR2, PSNR2, 3)
15 |
16 | # integration interval
17 | min_int = max(min(lR1), min(lR2))
18 | max_int = min(max(lR1), max(lR2))
19 |
20 | # indefinite integral of both polynomial curves
21 | p_int1 = np.polyint(p1)
22 | p_int2 = np.polyint(p2)
23 |
24 | # evaluates both poly curves at the limits of the integration interval
25 | # to find the area
26 | int1 = np.polyval(p_int1, max_int) - np.polyval(p_int1, min_int)
27 | int2 = np.polyval(p_int2, max_int) - np.polyval(p_int2, min_int)
28 |
29 | # find avg diff between the areas to obtain the final measure
30 | avg_diff = (int2-int1)/(max_int-min_int)
31 | else:
32 | # rate method: sames as previous one but with inverse order
33 | p1 = np.polyfit(PSNR1, lR1, 3)
34 | p2 = np.polyfit(PSNR2, lR2, 3)
35 |
36 | # integration interval
37 | min_int = max(min(PSNR1), min(PSNR2))
38 | max_int = min(max(PSNR1), max(PSNR2))
39 |
40 | # indefinite interval of both polynomial curves
41 | p_int1 = np.polyint(p1)
42 | p_int2 = np.polyint(p2)
43 |
44 | # evaluates both poly curves at the limits of the integration interval
45 | # to find the area
46 | int1 = np.polyval(p_int1, max_int) - np.polyval(p_int1, min_int)
47 | int2 = np.polyval(p_int2, max_int) - np.polyval(p_int2, min_int)
48 |
49 | # find avg diff between the areas to obtain the final measure
50 | avg_exp_diff = (int2-int1)/(max_int-min_int)
51 | avg_diff = (np.exp(avg_exp_diff)-1)*100
52 | return avg_diff
53 |
--------------------------------------------------------------------------------