View Full Version : Adaptive sharpen - Image sharpening shader
bacondither
13th May 2015, 18:00
It is an image enhancement shader written in HLSL that can be used by MPC-HC or MPDN and other software that can use pixel shaders. Github page (https://github.com/bacondither/Adaptive-sharpen)
Version 2018-04-14
BSD 2-clause license
Download (https://github.com/bacondither/Adaptive-sharpen/archive/2018-04-14.zip)
Changelog:
2018-04-14 - Misc tweaks.
2017-12-30 - Cosmetic changes and minor improvements.
2017-11-01 - Small improvement for anti-ringing.
- Tweaks and minor changes.
2017-04-11 - Use compression instead of clipping for nmax_scale & nmin_scale.
- Tweaked saturation compensation.
2017-01-22 - Replaced the algorithm for saturation compensation.
2016-12-07 - Changed how saturation is compensated for when brightness is altered.
- Misc tweaks.
2016-09-24 - Updated edge-detection weights, slightly better for high-frequency content.
- Minor tweaks.
2016-07-10 - Replaced lerp used for overshoot compression with a weighted power mean, reduces some ringing.
- Corrected minor error in the custom smoothstep function.
- Slightly improved weighting for nmax & nmin.
- Tweaked some parameters.
2016-01-12 - Faster sorting algorithm.
2016-01-07 - Fixed compiler warning X3571 and handle a possible divide by zero.
2015-12-09 - Use a concave sharpening kernel for higher center edge values.
- For summation of pixels in the laplace kernel use a sRGB gamma approximation.
2015-11-17 - Improved low threshold for sharpening.
- Linear light used for summation in the laplace kernel.
- Fixed tanh overflow.
2015-09-23 - Limit low values for the "middle ring" of laplace weights.
- Misc changes.
2015-08-19 - 2-pass algorithm.
- Better overshoot control.
- Reduced saturation loss on pixels made brighter.
2015-06-25 - Unrolls the sorting loop when compiled with ps 4.0+ (much better performance for ps 4.0+).
- Added fix for MPDN (alpha channel is always set to one).
- Style fixes.
2015-05-15 - No real change from the last version, just changed some stupid variable names and comments.
2015-04-05 - First release.
Installation instructions
The shader is tuned and designed for post resize.
Requires gamma light as input and a pixel shader of version 3 or higher.
Settings are stored in the second pass file.
curve_height is the main control for the sharpening strength.
The correct shader order are:
Pass one
Pass two
If the shader passes are added in the wrong order or the alpha channel is truncated, it will return a green screen.
It does what?
The shader tries to sharpen somewhat blurry edges the most, it avoids to sharpen near-flat areas and very sharp edges.
The outer weights of the laplace matrix is variable to mitigate ringing on relative sharp edges and to provide more sharpening on wider and blurrier edges.
The final stage is a soft limiter that confines overshoots based on local near min/max values.
Light overshoots are limited more due to it's worse appearance compared to dark undershoots in most cases.
Old Versions
Depreciated old one pass version:
Download - OLD - 2015-05-25 (https://u.teknik.io/aCpSO7.zip)
toniash
14th May 2015, 07:52
It is an image enhancement shader written in HLSL that can be used by MPC-HC or MPDN and other software that can use pixel shaders. It's tuned and designed for post resize and wants gamma light as input.
I will probably rewrite this post soon and add more information!
Meanwhile i hope it works for you :)
BSD 2-clause license
Requires pixel shader version 3.0
Version 2015-04-05
Download (http://www.filedropper.com/adaptivesharpen-2015-04-05)
Very good! :thanks:
What is gamma light?
Zachs
14th May 2015, 13:06
Think of Gamma light is the default mode in a render chain. So long as you don't add the ConvertToLinearLight.hlsl file which converts gamma light to linear light, you'll be in gamma light mode.
KhR0N1K
18th May 2015, 05:44
awesome work man was waiting for something like this for a long time. it looks amazing. i was testing the FineSharp and LumaSharpen through madVR but i like the way this one looks a lot better.
thanks :D
foxyshadis
18th May 2015, 10:04
Testing in MPDN, the image immediately becomes much darker. Is this expected? For some reason, the same thing happens with FineSharp.
Intel 4600, win8.1.
Testing in MPDN, the image immediately becomes much darker. Is this expected? For some reason, the same thing happens with FineSharp.
Intel 4600, win8.1.
works fine for me. even if work used as a pre resize shader.
KhR0N1K
18th May 2015, 10:16
Testing in MPDN, the image immediately becomes much darker. Is this expected? For some reason, the same thing happens with FineSharp.
Intel 4600, win8.1.
i noticed this happening when saving screens to compare using MPC-BE. but seemed fine when viewing in the player itself or using PrtScrn instead of Save Image. could be a completely different issue though.
RainyDog
20th May 2015, 12:50
Thanks for this bacondither. Tried it last night and liked what I saw.
I'd been using Lumasharpen pre and post resize with reduced clamp as I could never get it to my liking when used in post only. But I found Adaptive Sharpen to be much more effective post only so it might be just what I've been looking for.
Seemed much too aggressive when used as a pre resize sharpener though, but I suppose you mention that it has been tuned for post resize.
bacondither
11th June 2015, 03:47
Very good! :thanks:
awesome work man was waiting for something like this for a long time. it looks amazing. i was testing the FineSharp and LumaSharpen through madVR but i like the way this one looks a lot better.
thanks :D
Thanks for this bacondither. Tried it last night and liked what I saw.
I'd been using Lumasharpen pre and post resize with reduced clamp as I could never get it to my liking when used in post only. But I found Adaptive Sharpen to be much more effective post only so it might be just what I've been looking for.
Seemed much too aggressive when used as a pre resize sharpener though, but I suppose you mention that it has been tuned for post resize.
Thanks :)
Testing in MPDN, the image immediately becomes much darker. Is this expected? For some reason, the same thing happens with FineSharp.
Intel 4600, win8.1.
For me it works fine in MPDN.
Olivier C.
16th June 2015, 02:56
Hi,
Is there an equivalent of Masktools for GPU ?
Something like mt_merge (merge two layers according to a mask) for example ?
The actual formula is y = ((256 - m) * x1 + m * x2 + 128) / 256 ) where, for a given pixel :
y is the result (merge)
m is the mask value (from 0 to 255)
x1 is the first layer to merge
x2 is the second layer to merge
I am trying to port some avisynth scripts to shaders (GPU) but i am not an expert of .hlsl
Thanks
Olivier
vivan
16th June 2015, 03:42
It should work just like that, except everything is done in floating point ([0..1] range).
So
(1.0 - m) * x1 + m * x2 + 0.5
or it's equal
lerp (x1, x2, m) + 0.5
madshi
16th June 2015, 18:44
Some caution, though: I think AviSynth scripts usually run in YCbCr, so Y has a range from 16-235. In contrast to that, usually custom pixel shaders are executed in PC levels. So AviSynth 16 should map to floating point 0.0, and AviSynth 235 should map to floating point 1.0. Won't matter for some AviSynth scripts, but it will probably affect some.
Olivier C.
16th June 2015, 19:00
It should work just like that, except everything is done in floating point ([0..1] range).
So
(1.0 - m) * x1 + m * x2 + 0.5
or it's equal
lerp (x1, x2, m) + 0.5
Thanks vivan
Olivier C.
16th June 2015, 19:01
Some caution, though: I think AviSynth scripts usually run in YCbCr, so Y has a range from 16-235. In contrast to that, usually custom pixel shaders are executed in PC levels. So AviSynth 16 should map to floating point 0.0, and AviSynth 235 should map to floating point 1.0. Won't matter for some AviSynth scripts, but it will probably affect some.
Good point Mathias, thanks !
madshi
28th June 2015, 22:46
FYI, I've implemented this in the latest madVR build now, so users can directly play with it, instead of having to setup custom pixel shaders. Thanks for making this available under BSD license!
KhR0N1K
7th July 2015, 02:28
this may sound like a dumb question but do i have to have the options "don't use linear light for dithering" and "don't use linear light for smooth motion frame blending" in the madVR options under the "trade quality for performance" tab?
or do these not interfere with the gamma light for the Adaptive Sharpen shader?
also the same question for the "scale in linear light" options for upscaling and downscaling in madVR?
madshi
7th July 2015, 07:46
These options are totally unrelated to Adaptive Sharpen. The trade quality options should better be unchecked, unless you need them to be checked for performance reasons. For upscaling linear light is not recommended. For downscaling it's a matter of taste.
KhR0N1K
7th July 2015, 18:43
These options are totally unrelated to Adaptive Sharpen. The trade quality options should better be unchecked, unless you need them to be checked for performance reasons. For upscaling linear light is not recommended. For downscaling it's a matter of taste.
ok will do madshi. thanks a lot for the response. i will do what you said and leave them unchecked. and keep the scaling ones off as well.
foozoor
11th July 2015, 02:44
Could you make an opengl (glsl) version ?
It would be great for mpv-player users.
bacondither
16th July 2015, 16:28
Could you make an opengl (glsl) version ?
It would be great for mpv-player users.
I'm working on improving it now, perhaps in the future i will write a glsl version of it.
KhR0N1K
17th July 2015, 05:15
thanks for the experimentals man. i check back here everyday to see if there is improvements :). it is great already however lol.
Edit: the new experimental actually does look better btw. lol. amazing.
aufkrawall
18th July 2015, 20:25
Thanks for your efforts, bacondither. AdaptiveSharpen really looks fantastic post-resize with NNEDI3.
burfadel
24th July 2015, 22:29
Love this sharpening filter, it works wonders :). I'm using the experimental version. For anyone deciding to try it out, check back occasionally to the first post, as there may be a new experimental version.
KhR0N1K
29th July 2015, 21:30
Love this sharpening filter, it works wonders :). I'm using the experimental version. For anyone deciding to try it out, check back occasionally to the first post, as there may be a new experimental version.
exactly i check back every day lol
burfadel
11th August 2015, 11:24
I wonder if after a bit more tweaking whether it is found to be suitable to be added to MPDN as a sharpening option?
aufkrawall
11th August 2015, 14:19
You can already use it as a render script image processor and with conditionals, you can exactly define when to use it or not.
burfadel
11th August 2015, 15:48
I know, just thought it would be easier for people to set up and be aware of :)
aufkrawall
11th August 2015, 16:29
Yeah, true.
But dunno if this has priority, as SweetFX with Lumasharpen is already bundled with render scripts, but has to be used with image processor though.
bacondither
21st August 2015, 16:40
New version released! :) The previously experimental two pass version is now the stable one.
The experimental version section is removed and replaced with a old versions section.
Now i'm off out in the nice weather!
burfadel
21st August 2015, 20:01
Nice! I notice there is a very slight difference in the formula of the second pass between the 28 July version and the latest. Just thought I'd mention that since people may assume that you meant the 28 July version is the same when you said 'The previously experimental two pass version is now the stable one.'
Thanks, it works wonderfully! :)
KhR0N1K
22nd August 2015, 07:29
New version released! :) The previously experimental two pass version is now the stable one.
The experimental version section is removed and replaced with a old versions section.
Now i'm off out in the nice weather!
awesome man looks and works great thanks for all your hard work.
lanzorg
22nd August 2015, 07:51
I'm working on improving it now, perhaps in the future i will write a glsl version of it.
That would be really wonderful! :)
foozoor
23rd August 2015, 06:58
That would be really wonderful! :)
I hope he will hear our vow and that he will realise it. ;)
burfadel
14th September 2015, 19:21
Wow, missed there is a new September test build!
Thanks :)!
burfadel
23rd September 2015, 04:58
Download - EXPERIMENTAL - 2015-09-19 (https://u.teknik.io/k0pyMu.zip)
Thanks, as always :)!
bacondither
23rd September 2015, 19:31
New version 2015-09-23 released.
Tried optimizing some, it went faster for AMDs old VLIW gpus but slower for newer GCN. So that did not make it to this version.
The bilateral blur from the experimental version did better sometimes and worse other times. But the difference was so minimal and it was slower. So it's not included now.
aufkrawall
23rd September 2015, 19:38
Thanks for letting us know, interesting to read.
madshi
23rd September 2015, 20:00
@bacondither, can you double check AdaptiveSharpen with this test image?
http://madshi.net/FineSharpTest3.png
Last time I checked, AdaptiveSharpen makes this image brighter. Which is what most sharpeners do, so it's not surprising. But ideally, that should not happen. Maybe you can work on that? E.g. by using linear light in some parts of your algorithm? Or maybe by running a second pass which "repairs" modified brightness?
Thanks!
KhR0N1K
24th September 2015, 15:50
great work and progress bacon looking great and loving the constant upgrades
aufkrawall
29th September 2015, 17:09
@bacondither, can you double check AdaptiveSharpen with this test image?
http://madshi.net/FineSharpTest3.png
Last time I checked, AdaptiveSharpen makes this image brighter.
Yes, that's still the case with the current version. Would be great if this could be improved.
lanzorg
20th October 2015, 15:27
New version 2015-09-23 released.
Tried optimizing some, it went faster for AMDs old VLIW gpus but slower for newer GCN. So that did not make it to this version.
The bilateral blur from the experimental version did better sometimes and worse other times. But the difference was so minimal and it was slower. So it's not included now.
Could you port this version to glsl?
ts1
26th October 2015, 18:28
glsl version here (https://gist.github.com/bjin/e22e96861669e897cf65)
This shader also requires floating point processing apparently.
vood007
26th October 2015, 19:26
Here is glsl version of this shader:
Pass 1
...
This shader also requires floating point processing apparently.
Thanks for this. May i request a glsl version of the old one pass version too? Its linked in the first posting.
ts1
26th October 2015, 22:01
glsl version of Adaptive Sharpen (https://gist.github.com/igv/8a77e4eb8276753b54bb94c1c50c317e)
vood007
27th October 2015, 05:00
vood007
// Copyright (c) 2015, bacondither
// All rights reserved.
...
Thanks again. Works fine under Linux with MPV.
bacondither
29th October 2015, 15:33
@bacondither, can you double check AdaptiveSharpen with this test image?
http://madshi.net/FineSharpTest3.png
Last time I checked, AdaptiveSharpen makes this image brighter. Which is what most sharpeners do, so it's not surprising. But ideally, that should not happen. Maybe you can work on that? E.g. by using linear light in some parts of your algorithm? Or maybe by running a second pass which "repairs" modified brightness?
Thanks!
I agree that in some situations local contrast can be increased too much, but sometimes it should get brighter.
For example, if you blur the example image with a gaussian filter or a median filter it gets darker, "unblurring" it then should then make it look brighter.
But the original example image is pretty sharp already, increasing the local contrast/acutance a lot does not look that good in that example.
I have not found a satisfactory solution yet that does not compromise other situations. Sometime in the future i'm going to try testing a third "repair" pass.
I'm pretty sure i'll get some other ideas while in the shower or something... :)
glsl version here (https://gist.github.com/bjin/e22e96861669e897cf65)
This shader also requires floating point processing apparently.
Just had a quick look, i can't understand why is the inputs into tanh(x) is clamped to -+20. That could lead to some screwy results.
burfadel
29th October 2015, 15:38
oooh new test version, thanks :)
ts1
29th October 2015, 16:08
That is not my port but one of mpv's developers. We made ports in the same day :)
bacondither
29th October 2015, 16:23
That is not my port but one of mpv's developers. We made ports in the same day :)
Ah, okay.
I don't have MPV ready for testing now, but did some tests on it a while ago on ubuntu. Could not get some script for switching display frequencies to work as i wanted.
ts1
29th October 2015, 16:39
Windows builds (https://mpv.srsfckn.biz/) available also. Just create "portable_config" folder next to mpv.exe with your shaders and mpv.conf with content something like:
vo=opengl-hq:fbo-format=rgba16f:post-shaders="~~/adaptive-sharpen-pass1.glsl,~~/adaptive-sharpen-pass2.glsl"
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.