View Single Post
Old 4th January 2015, 04:32   #10  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
Post #10 of many

Dynamically changing borders detector (PART 1)
Code:
#######################################################
### DYNAMICALLY CHANGING BORDERS CROPPING FUNCTIONS ***    
#######################################################

Function DBSC_DynaCrop(clip c,String ScanDB,String "SpliceDB",int "W",int "H",String "Resizer",
    \ Int "CropLimit",Int "CropLimit_Left",Int "CropLimit_Top",Int "CropLimit_Right",Int "CropLimit_Bot",
    \ int "RLBT",int "LeftAdd",int "TopAdd",int "RightAdd",int "BotAdd",
    \ int "DissolveSkip",
    \ String "LogFn",String "LogTMPT",
    \ Float "CropThresh",Float "ATM",
    \ bool "Show",bool "Debug") {
/*

 DBSC_DynaCrop(). With Input SCanDB, Creates output SpliceDB and log, to crop dynamically changing borders from PROGRESSIVE clip.
  Requires RoboCrop v0.21+ for RoboCrop output LogFile, to get crop coords.
  As well as output SpliceDB, also outputs a user configurable log file (always outputs the log file).

 ScanDB, DBase as created via DBSC_Scan().
 SpliceDB filename, Optional default "", default "" produces no output DBase, only a LogFn log file.
   Where provided with an output SpliceDB filename, will:-
    If SpliceDB DOES NOT already Exist, Create zero record SpliceDB in STANDARD format.
    If SpliceDB DOES already Exist, Create SpliceDB in SAME format as existing DBase, but first initialled to zero Records.
   STANDARD Format SpliceDB consists of 12 Int fields and 1 string[256] field, ie to create zero record STANDARD format DB,
     use eg, RT_DBaseAlloc(SpliceDB,0,"iiiiiiiiiiiis")
     
     FieldNumber  Type     Contents
     0             Int     Start of Scene frame              
     1             Int     End of Scene frame
     2             Int     Input clip Width  (same for all DB).
     3             Int     Input clip Height (same for all DB).
     4             Int     Output clip resize Width  (same for all DB).
     5             Int     Output clip resize Height (same for all DB).
     6             Int     Scene Crop left (X) offset
     7             Int     Scene Crop left (Y) offset
     8             Int     Scene Abs Crop Width
     9             Int     Scene Abs Crop Heigth
     10            Int     Scene signed crop width (width relative)
     11            Int     Scene signed crop width (height relative)
     12            String  Resizer String (256 chars], Same for all DB
   You could provide an EXISTING SpliceDB with additional (to STANDARD) fields, where DBSC_DynaCrop() would 1st re-allocate with
   zero records (previous contents lost, but will have same number, type and size fields), and then the STANDARD fields are filled
   with the dynacrop data, any additional fields will be zeroed, ie Float==0.0, String=="".   
   You could later fill your addional (to STANDARD) fields in some fashion, with whatever you want via some other function of your
   own making. These additional user added fields could be accessed later via a DBSC_DynaSplice() 'Func' user function.             
     
 W,H, Output resize dimensions, Defaults to same size as input clip.
 Resizer, String, <Default = "Spline64Resize(_W_,_H_)">. Where '_W_' and '_H_' are replaced with output width and height in the function.
 CropLimit, Default 16, Default for all below Crop edges limits if not otherwise set below.
 CropLimit_Left, Default CropLimit. Maximum allowed border size on Left edge of clip.
 CropLimit_Top, Default CropLimit. Maximum allowed border size on Top edge of clip.
 CropLimit_Right, Default CropLimit. Maximum allowed border size on Right edge of clip.
 CropLimit_Bot, Default CropLimit. Maximum allowed border size on Bottom edge of clip.
 RLBT, Default 15($F). bit flags of edges to crop. 1=Top, 2=Bot, 4=Left, 8=Right. (1+2+4+8=15 = all sides) 
 LeftAdd, Default 0. Amount to add to detected Left edge, and before CropLimit enforced.
 TopAdd, Default 0. Amount to add to detected Top edge, and before CropLimit enforced.
 RightAdd, Default 0. Amount to add to detected Right edge, and before CropLimit enforced.
 BotAdd, Default 0. Amount to add to detected Bottom edge, and before CropLimit enforced.
 DissolveSkip, default 5. Number of frames to skip at either end of scene when detecting borders (try to avoid dissolves and wipes
   playing havoc with detections). Current scene must be at least 4 * DissolveSkip otherwise DissolveSkip ignored.    
 LogFn default "DynaCrop.log". User configurable output log file name.
 LogTMPT Log Template, Default="V_NNNN_ = Trim(_S_,_E_).Crop(_CX_,_CY_,_-CW_,_-CH_)._RSZ_"+Chr(9)+Chr(9)+"# Crop_W= _CW_ Crop_H= _CH_"
   Default produces eg "V0000 = Trim(0,215).Crop(2,4,-14,-6).Spline64Resize(640,400)    # Crop_W= 624 Crop_H= 390"
 ###
     Log TEMPLATE Replacements
      "_NNNN_" = trim/scene number, 0 relative
      "_S_" = Start frame Number, "_E_" = End frame number
      "_CX_", "_CY_", "_CW_","_CH_", Crop x,y,w,h,  "_-CW_" and "_-CH_" are -ve crop width  and height relative
      "_IW_", "_IH_" = Input clip width and height.
      "_W_", "_H_" = Resize width and height.
      "_RSZ_" = The resizer, defaulted or otherwise.

     Example:- Usage of DEFAULT output Log to Trim, Crop, Resize, and Splice [instead of DBSC_DynaSplice()].      
        Function SpliceTrimsString(String VideoString,int maxim,int "Wid",String "Func") {
            Func=Default(Func,"")
            HasFunc=(Func!="")
            Wid=Default(Wid,4)
            GSCript("""
                S=""
                for(i =0,maxim) {
                    S=(S!="" && HasFunc) ? RT_StrAddStr(S,".",Func) : S
                    if(S!="") {S=RT_StrAddStr(S,"+")}
                    S=RT_StrAddStr(S,VideoString+RT_NumberString(i,width=wid))  # RT_StrAddStr, Avoid v2.58 bug
                }
            """)
            return S
        }
        AviSource("Test.avi")
        FUNC="DBSC_Levels(Show=True)"             # DEMO user Func for DBSC_DynaSplice
        
        TRIMS=RT_ReadTxtFromFile("DynaCrop.log")
        Lines=RT_TxtQueryLines(TRIMS)                      # Total lines in text file
        Eval(TRIMS)                                        # Trim, Crop, Resize
        Eval(SpliceTrimsString("V",Lines-1,Func=Func))               # Splice trims
        Return last
 ###
 CropThresh is RoboCrop thresh, Default -32 (RoboCrop DEFAULTED AUTOTHRESH). Best leave alone, will auto adapt.
 ATM, RoboCrop ATM, Default 16.0. Best leave alone, will auto adapt.
 
 RETURN:- Returns clip that has been trimmed, cropped, resized and spliced, with optional info if SHOW=true.
    
*/
Oops, Edited in Blue. Managed to insert an old pre-DEMO version of script.
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 12th February 2015 at 14:50. Reason: Updated
StainlessS is offline   Reply With Quote