View difference between Paste ID: AsbQaA6J and gkBrBX7Z
SHOW: | | - or go back to the newest paste.
1-
# http://avisynth.nl/index.php/FineDehalo
1+
# FineDehalo
2-
# 1.1 mod3
2+
# 1.1 mod4
3
#
4
# This program is free software. It comes without any warranty, to
5
# the extent permitted by applicable law. You can redistribute it
6
# and/or modify it under the terms of the Do What The Fuck You Want
7
# To Public License, Version 2, as published by Sam Hocevar. See
8
# http://sam.zoy.org/wtfpl/COPYING for more details.
9
10-
Function FineDehalo (clip src, float "rx", float "ry", int "thmi", int "thma", int "thlimi", int "thlima", float "darkstr", float "brightstr", int "showmask", float "contra", bool "excl", float "edgeproc", val "exdehalo")
10+
Function FineDehalo (clip src, float "rx", float "ry", int "thmi", int "thma", int "thlimi", int "thlima", float "darkstr", float "brightstr", int "showmask", float "contra", bool "excl", float "edgeproc", val "exdehalo", clip "exedgesm")
11
{
12
	rx        = Default (rx,          2)
13
	ry        = Default (ry,         rx)
14
	thmi      = Default (thmi,       80)
15
	thma      = Default (thma,      128)
16
	thlimi    = Default (thlimi,     50)
17
	thlima    = Default (thlima,    100)
18
	darkstr   = Default (darkstr,   1.0)
19
	brightstr = Default (brightstr, 1.0)
20
	showmask  = Default (showmask,    0)
21
	contra    = Default (contra,    0.0)
22
	excl      = Default (excl,     true)
23
	edgeproc  = Default (edgeproc,  0.0)
24
25
	rx_i = Round (rx)
26
	ry_i = Round (ry)
27
28
	src
29
30
31
	### Dehaloing ###
32
33
	dehaloed = defined(exdehalo) ? isclip(exdehalo) ? exdehalo : eval("last." + exdehalo) : DeHalo_alpha (rx=rx, ry=ry, darkstr=darkstr, brightstr=brightstr)
34
35
	# Contrasharpening
36
	dehaloed =   (contra > 0)
37
\	           ? dehaloed.FineDehalo_contrasharp (src, contra)
38
\	           : dehaloed
39
40
	edgesm = defined(exedgesm) ? exedgesm : FineDehaloedges(rx=rx, ry=ry, thmi=thmi, thma=thma, thlimi=thlimi, thlima=thlima, showmask=showmask, excl=excl, edgeproc=edgeproc)
41
42
	### Masking ###
43
44
		  (showmask != 0) ? edgesm
45
	\	:                   mt_merge (last, dehaloed, edgesm, y=3, u=2, v=2)
46
}
47
48
# level == 1.0 : normal contrasharp
49
Function FineDehalo_contrasharp (clip dehaloed, clip src, float level)
50
{
51
	bb  = dehaloed.RemoveGrain (11, -1)
52
	bb2 = bb.Repair (bb.Repair (bb.Medianblur (2, -256, -256), 1), 1)
53
	xd  = mt_makediff (bb, bb2)
54
	xd  = xd.mt_lut ("x 128 - 2.49 * "+String(level)+" * 128 +")
55
	xdd = mt_lutxy (
56
\		xd,
57
\		mt_makediff (src, dehaloed),
58
\		"x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?"
59
\	)
60
61
	dehaloed.mt_adddiff (xdd, y=3, u=2, v=2)
62
}
63
64
65
66
67
# Try to remove 2nd order halos.
68
Function FineDehalo2 (clip src, string "hconv", string "vconv", int "showmask")
69
{
70
	hconv    = Default (hconv, "-1 -2 0 0 40 0 0 -2 -1")
71
	vconv    = Default (vconv, "-2 -1 0 0 40 0 0 -1 -2")
72
	showmask = Default (showmask, 0)
73
74
	src
75
	fix_h = mt_convolution (horizontal="1", vertical=vconv, y=3, u=2, v=2)
76
	fix_v = mt_convolution (horizontal=hconv, vertical="1", y=3, u=2, v=2)
77
	edges_h = mt_edge (mode="1 2 1 0 0 0 -1 -2 -1", thY1=0, thY2=255)
78
	edges_v = mt_edge (mode="1 0 -1 2 0 -2 1 0 -1", thY1=0, thY2=255)
79
	mask_h = edges_h	#.mt_lut (expr="x 2 *")
80
	mask_v = edges_v	#.mt_lut (expr="x 2 *")
81
	temp_h = mt_lutxy (mask_h, mask_v, expr="x 3 * y -")
82
	temp_v = mt_lutxy (mask_v, mask_h, expr="x 3 * y -")
83
	mask_h = temp_h
84
	mask_v = temp_v
85
86
	mask_h = mask_h.FineDehalo2_grow_mask ("vertical")
87
	mask_v = mask_v.FineDehalo2_grow_mask ("horizontal")
88
89
	src
90
	mt_merge (last, fix_h, mask_h, y=3, u=2, v=2)
91
	mt_merge (last, fix_v, mask_v, y=3, u=2, v=2)
92
93
	  (showmask == 1) ? mt_logic (mask_h, mask_v, mode="max").GreyScale ()
94
\	:                   last
95
}
96
97
Function FineDehalo2_grow_mask (clip mask, string mode)
98
{
99
	Assert ((mode == "horizontal" || mode == "vertical"), "Wrong mode")
100
101
	mask
102
	mt_expand (mode=mode).mt_inpand (mode=mode)
103
	mask_1 = mt_expand (mode=mode)
104-
	outside = outside.RemoveGrain (20, -1).mt_lut ("x 2 *")
104+
105
	mt_lutxy (mask_2, mask_1, expr="x y -")
106
	RemoveGrain (12, -1).mt_lut (expr="x 1.8 *")
107
}
108-
	mt_merge (last, dehaloed, outside, y=3, u=2, v=2)
108+
109
Function FineDehaloanalog (clip src, string "exdehalo")
110
{
111
exdehalo=Default (exdehalo, "VHSHaloremover(2,2,200,100,0.5)")
112
src
113
edgm = EMask_dhh(ColorYUV(autogain=true), 3, 255,true)
114
hfil = DeHalo_alpha_mt(darkstr=0.2,brightstr=0.8).FineDehalo(exdehalo=DeHaloHmod(2,exdehalo=exdehalo,extmask=edgm))
115
RM2  = DR_Radius_dhh(edgm.mt_inflate(),2,0).mt_inflate()
116
DeRinging = mt_Merge(hfil, src, edgm.mt_inflate(155,155))
117
mt_Merge(src, DeRinging, RM2, u=2, v=2)
118
}
119
120
Function FineDehaloedges (clip src, float "rx", float "ry", int "thmi", int "thma", int "thlimi", int "thlima", int "showmask", bool "excl", float "edgeproc")
121
{
122
	rx        = Default (rx,          2)
123
	ry        = Default (ry,         rx)
124
	thmi      = Default (thmi,       80)
125
	thma      = Default (thma,      128)
126
	thlimi    = Default (thlimi,     50)
127
	thlima    = Default (thlima,    100)
128
	showmask  = Default (showmask,    0)
129
	excl      = Default (excl,     true)
130
	edgeproc  = Default (edgeproc,  0.0)
131
132
	rx_i = Round (rx)
133
	ry_i = Round (ry)
134
135
	src
136
137
	### Main edges ###
138
139
	# Basic edge detection, thresholding will be applied later.
140
	edges = mt_edge (mode="prewitt", thY1=0, thY2=255)
141
142
	# Keeps only the sharpest edges (line edges)
143
	strong = edges.mt_lut (expr="x "+String(thmi)+" - "+String(thma-thmi)+" / 255 *")
144
145
	# Extends them to include the potential halos
146
	large = strong.mt_expand_multi (sw=rx_i, sh=ry_i)
147
148
149
	### Exclusion zones ###
150
151
	# When two edges are close from each other (both edges of a single
152
	# line or multiple parallel color bands), the halo removal
153
	# oversmoothes them or makes seriously bleed the bands, producing
154
	# annoying artifacts. Therefore we have to produce a mask to exclude
155
	# these zones from the halo removal.
156
157
	# Includes more edges than previously, but ignores simple details
158
	light = edges.mt_lut (expr="x "+String(thlimi)+" - "+String(thlima-thlimi)+" / 255 *")
159
160
	# To build the exclusion zone, we make grow the edge mask, then shrink
161
	# it to its original shape. During the growing stage, close adjacent
162
	# edge masks will join and merge, forming a solid area, which will
163
	# remain solid even after the shrinking stage.
164
165
	# Mask growing
166
	shrink = light.mt_expand_multi (sw=rx_i, sh=ry_i, mode="ellipse")
167
168
	# At this point, because the mask was made of a shades of grey, we may
169
	# end up with large areas of dark grey after shrinking. To avoid this,
170
	# we amplify and saturate the mask here (actually we could even
171
	# binarize it).
172
	shrink = shrink.mt_lut ("x 4 *")
173
174
	# Mask shrinking
175
	shrink = shrink.mt_inpand_multi (sw=rx_i, sh=ry_i, mode="ellipse")
176
177
	# This mask is almost binary, which will produce distinct
178-
Function FineDehaloanalog (clip src)
178+
179
	shrink = shrink.RemoveGrain (20, -1)
180
	shrink = shrink.RemoveGrain (20, -1)
181
182-
hfil = DeHalo_alpha_mt(darkstr=0.2,brightstr=0.8).FineDehalo(exdehalo=DeHaloHmod(2,exdehalo="VHSHaloremover(4,3,200,100,0.5)",extmask=edgm))
182+
183
	### Final mask building ###
184
185
	# Previous mask may be a bit weak on the pure edge side, so we ensure
186
	# that the main edges are really excluded. We do not want them to be
187
	# smoothed by the halo removal.
188
	shr_med = (excl) ? mt_logic (strong, shrink, mode="max") : strong
189
190
	# Substracts masks and amplifies the difference to be sure we get 255
191
	# on the areas to be processed.
192
	outside = mt_lutxy (large, shr_med, "x y - 2 *")
193
194
	# If edge processing is required, adds the edgemask
195
	ep_str  = "x y "+String(edgeproc * 0.66)+" * +"
196
	outside = (edgeproc > 0) ? mt_lutxy (outside, strong, ep_str) : outside
197
198
	# Smooth again and amplify to grow the mask a bit, otherwise the halo
199
	# parts sticking to the edges could be missed.
200
	outside.RemoveGrain (20, -1).mt_lut ("x 2 *")
201
202
	  (showmask == 1) ? outside.GreyScale ()
203
\	: (showmask == 2) ? shrink.GreyScale ()
204
\	: (showmask == 3) ? edges.GreyScale ()
205
\	: (showmask == 4) ? strong.GreyScale ()
206
\	:                   last
207
}