Skip to content

Instantly share code, notes, and snippets.

@Ichunjo
Created January 23, 2022 02:31
Show Gist options
  • Select an option

  • Save Ichunjo/723b13111dbc98db6fabc08149b12feb to your computer and use it in GitHub Desktop.

Select an option

Save Ichunjo/723b13111dbc98db6fabc08149b12feb to your computer and use it in GitHub Desktop.
RGB -> HSV VideoNode and catch red parts
import vapoursynth as vs
from vsmask.edge import FDOGTCanny
from vsmask.util import XxpandMode, expand, inpand, max_planes
from vsutil import join, split
core = vs.core
# clip = core.imwri.Read('unknown.png') * 10
clip = core.imwri.Read('unknown (1).png') * 10
clip = clip.resize.Point(format=vs.RGBS).std.RemoveFrameProps('_Matrix')
clip.set_output(0)
def to_hsv(clip: vs.VideoNode) -> vs.VideoNode:
planes = split(clip.std.RemoveFrameProps('_Matrix'))
cmax = core.std.Expr(planes, 'x y z max max')
cmin = core.std.Expr(planes, 'x y z min min')
Δ = core.std.Expr([cmax, cmin], 'x y -')
rc = core.std.Expr([cmax, planes[0], Δ], 'x y - z /')
gc = core.std.Expr([cmax, planes[1], Δ], 'x y - z /')
bc = core.std.Expr([cmax, planes[2], Δ], 'x y - z /')
# x y z | a b c d e
# r g b | rc gc bc cmax Δ
h = core.std.Expr(planes + [rc, gc, bc, cmax, Δ], 'e 0 = 0 d x = c z - d y = 2 a + c - 4 z + a - ? ? ? 6 /')
# h = core.std.Expr(planes + [rc, gc, bc, cmax], 'cmax r = bc gc - cmax g = 2 rc + bc - 4 gc + rc - ? ? 6 /')
h = core.std.Expr(h, 'x 0 < x 1 + x ?')
s = core.std.Expr([Δ, cmax], 'y 0 = 0 x y / ?')
v = cmax
return join([h, s, v])
clip_hsv = to_hsv(clip)
clip_hsv.set_output(1)
h_thr = 20 / 255
s_hr = 80 / 255
v_thrl = 255 / 255
v_thrh = 15 / 255
cclip = core.std.Expr(split(clip_hsv), f'x {h_thr} < x 1 {h_thr} - > or y {s_hr} > and z {v_thrl} < z {v_thrh} > and and 1 0 ?')
cclip = expand(cclip, 20, 20, XxpandMode.ELLIPSE)
cclip = inpand(cclip, 15, 15, XxpandMode.ELLIPSE)
edgemask = max_planes(
*split(FDOGTCanny().get_mask(clip).std.RemoveFrameProps('_Matrix'))
).std.Binarize(0.4).std.Maximum().std.Minimum()
edgemask.set_output(2)
final = core.std.Expr([cclip, edgemask], 'x 1 >= y 1 >= and x 0 ?')
final.set_output(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment