A really simple mask cleaning plugin for VapourSynth based on mt_hysteresis, also known as a Connected Component Labeling filter.
It discards all areas of less than length pixels with values bigger or equal to thresh. Original plugin by tp7. Ported to VapourSynth by Beatrice-Raws and modified by dtlnor.
- VapourSynth R54+ (API 4)
- System: 64bit
- CPU: AVX2+
core.tmcm.TMaskCleanerMod(clip clip, [int length, int thresh, int fade, bint binarize, int connectivity, bint reverse, int mode])
tmcm.TMaskCleanerMod(clip, length=5, thresh=235, fade=0)Original plugin works only with 8 bit input (and only Y plane). Beatrice-Raws port should support 10-16 bit as well, my mod adds 32bit float support on top of it (only plane 0 will be processed, 1 and 2 will be copied).
This mod adds options inspired by OpenCV's CCL based on the Beatrice-Raws port.
See the Syntax and Parameters section for details.
core.tmcm.GetCCLStats(clip clip, [int thresh, int connectivity])
tmcm.GetCCLStats(clip, thresh=235)Get the following FrameProps:
_CCLStatNumLabels
_CCLStatAreas
_CCLStatLefts
_CCLStatTops
_CCLStatWidths
_CCLStatHeights
_CCLStatCentroids_x
_CCLStatCentroids_y
These contain arrays (except _CCLStatNumLabels) if the input clip isn't blank. Note that the first label is always the background label, so you'll typically access frame properties like this:
f.props.get('_CCLStatAreas', None)[1:]-
clip
Input clip. Supports 8-16 bit integer / 32bit float Gray/YUV formats. For YUV input, only plane 0 is processed; planes 1 and 2 are copied. -
length =
5
Discards connected areas that less thanlengthpixels. (Usereverseto discard areas larger thanlength) -
thresh =
235
Binarize threshold. Discards pixels with values less thanthreshbefore applying thelengthfilter. Range: 0-255 for 8-bit input, 0-65535 for 16-bit input etc. Default235 << (bitsPerSample - 8)for 8-16bit or1.0for 32bit float input. -
fade =
0
Assume input clip is 8bit.
Controls gradual transition from 0 to 255:- Areas below
lengthwill be zeroed - Areas within [
length,length+fade] will be assigned values gradually from 0 to 255 - Areas above
length+fadewill be set to 255
This allows adaptive processing of area ranges.
For example, transforming the mask from linear (0→255) distribution denoted as/to:/\distribution (0→128→0)
using.Expr("x 128 > 255 x - x ?")
= ifx > 128set255-x, else setx||to grab some arbitrary range
using.Expr("x 118 > x 138 < & 255 0 ?")
= only ifx > 118 && x < 138set255, else set0- Non-linear
_/_mask by smoothly amplifying the middle
using.Expr("x 128 > 255 x - x ? 100 - 28 / sqrt 256 *")
= sqrt(/\- 100 / 28) * 255
- Areas below
-
binarize =
falsefalse: Retain original luma values before applyinglengthfilter and fade multiplication.
This means the output of retained areas will have their values copied from the source clip.true: Apply binarization withthreshbefore applyinglengthfilter and fade multiplication.
This means the output of retained areas will be changed to peak value (255 for 8-bit, 65535 for 16-bit, 1.0 for 32-bit float)
-
connectivity =
8
Determines how connected components are identified:8: Include all 8 surrounding pixels (including diagonals) in a 3×3 neighborhood4: Include only adjacent pixels (top/bottom/left/right) in a 3×3 neighborhood
-
reverse =
false
Set to true to filter out objects larger thanlength.
Assume input clip is 8bit.false:- Values below
lengthwill be zeroed - Values within [
length,length+fade] will be assigned values gradually from 0 to 255 - Values above
length+fadewill be set to 255
- Values below
true:- Values above
lengthwill be zeroed - Values within [
length-fade,length] will be assigned values gradually from 255 to 0 - Values below
length-fadewill be set to 255
- Values above
-
mode =
0
Determines how connected components are filtered.
Note that the meaning oflengthchanges based on the mode.0: Filter by area of component1: Filter by centroid x position2: Filter by centroid y position3: Filter by left position of bounding box4: Filter by top position of bounding box5: Filter by right position of bounding box6: Filter by bottom position of bounding box7: Filter by width of bounding box8: Filter by height of bounding box
This plugin is licensed under the MIT license. Binaries are GPL v2 because if I understand licensing stuff right (please tell me if I don't) they must be.