View Full Version : Applying a filter only to certain part of a frame
Marin85
13th October 2012, 22:33
Hi,
is there any way to apply a filter only to certain part of a frame, e.g. applying sharpening filter only to the lower left quadrant of a frame by, say, specifying "coordinates" for the quadrant corners?
manolito
13th October 2012, 22:57
Oh yes, this is possible...:)
Here is a generic script (most of the logic thanks to Gavino). Save it as "ActiveArea.avs", fill in the desired coordinates and replace "My_Filter" with the one you need.
# Define the coordinates of the desired active area
# X1 is left, Y1 is top, X2 is right, Y2 is bottom
X1 =
Y1 =
X2 =
Y2 =
# Make the values MOD4
X1 = X1 - (X1 % 4)
Y1 = Y1 - (y1 % 4)
X2 = (X2 + 3) / 4 * 4
Y2 = (Y2 + 3) / 4 * 4
Active = crop(X1,Y1,X2-X1,Y2-Y1)
Above = Y1 > 0 ? crop(0,0,width(),Y1) : NOP
Below = Y2 < height() ? crop(0,Y2,width(),height()-Y2) : NOP
Left = X1 > 0 ? crop(0,Y1,X1,Y2-Y1) : NOP
Right = X2 < width() ? crop(X2,Y1,width()-X2,Y2-Y1) : NOP
Last = Active
My_Filter(...parameters...)
# My_Filter_2(...parameters...)
# My_Filter_3(...parameters...)
IsClip(Left) ? StackHorizontal(Left, Last) : NOP
IsClip(Right) ? StackHorizontal(Last, Right) : NOP
IsClip(Above) ? StackVertical(Above, Last) : NOP
IsClip(Below) ? StackVertical(Last, Below) : NOP
return Last
Good luck!
Cheers
manolito
mastrboy
13th October 2012, 22:59
Hi,
is there any way to apply a filter only to certain part of a frame, e.g. applying sharpening filter only to the lower left quadrant of a frame by, say, specifying "coordinates" for the quadrant corners?
If i remember correctly you can use a image as a mask with overlay, though i can't produce the necessary code for it :scared:
Marin85
1st November 2012, 22:40
@manolito: Hallelujah, brother! I was not even aware that one can stack clips (of suitable dimensions) in Avisynth. How could I think that, I must have been blind! Anyway, it makes now perfect sense, and that script does the necessary steps of cropping and stacking to achieve the end result. Thanks for posting it, it sure saves a lot of time!
Mounir
18th March 2014, 16:25
I need to work on the top part of the frame (a rectangle zone if you will) but can't seem to make this script work. With virtualdub i have calculated that Y2 need to be at 448 the rest at zero.
Then the filter i want to use:
mergechroma(last)
fft3dfilter(bt=4,sigma=19,plane=3,degrid=1)
mergeluma(last)
The error i get is image heights don't match
Someone give me a hand, thanks
feisty2
18th March 2014, 16:27
use mt_lutspa
Mounir
18th March 2014, 16:41
Well can't you tell me more ?
Where should i put 448 exactly in the script of manolito
and where should i put my filter, i mean how to script all that
TurboPascal7
19th March 2014, 03:19
mask = mt_lutspa("absolute", expr="y 448 <= 255 0 ?")
mt_merge(last, blackness(last), mask)
Replace blackness with your filtering. Add luma=true to mt_merge if you want to process chroma too.
It's not performance optimal because you still process the whole image and then discard some of the pixels, but it works.
raffriff42
19th March 2014, 08:23
Here's an example of using an image mask - it is much more versatile than specifying rectangles. You create the mask image in any paint editor.# http://avisynth.nl/index.php/Variableblur
LoadPlugin("..\variableblur07\variableblur.dll")
V=AviSource("D:\VideoProjects\raw\ntia_purple4b_ut_hufy720.avi")
\ .BilinearResize(640, 360) /* resize for demonstration purposes */
\ .ConvertToYV12
VB=V.GaussianBlur(varY=30, varC=60) /* blurred copy of original */
MI=ImageSource("D:\VideoProjects\work\Masks\LL-soft.png")
\ .ConvertToYV12(matrix="PC.601") /* PC matrix for full range image */
\ .BilinearResize(V.Width, V.Height) /* mask size must match video */
\ .Invert /* reverse the mask as needed */
\ .Levels(64, 1, 200, 0, 255, coring=false) /* optional fine tuning */
return StackVertical(
\ V, VB, MI,
\ Overlay(V, VB, mask=MI))
https://www.dropbox.com/s/b8fv2jey8pxvptd/avisynth-masked-blur-demo.jpg?raw=1
manolito
19th March 2014, 21:24
@Mounir
In case you do not want to use Mask Tools, I edited my script to support multiple filters.
Cheers
manolito
Mounir
19th March 2014, 22:00
invalid arguments to function is what i get
Can't you answer my previous question (you're too busy i guess) how should i write it considering i want Y2 to be 448 (checked with vdub)
No examples in your script..
manolito
19th March 2014, 23:40
With virtualdub i have calculated that Y2 need to be at 448 the rest at zero.
If you need to process a rectangle at the top of your clip, Y2 = 448 makes sense, but "the rest at zero" makes no sense at all. If X1 = 0 then X2 must be greater than 0, in your case it should probably be equal to the width of your clip.
For a frame size of 720 x 576 where you want to process only the upper part of the frame with a size of 720 x 448, your setting for the coordinates should look like this:
X1 = 0
Y1 = 0
X2 = 720
Y2 = 448
Cheers
manolito
Mounir
20th March 2014, 12:57
Invalid arguments to function crop
..fantastic
coded as follow:
X1 = 0
Y1 = 0
X2 = 720
Y2 = 448
# Make the values MOD4
X1 = X1 - (X1 % 4)
Y1 = Y1 - (y1 % 4)
X2 = (X2 + 3) / 4 * 4
Y2 = (Y2 + 3) / 4 * 4
Active = crop(X1,Y1,X2-X1,Y2-Y1)
Above = Y1 > 0 ? crop(0,0,width(),Y1) : NOP
Below = Y2 < height() ? crop(0,Y2,width(),height()-Y2) : NOP
Left = X1 > 0 ? crop(0,Y1,X1,Y2-Y1) : NOP
Right = X2 < width() ? crop(X2,Y1,width()-X2,Y2-Y1) : NOP
Last = Active
My_Filter(fft3dfilter(bt=4,sigma=19,plane=3,degrid=1))
IsClip(Left) ? StackHorizontal(Left, Last) : NOP
IsClip(Right) ? StackHorizontal(Last, Right) : NOP
IsClip(Above) ? StackVertical(Above, Last) : NOP
IsClip(Below) ? StackVertical(Last, Below) : NOP
return Last
manolito
20th March 2014, 16:34
Invalid arguments to function crop
..fantastic
coded as follow:
replace this one
My_Filter(fft3dfilter(bt=4,sigma=19,plane=3,degrid=1))
with this
fft3dfilter(bt=4,sigma=19,plane=3,degrid=1)
Cheers
manolito
//EDIT//
Just tested the script here to be absolutely sure...
Works beautifully here. Are the dimensions of your source clip smaller than the rectangle you are defining?
Mounir
21st March 2014, 11:54
the video is 720x480 yuy2 huff interlaced
I'm still gettin the crop error
manolito
21st March 2014, 13:58
Alright, I did not have such a source file, so I made one... :D
You can download my test files here:
http://www28.zippyshare.com/v/75055915/file.html
The file was created from a TV capture with Huffyuv 2.1.1 CCESP Patch 0.22. On my system GraphStudio tells me that the default decoder is ffdshow.
With this file the Active_Area script works without a hitch. If my source file works on your computer then it is clear that something is wrong with your source. If you get the same crop error with my file then something else is flaky with your system.
Maybe your AviSynth version, the decoder used by your system, whatever... You could try to add a
ConvertToYV12(interlaced=true)
to your script right after loading the source. Or use the DirectShow filter tweaker to force ffdshow as your AVI decoder.
Anyways, I believe I cannot help you any further with this problem...
Cheers
manolito
Mounir
21st March 2014, 14:48
Invalid args to function crop, line 20
line 20 being:
Active = crop(X1,Y1,X2-X1,Y2-Y1)
Gavino
21st March 2014, 15:23
@Mounir, you do realize that you have to add a source filter to the script, right?
manolito's code assumes that 'last' has been set in an earlier part of the script.
Mounir
21st March 2014, 16:32
I didn't realise, stupid me
manolito
21st March 2014, 17:04
Thanks Gavino,
still weird that the result is a crop error. I would have thought that "script does not return a clip" would be the result if no source was specified...
Cheers
manolito
Gavino
21st March 2014, 17:13
Avisynth stops on the first error, so never gets to the end of the script.
What is happening is that Crop() expects a clip as its first argument, and none is supplied. So it tries again implicitly using 'last' as the argument, but since 'last' has not been set, this also fails.
General tip: If you get the error "Invalid arguments to function xxx", and there is no obvious 'wrong' argument, check to see if implicit 'last' is being used, and if so, that 'last' has been set correctly.
vBulletin® v3.8.11, Copyright ©2000-2025, vBulletin Solutions Inc.