Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Usage

Reply
 
Thread Tools Search this Thread Display Modes
Old 18th February 2021, 15:33   #1  |  Link
Arx1meD
Registered User
 
Arx1meD's Avatar
 
Join Date: Feb 2021
Posts: 121
Brightness - restore dark and bright zones

This script will be useful for restoring dark and bright zones of a video or for smoothly changing luma.
Thanks to Didée for his Ylevels. This inspired me to create other functions.

Code:
#                        Brightness v1.2
#
# v1.0 - First release.
# v1.1 - Adapted for use in all versions of AviSynth.
# v1.2 - Changed the function for CurveL.
#        Now if 'light' > 0 bright areas will be brighter.
#        Changed the default value of 'light' to -20.

/*
Needed plugins: MaskTools2 v2.2.16 or greater.
    https://github.com/pinterf/masktools/releases
    http://avisynth.nl/index.php/MaskTools2/mt_lut

Brightness(mode, dark, light, bit, chroma, DarkLightZonesOnly, SaveMinMaxLuma)

mode:
-----
    "Line" - linear brightness variation.
    "Cos"  - changes brightness by Cosine function.
    "Cube" - changes brightness by a cubic power function.
    Default: "Cube"

dark:
-----
    The strength of changing dark zones.
    The recommended range is -30 ... 30. The range is not limited, in theory.
    Default: 20

light:
------
    The strength of changing light zones.
    The recommended range is -30...30.
    Default: -20

bit:
----
    Bit depth per channel in input clip.
    -1 - for autoscale in MaskTools2 (slower).
     0 - for autodetection (faster).
    8, 10, 12, 14, 16.
    Default: 0

chroma:
-------
    true  - it means "process"; set u = v = 3 in mt_lut() MaskTools2.
    false - it means "copy" or "copy first"; set u = v = 2.
    Default: false

DarkLightZonesOnly:
-------------------
    Changes only dark and light zones.
    true  or  false.
    If true, then "SaveMinMaxLuma" is ignored.
    Default: false

SaveMinMaxLuma:
---------------
    Smooth brightness change for dark and bright zones. Keep minimum (0) and maximum (255) value of luma range.
    true  or  false.
    Default: true

Usage Default:
Brightness(mode="Cube", dark=20, light=-20, bit=0, chroma=false, DarkLightZonesOnly=false, SaveMinMaxLuma=true)
or
Brightness("Cube", 20, -20, 0, false, false, true)
*/

function Brightness(clip input, string "mode", float "dark", float "light", int "bit", bool "chroma", bool "DarkLightZonesOnly", bool "SaveMinMaxLuma") {
mode   = Default(mode, "Cube")
dark   = Default(dark, 20)
light  = Default(light, -20)
bit    = Default(bit, 0)
chroma = Default(chroma, false)
DLZO   = Default(DarkLightZonesOnly, false)
SMML   = Default(SaveMinMaxLuma, true)

Assert(mode == "Line" || mode == "Cos" || mode == "Cube", "Brightness: Mode must be: Line, Cos, Cube")
Assert(bit == -1 || bit == 0 || bit == 8 || bit == 10 || bit == 12 || bit == 14 || bit == 16, \
       "Brightness: Bit depth per channel must be only: 8, 10, 12, 14, 16 or -1, 0 for autodetection")

Try { bpc = bit == 0 || bit == -1 ? input.BitsPerComponent : bit } Catch (msg) { bpc=8 }      #BitsPerComponent only in AVS+
scale_inputs = bit >= 0 ? "none" : bpc == 8 ? "none" : "allf"   #"all" range in 8 bits Y=16...235; "allf" range in 8 bits Y=0...255
use_expr = bit >= 0 ? 0 : bpc == 8 ? 0 : 2 
chrom = chroma ? "process" : "copy"

LUMA = bit >= 0 ? Pow(2.0, bpc) - 1 : 255.0
maxY = String(LUMA)
midY = String(LUMA/2.0)

stD = bit >= 0 ? String(dark * Pow(2.0, bpc - 8)) : String(dark)
stL = bit >= 0 ? String(light * Pow(2.0, bpc - 8)) : String(light)

k = String(mode == "Line" || mode == "Cube" ? 0 : 1)
n = String(mode == "Cube" ? 3 : 1) # odd numbers only
j = String(30) # even numbers only
l = string(21) # odd numbers only
m = String(SMML == true ? 1 : 0)
f = String(mode == "Line" ? 1.25 : mode == "Cos" ? 1.15 : 1.05)

curveD = dark == 0 ? "x" : !DLZO ? "x "+stD+" "+k+" 1 + 1 x "+midY+" / - "+n+" ^ * pi x * "+maxY+" / cos "+k+" * - * 1 1 x "+midY+" / - "+j+" ^ "+m+" * - * +"
                               \ : "x "+stD+" 1 x "+midY+" / - "+l+" ^ pi x * "+maxY+" / cos "+l+" ^ - * "+f+" * -"
curveL = light == 0 ? "x" : !DLZO ? "x "+stL+" "+k+" 1 + 1 x "+midY+" / - "+n+" ^ * pi x * "+maxY+" / cos "+k+" * - * 1 1 x "+midY+" / - "+j+" ^ "+m+" * - * -"
                                \ : "x "+stL+" 1 x "+midY+" / - "+l+" ^ pi x * "+maxY+" / cos "+l+" ^ - * "+f+" * +"

# function 1:  x + 'st'*((1+'k')*(1 - x/127.5)^'n' - 'k'*cos(x*pi/255))*(1 - 'm'*(1 - x/127.5)^'j')
# function 2:  x - 'f'*'st'*((1 - x/127.5)^'l' - cos(x*pi/255)^'l')

out = "x "+midY+" > "+curveL+" "+curveD+" ?"

mt_lut(input, expr=out, Y=3, chroma=chrom, scale_inputs=scale_inputs, use_expr=use_expr)
# mt_lut(input, expr=out, Y=3, chroma=chrom) # for MaskTools v2.0a48
}
Explanation of what the script does.
1. Basic functions for smoothly changing luma:

Backup copy formulas 1 graphs 1
2. Functions for changes only dark and light zones:

Backup copy formulas 2 graphs 2

Examples:
Brightness(mode="Cube", dark=20, light=0, bit=0, chroma=false, DarkLightZonesOnly=true, SaveMinMaxLuma=true)

Backup copy Source frame Restored frame
Comparison of original and restored clip

Last edited by Arx1meD; 16th February 2023 at 16:56. Reason: Changed image hosting. In mt_lut changed yexpr to expr, use_expr to 2
Arx1meD is offline   Reply With Quote
Old 12th February 2023, 21:23   #2  |  Link
anton_foy
Registered User
 
Join Date: Dec 2005
Location: Sweden
Posts: 702
Looks great! Would be interesting to try for prefiltering dark footage for better low light details after mdegrain. I will test.
anton_foy is offline   Reply With Quote
Old 14th February 2023, 18:26   #3  |  Link
Selur
Registered User
 
Selur's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 7,259
Looks interesting, btw. to see what's in the dark is Retinex

(AutoAdjust is also a good method to just brighten dark areas.)
__________________
Hybrid here in the forum, homepage

Last edited by Selur; 14th February 2023 at 18:32.
Selur is offline   Reply With Quote
Old 14th February 2023, 18:46   #4  |  Link
Arx1meD
Registered User
 
Arx1meD's Avatar
 
Join Date: Feb 2021
Posts: 121
Retinex? Is there one for Avisynth?
Arx1meD is offline   Reply With Quote
Old 14th February 2023, 19:08   #5  |  Link
Selur
Registered User
 
Selur's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 7,259
yup: https://github.com/Asd-g/AviSynth-Retinex
__________________
Hybrid here in the forum, homepage
Selur is offline   Reply With Quote
Old 16th February 2023, 09:25   #6  |  Link
coolgit
Registered User
 
Join Date: Apr 2019
Posts: 217
When I brighten something, I would add a certain value, and subtract a certain value when less bright, i.e. darker.

However in your script when I brighten bright zones by adding a certain value, it becomes darker, subtract a certain value and it becomes brighter. This makes no sense. Looking at the code I changed one thing, in red.

Code:
curveL = light == 0 ? "x" : !DLZO ? "x "+stL+" "+k+" 1 + 1 x "+midY+" / - "+n+" ^ * pi x * "+maxY+" / cos "+k+" * - * 1 1 x "+midY+" / - "+j+" ^ "+m+" * - * +"
                               \ : "x "+stL+" 1 x "+midY+" / - "+l+" ^ pi x * "+maxY+" / cos "+l+" ^ - * "+f+" * +"
Now 20 makes whites brighter and -20 makes whites darker, the same as black colours.
coolgit is offline   Reply With Quote
Old 16th February 2023, 12:34   #7  |  Link
Arx1meD
Registered User
 
Arx1meD's Avatar
 
Join Date: Feb 2021
Posts: 121
coolgit,
dark > 0 - Dark areas will be brighter.
light > 0 - Light areas will be darker.
Do you need to have vice versa?
Arx1meD is offline   Reply With Quote
Old 16th February 2023, 13:44   #8  |  Link
coolgit
Registered User
 
Join Date: Apr 2019
Posts: 217
Quote:
Originally Posted by Arx1meD View Post
coolgit,
dark > 0 - Dark areas will be brighter.
light > 0 - Light areas will be darker.
Do you need to have vice versa?
That isn't logical.

dark + v = brighter
light + v = brighter

dark - v = less brighter (darker)
light - v = less brighter (darker)

logical with all existing software dealing with brightness.
coolgit is offline   Reply With Quote
Old 16th February 2023, 13:21   #9  |  Link
Julek
Registered User
 
Julek's Avatar
 
Join Date: Dec 2020
Posts: 84
CLAHE is also a very good filter for this: https://github.com/dnjulek/VapourSynth-EqualizeHist
(no AVS ver)
__________________
CPU: AMD 3700X | GPU: RTX 3070Ti | RAM: 32GB 3200MHz
GitHub
Julek is offline   Reply With Quote
Old 16th February 2023, 14:43   #10  |  Link
Arx1meD
Registered User
 
Arx1meD's Avatar
 
Join Date: Feb 2021
Posts: 121
coolgit, I think you are right.
Formulas do not need to be changed. They are right. Need to invert part of the luma curve 127.5-255.
Change only:
Code:
light  = Default(light, 20)*(-1.0)
Or change:
Code:
curveL = light == 0 ? "x" : !DLZO ? "x "+stL+" "+k+" 1 + 1 x "+midY+" / - "+n+" ^ * pi x * "+maxY+" / cos "+k+" * - * 1 1 x "+midY+" / - "+j+" ^ "+m+" * - * -"
                                \ : "x "+stL+" 1 x "+midY+" / - "+l+" ^ pi x * "+maxY+" / cos "+l+" ^ - * "+f+" * +"

out = "x "+midY+" > "+curveL+" "+curveD+" ?"

Last edited by Arx1meD; 16th February 2023 at 15:09.
Arx1meD is offline   Reply With Quote
Old 16th February 2023, 15:12   #11  |  Link
kedautinh12
Registered User
 
Join Date: Jan 2018
Posts: 2,153
You need edit script in first post, i will download it, thanks
kedautinh12 is offline   Reply With Quote
Old 16th February 2023, 17:00   #12  |  Link
Arx1meD
Registered User
 
Arx1meD's Avatar
 
Join Date: Feb 2021
Posts: 121
Change post #1.
Arx1meD is offline   Reply With Quote
Old 17th February 2023, 01:00   #13  |  Link
coolgit
Registered User
 
Join Date: Apr 2019
Posts: 217
That worked nicely, cheers.
coolgit is offline   Reply With Quote
Old 18th March 2023, 15:52   #14  |  Link
coolgit
Registered User
 
Join Date: Apr 2019
Posts: 217
Using
Code:
dark=0, light=-10
is excellent at reducing over sharpen video without the need to blur.
coolgit is offline   Reply With Quote
Old 17th November 2023, 12:29   #15  |  Link
butterw2
Registered User
 
Join Date: Jun 2020
Posts: 303
Quote:
Originally Posted by coolgit View Post
Using
Code:
dark=0, light=-10
is excellent at reducing over sharpen video without the need to blur.
thanks, I'll try it out in a shader.

The save luma line variant would requires dark=-light in order to have continuity on contrast at x=0.5.
it's possible to use lower values of n (ex n=2 or n=4 as in the plot linked below):
f(x, k) = x + k*(1-2*x)(1-(1-2*x)^n)
with n even, >0
and k in [-0.12, 0.12]
https://raw.githubusercontent.com/bu...deContrast.png
butterw2 is offline   Reply With Quote
Old 15th November 2023, 05:02   #16  |  Link
Selur
Registered User
 
Selur's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 7,259
btw. if anyone got the time and motivation, a Vapoursynth port of this would be nice.
Also shouldn't there be some adjustemnts depending whether the source if 'limited' or 'full' luma? (to use 16-235 instead of 0-255)
__________________
Hybrid here in the forum, homepage

Last edited by Selur; 15th November 2023 at 05:05.
Selur is offline   Reply With Quote
Old 17th November 2023, 17:55   #17  |  Link
Arx1meD
Registered User
 
Arx1meD's Avatar
 
Join Date: Feb 2021
Posts: 121
Quote:
Originally Posted by Selur View Post
btw. if anyone got the time and motivation, a Vapoursynth port of this would be nice.
Also shouldn't there be some adjustemnts depending whether the source if 'limited' or 'full' luma? (to use 16-235 instead of 0-255)
I never had to use a video source with a limited level of luma. If you have such a video, then try this:
ColorYUV(levels="TV->PC")
Brightness(...)
ColorYUV(levels="PC->TV")
Arx1meD is offline   Reply With Quote
Old 17th November 2023, 18:52   #18  |  Link
Selur
Registered User
 
Selur's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 7,259
sure, but wouldn't it be better to just change the limits depending on whether the input is pc or tv scale?
255.0 -> 235.0
127.5 -> 167.5
255 -> 235
__________________
Hybrid here in the forum, homepage
Selur is offline   Reply With Quote
Reply

Tags
bright, dark, restore, zones

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 18:54.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.