Log in

View Full Version : mask formation for motion compenasated filtering


yup
20th June 2007, 10:11
Hi folk!
During last time I try write script for remove impulse noise from captured video. Script below:
LoadPlugin("mt_masktools.dll") # v2.0a30
#SetMemoryMax(256)
AVISource("seldv2.avi")
Crop(16,16,-16,-16)
AssumeBFF()
ConvertToYV12(interlaced=true)
fields=SeparateFields()
backward_vec = fields.MVAnalyse(isb = true, delta = 2, pel = 2, overlap=4, sharp=2, idx = 1,dct=1, chroma=false)
forward_vec = fields.MVAnalyse(isb = false, delta = 2, pel = 2, overlap=4, sharp=2, idx = 1,dct=1, chroma=false)
filtered=fields.MVDegrain1(backward_vec,forward_vec,thSAD=400,idx=1)
backward_vecf = filtered.MVAnalyse(blksize=8, isb = true, delta = 2, pel = 2, overlap=4, sharp=2, idx = 2,dct=1, chroma=false)
forward_vecf = filtered.MVAnalyse(blksize=8, isb = false, delta = 2, pel = 2, overlap=4, sharp=2, idx = 2,dct=1, chroma=false)
fc = fields.MVFlow(forward_vecf, idx=3, thSCD1=10000)
bc = fields.MVFlow(backward_vecf, idx=3, thSCD1=10000)
mask_backward=filtered.MVMask(backward_vecf, kind=2, ml=50)
mask_forward=filtered.MVMask(forward_vecf, kind=2, ml=50)
mask_backward = mt_binarize(mask_backward,threshold=254)
mask_forward = mt_binarize(mask_forward,threshold=254)
mask_totor= mt_logic(mask_backward, mask_forward, mode="or")
mask_totand= mt_logic(mask_backward, mask_forward, mode="and")
masksdi=SDIPixelMask(bc, fields, fc,25, 0.8) # define your thresholds here
#masksdi=mt_expand(masksdi)
interleave(bc,filtered,fc)
ml3dex(Y=3,U=0, V=0)
fml3dex=SelectEvery(3,1)
fsdi=mt_merge(fields,fml3dex,masksdi)
mt_merge(fsdi,fields,mask_totand)
StackVertical(fsdi, fields)
#Weave()
#AddBorders(16,16,16,16)

# Thanks to gzarkadas for this function set
# this is for Abs(p - f) or Abs(p - b)
Function absdiff(clip c1, clip c2) { return mt_lutxy(c1, c2, "x y - abs") }

# this build the expression for the SDI function
Function SDI_RPN(string expr_d1, string expr_d2, string expr_t1, string expr_t2) {
_d1 = expr_d1 + " "
_d2 = expr_d2 + " "
_t1 = expr_t1 + " "
_t2 = expr_t2 + " "
return _d1 + _t1 + "> " + _d2 + _t1 + "> | 1.0 " \
+ _d1 + _d2 + "- " + _d1 + _d2 + "+ / abs - 0.0 ? " \
+ _t2 + "> 1.0 " + _d1 + _t1 + "> " + _d2 + _t1 + "> | 1.0 " \
+ _d1 + _d2 + "- " + _d1 + _d2 + "+ / abs - 0.0 ? "
}

# this creates the mask
Function SDIPixelMask(clip prev, clip curr, clip next, float t1, float t2) {

c_d1 = absdiff(curr, next)
c_d2 = absdiff(curr, prev)

# since SDI returns 0..1 we multiply with 255

mask = mt_lutxy(c_d1, c_d2, SDI_RPN("x", "y", String(t1), String(t2)) + " 255 *")
return mask
}
http://imgplace.com/directory/dir4530/1182329489_5174.png (http://www.imgplace.com)
Now script work better, but problem with some block where estimate fast motion (see top picture women without part of forefinger). I use 2 mask, one SDI (Spike Detection Index) for find impulse noise and mask_tot for occlusion in both directions.
May be need use one more additional mask for this region, but why?
Please advice.
With kind regards yup.

Fizick
20th June 2007, 14:01
probably it will be better to continue your old thread innstead of creation new thread.

Didée
21st June 2007, 00:07
A somewhat complicated script that doesn't do quite the right thing, it seems ... Better start from scratch.

Quick tip: replace MVFlow compensation with MVCompensate. (Because MVFlow may produce thin structures that might confuse SDIPixelMask.)

BTW: You are NOT using the mask_totand thingy:
fsdi=mt_merge(fields,fml3dex,masksdi)
mt_merge(fsdi,fields,mask_totand)
StackVertical(fsdi, fields) ... because the result of the mt_merge-line with mask_totand is not called at all. You're outputting the clip fsdi, without the merge.

yup
21st June 2007, 07:20
Fizick
I introduced using masks. Old thread became long.
Didée
Thanks for advice, I try MVCompensate instead MVFlow with same result. I think main problem pulse noise which introduce error for motion estimation and because approach could be multi pass.
This capture very old VHS tape 1995 year, very important for me and my friends and I try fnid way for remove noise.
With kind regards yup.

Didée
21st June 2007, 11:54
A pity there is no sample to play with.:)

My impression is this:

You want to remove spikes. For that, you're creating a mask to detect those spikes. (this thread (http://forum.doom9.org/showthread.php?t=123996)...)
But: looking at the result you get now, it seems that your "spike mask" is not good. (If it was good, it would not allow to remove things like that finger ... but it does.)
True is that those spikes might confuse the motion estimation. Not easy to judge by how much, but still.

Looking at the problem as a whole, I'm not convinced by your approach. Let's try something more simple:

LoadPlugin("mt_masktools.dll")
LoadPlugin("RemoveGrain.dll")
LoadPlugin("MVTools.dll")
SetMemoryMax(256)
AVISource("seldv2.avi")
Crop(16,16,-16,-16)
AssumeBFF()
ConvertToYV12(interlaced=true)
fields=SeparateFields()
LOP = fields.removegrain(4).merge(fields,0.5).removegrain(4) # sharp lowpass
# LOP = fields.removegrain(4).removegrain(11) # alternative #1: soft lowpass
# LOP = fields.mt_luts(fields,mode="median",pixels="0 -1 0 0 0 1",U=3,V=3)
# # alternative #2: strictly vertical median
HIP = mt_makediff(LOP,fields,U=3,V=3)
bw_vec = LOP.MVAnalyse(isb = true, delta = 2, pel = 2, overlap=4, sharp=2, idx = 1,dct=1, chroma=false)
fw_vec = LOP.MVAnalyse(isb = false, delta = 2, pel = 2, overlap=4, sharp=2, idx = 1,dct=1, chroma=false)
interleave( HIP.MVCompensate(fw_vec,thSCD1=800, idx=2),
\ HIP,
\ HIP.MVCompensate(bw_vec,thSCD1=800, idx=2) )
clense(reduceflicker=false).selectevery(3,1)
HIP2 = mt_lutxy(HIP,last,"x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?",U=3,V=3)
LOP.mt_makediff(HIP2,U=3,V=3)
weave()

Without explanation for the moment ... just try and see if it works out. (Can't tell for sure without a sample.)
Note there are three different versions for creating the "LOP" lowpass. Try them all, one at a time, and compare.

yup
21st June 2007, 13:38
Didée!
:thanks:
I try Your script, get little blured image but black lines totaly not removed. I upload 50 frames. See link
http://www.sendspace.com/file/48e4zk
My old script without SDI mask totaly remove after 2-3 pass depend from file place and blured also.
With kind regards yup.