<s> '''<STR_LIT>''' <EOL> import scipy as sp <EOL> import pyvision as pv <EOL> import math <EOL> import cv <EOL> BG_SUBTRACT_FD = "<STR_LIT>" <EOL> BG_SUBTRACT_MCFD = "<STR_LIT>" <EOL> BG_SUBTRACT_MF = "<STR_LIT>" <EOL> BG_SUBTRACT_AMF = "<STR_LIT>" <EOL> class AbstractBGModel : <EOL> def __init__ ( self , imageBuffer , thresh = <NUM_LIT:20> , soft_thresh = False ) : <EOL> '''<STR_LIT>''' <EOL> self . _imageBuffer = imageBuffer <EOL> self . _threshold = thresh <EOL> self . _softThreshold = soft_thresh <EOL> def _computeBGDiff ( self ) : <EOL> '''<STR_LIT>''' <EOL> raise NotImplemented <EOL> def getForegroundMask ( self ) : <EOL> '''<STR_LIT>''' <EOL> diff = self . _computeBGDiff ( ) <EOL> if self . _softThreshold : <EOL> mask = <NUM_LIT:1> - ( math . e ) ** ( - ( <NUM_LIT:1.0> * diff ) / self . _threshold ) <EOL> else : <EOL> mask = ( sp . absolute ( diff ) > self . _threshold ) <EOL> return pv . Image ( mask * <NUM_LIT> ) <EOL> class FrameDifferencer ( AbstractBGModel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , imageBuffer , thresh = <NUM_LIT:20> , soft_thresh = False ) : <EOL> AbstractBGModel . __init__ ( self , imageBuffer , thresh , soft_thresh ) <EOL> def _computeBGDiff ( self ) : <EOL> prevImg = self . _imageBuffer [ <NUM_LIT:0> ] . asMatrix2D ( ) <EOL> curImg = self . _imageBuffer . getMiddle ( ) . asMatrix2D ( ) <EOL> nextImg = self . _imageBuffer [ - <NUM_LIT:1> ] . asMatrix2D ( ) <EOL> delta1 = sp . absolute ( curImg - prevImg ) <EOL> delta2 = sp . absolute ( nextImg - curImg ) <EOL> return sp . minimum ( delta1 , delta2 ) <EOL> class MotionCompensatedFrameDifferencer ( AbstractBGModel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , imageBuffer , thresh = <NUM_LIT:20> , soft_thresh = False ) : <EOL> AbstractBGModel . __init__ ( self , imageBuffer , thresh , soft_thresh ) <EOL> self . _flow = pv . OpticalFlow ( ) <EOL> if imageBuffer . isFull ( ) : <EOL> self . _initFlow ( ) <EOL> def _initFlow ( self ) : <EOL> '''<STR_LIT>''' <EOL> for i in range ( len ( self . _imageBuffer ) ) : <EOL> self . _flow . update ( self . _imageBuffer [ i ] ) <EOL> def getOpticalFlow ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _flow <EOL> def setOpticalFlow ( self , OF_Object ) : <EOL> '''<STR_LIT>''' <EOL> self . _flow = OF_Object <EOL> def _computeBGDiff ( self ) : <EOL> self . _flow . update ( self . _imageBuffer . getLast ( ) ) <EOL> n = len ( self . _imageBuffer ) <EOL> prev_im = self . _imageBuffer [ <NUM_LIT:0> ] <EOL> forward = None <EOL> for i in range ( <NUM_LIT:0> , n / <NUM_LIT:2> ) : <EOL> if forward == None : <EOL> forward = self . _imageBuffer [ i ] . to_next <EOL> else : <EOL> forward = forward * self . _imageBuffer [ i ] . to_next <EOL> w , h = size = prev_im . size <EOL> mask = cv . CreateImage ( size , cv . IPL_DEPTH_8U , <NUM_LIT:1> ) <EOL> cv . Set ( mask , <NUM_LIT:0> ) <EOL> interior = cv . GetSubRect ( mask , pv . Rect ( <NUM_LIT:2> , <NUM_LIT:2> , w - <NUM_LIT:4> , h - <NUM_LIT:4> ) . asOpenCV ( ) ) <EOL> cv . Set ( interior , <NUM_LIT:255> ) <EOL> mask = pv . Image ( mask ) <EOL> prev_im = forward ( prev_im ) <EOL> prev_mask = forward ( mask ) <EOL> next_im = self . _imageBuffer [ n - <NUM_LIT:1> ] <EOL> back = None <EOL> for i in range ( n - <NUM_LIT:1> , n / <NUM_LIT:2> , - <NUM_LIT:1> ) : <EOL> if back == None : <EOL> back = self . _imageBuffer [ i ] . to_prev <EOL> else : <EOL> back = back * self . _imageBuffer [ i ] . to_prev <EOL> next_im = back ( next_im ) <EOL> next_mask = back ( mask ) <EOL> curr_im = self . _imageBuffer [ n / <NUM_LIT:2> ] <EOL> prevImg = prev_im . asMatrix2D ( ) <EOL> curImg = curr_im . asMatrix2D ( ) <EOL> nextImg = next_im . asMatrix2D ( ) <EOL> prevMask = prev_mask . asMatrix2D ( ) <EOL> nextMask = next_mask . asMatrix2D ( ) <EOL> delta1 = sp . absolute ( curImg - prevImg ) <EOL> delta2 = sp . absolute ( nextImg - curImg ) <EOL> delta1 = sp . minimum ( delta1 , prevMask ) <EOL> delta2 = sp . minimum ( delta2 , nextMask ) <EOL> return sp . minimum ( delta1 , delta2 ) <EOL> class MedianFilter ( AbstractBGModel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , imageBuffer , thresh = <NUM_LIT:20> , soft_thresh = False ) : <EOL> AbstractBGModel . __init__ ( self , imageBuffer , thresh , soft_thresh ) <EOL> def _getMedianVals ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . _imageStack = self . _imageBuffer . asStackBW ( ) <EOL> medians = sp . median ( self . _imageStack , axis = <NUM_LIT:0> ) <EOL> return medians <EOL> def _computeBGDiff ( self ) : <EOL> imgGray = self . _imageBuffer . getLast ( ) . asMatrix2D ( ) <EOL> imgBG = self . _getMedianVals ( ) <EOL> return ( imgGray - imgBG ) <EOL> class ApproximateMedianFilter ( MedianFilter ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , imageBuffer , thresh = <NUM_LIT:20> , soft_thresh = False ) : <EOL> if not imageBuffer . isFull ( ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> MedianFilter . __init__ ( self , imageBuffer , thresh , soft_thresh ) <EOL> self . _medians = self . _getMedianVals ( ) <EOL> def _updateMedian ( self ) : <EOL> curImg = self . _imageBuffer . getLast ( ) <EOL> curMat = curImg . asMatrix2D ( ) <EOL> median = self . _medians <EOL> up = ( curMat > median ) * <NUM_LIT:1.0> <EOL> down = ( curMat < median ) * <NUM_LIT:1.0> <EOL> self . _medians = self . _medians + up - down <EOL> def _computeBGDiff ( self ) : <EOL> self . _updateMedian ( ) <EOL> imgGray = self . _imageBuffer . getLast ( ) . asMatrix2D ( ) <EOL> imgBG = self . _medians <EOL> return ( imgGray - imgBG ) </s>
