<s> from SimpleCV . base import np , cv , math , time , spsd <EOL> from copy import copy <EOL> try : <EOL> import cv2 <EOL> except ImportError : <EOL> pass <EOL> def mfTracker ( img , bb , ts , oldimg , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> numM = <NUM_LIT:10> <EOL> numN = <NUM_LIT:10> <EOL> margin = <NUM_LIT:5> <EOL> winsize_ncc = <NUM_LIT:10> <EOL> winsize_lk = <NUM_LIT:4> <EOL> for key in kwargs : <EOL> if key == '<STR_LIT>' : <EOL> numM = kwargs [ key ] <EOL> elif key == '<STR_LIT>' : <EOL> numN = kwargs [ key ] <EOL> elif key == '<STR_LIT>' : <EOL> margin = kwargs [ key ] <EOL> elif key == '<STR_LIT>' : <EOL> winsize_ncc = kwargs [ key ] <EOL> elif key == '<STR_LIT>' : <EOL> winsize_lk = kwargs [ key ] <EOL> oldg = oldimg . getGrayNumpyCv2 ( ) <EOL> newg = img . getGrayNumpyCv2 ( ) <EOL> bb = [ bb [ <NUM_LIT:0> ] , bb [ <NUM_LIT:1> ] , bb [ <NUM_LIT:0> ] + bb [ <NUM_LIT:2> ] , bb [ <NUM_LIT:1> ] + bb [ <NUM_LIT:3> ] ] <EOL> bb , shift = fbtrack ( oldg , newg , bb , numM , numN , margin , winsize_ncc , winsize_lk ) <EOL> bb = [ bb [ <NUM_LIT:0> ] , bb [ <NUM_LIT:1> ] , bb [ <NUM_LIT:2> ] - bb [ <NUM_LIT:0> ] , bb [ <NUM_LIT:3> ] - bb [ <NUM_LIT:1> ] ] <EOL> track = MFTrack ( img , bb , shift ) <EOL> return track <EOL> def fbtrack ( imgI , imgJ , bb , numM = <NUM_LIT:10> , numN = <NUM_LIT:10> , margin = <NUM_LIT:5> , winsize_ncc = <NUM_LIT:10> , winsize_lk = <NUM_LIT:4> ) : <EOL> """<STR_LIT>""" <EOL> nPoints = numM * numN <EOL> sizePointsArray = nPoints * <NUM_LIT:2> <EOL> pt = getFilledBBPoints ( bb , numM , numN , margin ) <EOL> fb , ncc , status , ptTracked = lktrack ( imgI , imgJ , pt , nPoints , winsize_ncc , winsize_lk ) <EOL> nlkPoints = sum ( status ) [ <NUM_LIT:0> ] <EOL> startPoints = [ ] <EOL> targetPoints = [ ] <EOL> fbLKCleaned = [ <NUM_LIT:0.0> ] * nlkPoints <EOL> nccLKCleaned = [ <NUM_LIT:0.0> ] * nlkPoints <EOL> M = <NUM_LIT:2> <EOL> nRealPoints = <NUM_LIT:0> <EOL> for i in range ( nPoints ) : <EOL> if ptTracked [ M * i ] is not - <NUM_LIT:1> : <EOL> startPoints . append ( ( pt [ <NUM_LIT:2> * i ] , pt [ <NUM_LIT:2> * i + <NUM_LIT:1> ] ) ) <EOL> targetPoints . append ( ( ptTracked [ <NUM_LIT:2> * i ] , ptTracked [ <NUM_LIT:2> * i + <NUM_LIT:1> ] ) ) <EOL> fbLKCleaned [ nRealPoints ] = fb [ i ] <EOL> nccLKCleaned [ nRealPoints ] = ncc [ i ] <EOL> nRealPoints += <NUM_LIT:1> <EOL> medFb = getMedian ( fbLKCleaned ) <EOL> medNcc = getMedian ( nccLKCleaned ) <EOL> nAfterFbUsage = <NUM_LIT:0> <EOL> for i in range ( nlkPoints ) : <EOL> if fbLKCleaned [ i ] <= medFb and nccLKCleaned [ i ] >= medNcc : <EOL> startPoints [ nAfterFbUsage ] = startPoints [ i ] <EOL> targetPoints [ nAfterFbUsage ] = targetPoints [ i ] <EOL> nAfterFbUsage += <NUM_LIT:1> <EOL> newBB , scaleshift = predictBB ( bb , startPoints , targetPoints , nAfterFbUsage ) <EOL> return ( newBB , scaleshift ) <EOL> def lktrack ( img1 , img2 , ptsI , nPtsI , winsize_ncc = <NUM_LIT:10> , win_size_lk = <NUM_LIT:4> , method = cv2 . cv . CV_TM_CCOEFF_NORMED ) : <EOL> """<STR_LIT>""" <EOL> template_pt = [ ] <EOL> target_pt = [ ] <EOL> fb_pt = [ ] <EOL> ptsJ = [ - <NUM_LIT:1> ] * len ( ptsI ) <EOL> for i in range ( nPtsI ) : <EOL> template_pt . append ( ( ptsI [ <NUM_LIT:2> * i ] , ptsI [ <NUM_LIT:2> * i + <NUM_LIT:1> ] ) ) <EOL> target_pt . append ( ( ptsI [ <NUM_LIT:2> * i ] , ptsI [ <NUM_LIT:2> * i + <NUM_LIT:1> ] ) ) <EOL> fb_pt . append ( ( ptsI [ <NUM_LIT:2> * i ] , ptsI [ <NUM_LIT:2> * i + <NUM_LIT:1> ] ) ) <EOL> template_pt = np . asarray ( template_pt , dtype = "<STR_LIT>" ) <EOL> target_pt = np . asarray ( target_pt , dtype = "<STR_LIT>" ) <EOL> fb_pt = np . asarray ( fb_pt , dtype = "<STR_LIT>" ) <EOL> target_pt , status , track_error = cv2 . calcOpticalFlowPyrLK ( img1 , img2 , template_pt , target_pt , <EOL> winSize = ( win_size_lk , win_size_lk ) , flags = cv2 . OPTFLOW_USE_INITIAL_FLOW , <EOL> criteria = ( cv2 . TERM_CRITERIA_EPS | cv2 . TERM_CRITERIA_COUNT , <NUM_LIT:10> , <NUM_LIT> ) ) <EOL> fb_pt , status_bt , track_error_bt = cv2 . calcOpticalFlowPyrLK ( img2 , img1 , target_pt , fb_pt , <EOL> winSize = ( win_size_lk , win_size_lk ) , flags = cv2 . OPTFLOW_USE_INITIAL_FLOW , <EOL> criteria = ( cv2 . TERM_CRITERIA_EPS | cv2 . TERM_CRITERIA_COUNT , <NUM_LIT:10> , <NUM_LIT> ) ) <EOL> status = status & status_bt <EOL> ncc = normCrossCorrelation ( img1 , img2 , template_pt , target_pt , status , winsize_ncc , method ) <EOL> fb = euclideanDistance ( template_pt , target_pt ) <EOL> newfb = - <NUM_LIT:1> * np . ones ( len ( fb ) ) <EOL> newncc = - <NUM_LIT:1> * np . ones ( len ( ncc ) ) <EOL> for i in np . argwhere ( status ) : <EOL> i = i [ <NUM_LIT:0> ] <EOL> ptsJ [ <NUM_LIT:2> * i ] = target_pt [ i ] [ <NUM_LIT:0> ] <EOL> ptsJ [ <NUM_LIT:2> * i + <NUM_LIT:1> ] = target_pt [ i ] [ <NUM_LIT:1> ] <EOL> newfb [ i ] = fb [ i ] <EOL> newncc [ i ] = ncc [ i ] <EOL> return newfb , newncc , status , ptsJ <EOL> def getMedianUnmanaged ( a ) : <EOL> if not a : <EOL> return None <EOL> newl = copy ( a ) <EOL> newl . sort ( ) <EOL> while True : <EOL> try : <EOL> newl . remove ( <NUM_LIT:0> ) <EOL> except ValueError : <EOL> if newl : <EOL> return newl [ len ( newl ) / <NUM_LIT:2> ] <EOL> return <NUM_LIT:0> <EOL> def getMedian ( a ) : <EOL> median = getMedianUnmanaged ( a ) <EOL> return median <EOL> def calculateBBCenter ( bb ) : <EOL> """<STR_LIT>""" <EOL> center = ( <NUM_LIT:0.5> * ( bb [ <NUM_LIT:0> ] + bb [ <NUM_LIT:2> ] ) , <NUM_LIT:0.5> * ( bb [ <NUM_LIT:1> ] + bb [ <NUM_LIT:3> ] ) ) <EOL> return center <EOL> def getFilledBBPoints ( bb , numM , numN , margin ) : <EOL> """<STR_LIT>""" <EOL> pointDim = <NUM_LIT:2> <EOL> bb_local = ( bb [ <NUM_LIT:0> ] + margin , bb [ <NUM_LIT:1> ] + margin , bb [ <NUM_LIT:2> ] - margin , bb [ <NUM_LIT:3> ] - margin ) <EOL> if numM == <NUM_LIT:1> and numN == <NUM_LIT:1> : <EOL> pts = calculateBBCenter ( bb_local ) <EOL> return pts <EOL> elif numM > <NUM_LIT:1> and numN == <NUM_LIT:1> : <EOL> divM = numM - <NUM_LIT:1> <EOL> divN = <NUM_LIT:2> <EOL> spaceM = ( bb_local [ <NUM_LIT:3> ] - bb_local [ <NUM_LIT:1> ] ) / divM <EOL> center = calculateBBCenter ( bb_local ) <EOL> pt = [ <NUM_LIT:0.0> ] * ( <NUM_LIT:2> * numM * numN ) <EOL> for i in range ( numN ) : <EOL> for j in range ( numM ) : <EOL> pt [ i * numM * pointDim + j * pointDim + <NUM_LIT:0> ] = center [ <NUM_LIT:0> ] <EOL> pt [ i * numM * pointDim + j * pointDim + <NUM_LIT:1> ] = bb_local [ <NUM_LIT:1> ] + j * spaceM <EOL> return pt <EOL> elif numM == <NUM_LIT:1> and numN > <NUM_LIT:1> : <EOL> divM = <NUM_LIT:2> <EOL> divN = numN - <NUM_LIT:1> <EOL> spaceN = ( bb_local [ <NUM_LIT:2> ] - bb_local [ <NUM_LIT:0> ] ) / divN <EOL> center = calculateBBCenter ( bb_local ) <EOL> pt = [ <NUM_LIT:0.0> ] * ( ( numN - <NUM_LIT:1> ) * numM * pointDim + numN * pointDim ) <EOL> for i in range ( numN ) : <EOL> for j in range ( numN ) : <EOL> pt [ i * numM * pointDim + j * pointDim + <NUM_LIT:0> ] = bb_local [ <NUM_LIT:0> ] + i * spaceN <EOL> pt [ i * numM * pointDim + j * pointDim + <NUM_LIT:1> ] = center [ <NUM_LIT:1> ] <EOL> return pt <EOL> elif numM > <NUM_LIT:1> and numN > <NUM_LIT:1> : <EOL> divM = numM - <NUM_LIT:1> <EOL> divN = numN - <NUM_LIT:1> <EOL> spaceN = ( bb_local [ <NUM_LIT:2> ] - bb_local [ <NUM_LIT:0> ] ) / divN <EOL> spaceM = ( bb_local [ <NUM_LIT:3> ] - bb_local [ <NUM_LIT:1> ] ) / divM <EOL> pt = [ <NUM_LIT:0.0> ] * ( ( numN - <NUM_LIT:1> ) * numM * pointDim + numM * pointDim ) <EOL> for i in range ( numN ) : <EOL> for j in range ( numM ) : <EOL> pt [ i * numM * pointDim + j * pointDim + <NUM_LIT:0> ] = float ( bb_local [ <NUM_LIT:0> ] + i * spaceN ) <EOL> pt [ i * numM * pointDim + j * pointDim + <NUM_LIT:1> ] = float ( bb_local [ <NUM_LIT:1> ] + j * spaceM ) <EOL> return pt <EOL> def getBBWidth ( bb ) : <EOL> """<STR_LIT>""" <EOL> return bb [ <NUM_LIT:2> ] - bb [ <NUM_LIT:0> ] + <NUM_LIT:1> <EOL> def getBBHeight ( bb ) : <EOL> """<STR_LIT>""" <EOL> return bb [ <NUM_LIT:3> ] - bb [ <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> def predictBB ( bb0 , pt0 , pt1 , nPts ) : <EOL> """<STR_LIT>""" <EOL> ofx = [ ] <EOL> ofy = [ ] <EOL> for i in range ( nPts ) : <EOL> ofx . append ( pt1 [ i ] [ <NUM_LIT:0> ] - pt0 [ i ] [ <NUM_LIT:0> ] ) <EOL> ofy . append ( pt1 [ i ] [ <NUM_LIT:1> ] - pt0 [ i ] [ <NUM_LIT:1> ] ) <EOL> dx = getMedianUnmanaged ( ofx ) <EOL> dy = getMedianUnmanaged ( ofy ) <EOL> ofx = ofy = <NUM_LIT:0> <EOL> lenPdist = nPts * ( nPts - <NUM_LIT:1> ) / <NUM_LIT:2> <EOL> dist0 = [ ] <EOL> for i in range ( nPts ) : <EOL> for j in range ( i + <NUM_LIT:1> , nPts ) : <EOL> temp0 = ( ( pt0 [ i ] [ <NUM_LIT:0> ] - pt0 [ j ] [ <NUM_LIT:0> ] ) ** <NUM_LIT:2> + ( pt0 [ i ] [ <NUM_LIT:1> ] - pt0 [ j ] [ <NUM_LIT:1> ] ) ** <NUM_LIT:2> ) ** <NUM_LIT:0.5> <EOL> temp1 = ( ( pt1 [ i ] [ <NUM_LIT:0> ] - pt1 [ j ] [ <NUM_LIT:0> ] ) ** <NUM_LIT:2> + ( pt1 [ i ] [ <NUM_LIT:1> ] - pt1 [ j ] [ <NUM_LIT:1> ] ) ** <NUM_LIT:2> ) ** <NUM_LIT:0.5> <EOL> if temp0 != <NUM_LIT:0> : <EOL> dist0 . append ( float ( temp1 ) / temp0 ) <EOL> else : <EOL> dist0 . append ( <NUM_LIT:1.0> ) <EOL> shift = getMedianUnmanaged ( dist0 ) <EOL> if shift is None : <EOL> return ( bb0 , <NUM_LIT:1.0> ) <EOL> if shift > <NUM_LIT> or shift < <NUM_LIT> : <EOL> shift = <NUM_LIT:1> <EOL> s0 = <NUM_LIT:0.5> * ( shift - <NUM_LIT:1> ) * getBBWidth ( bb0 ) <EOL> s1 = <NUM_LIT:0.5> * ( shift - <NUM_LIT:1> ) * getBBHeight ( bb0 ) <EOL> x1 = bb0 [ <NUM_LIT:0> ] - s0 + dx <EOL> y1 = bb0 [ <NUM_LIT:1> ] - s1 + dy <EOL> x2 = bb0 [ <NUM_LIT:2> ] + s0 + dx <EOL> y2 = bb0 [ <NUM_LIT:3> ] + s1 + dy <EOL> w = x2 - x1 <EOL> h = y2 - y1 <EOL> if x1 <= <NUM_LIT:0> or x2 <= <NUM_LIT:0> or y1 <= <NUM_LIT:0> or y2 <= <NUM_LIT:0> or w <= <NUM_LIT:20> or h <= <NUM_LIT:20> : <EOL> x1 = bb0 [ <NUM_LIT:0> ] <EOL> y1 = bb0 [ <NUM_LIT:1> ] <EOL> x2 = bb0 [ <NUM_LIT:2> ] <EOL> y2 = bb0 [ <NUM_LIT:3> ] <EOL> bb1 = ( int ( x1 ) , int ( y1 ) , int ( x2 ) , int ( y2 ) ) <EOL> return ( bb1 , shift ) <EOL> def getBB ( pt0 , pt1 ) : <EOL> xmax = np . max ( ( pt0 [ <NUM_LIT:0> ] , pt1 [ <NUM_LIT:0> ] ) ) <EOL> xmin = np . min ( ( pt0 [ <NUM_LIT:0> ] , pt1 [ <NUM_LIT:0> ] ) ) <EOL> ymax = np . max ( ( pt0 [ <NUM_LIT:1> ] , pt1 [ <NUM_LIT:1> ] ) ) <EOL> ymin = np . min ( ( pt0 [ <NUM_LIT:1> ] , pt1 [ <NUM_LIT:1> ] ) ) <EOL> return xmin , ymin , xmax , ymax <EOL> def getRectFromBB ( bb ) : <EOL> return bb [ <NUM_LIT:0> ] , bb [ <NUM_LIT:1> ] , bb [ <NUM_LIT:2> ] - bb [ <NUM_LIT:0> ] , bb [ <NUM_LIT:3> ] - bb [ <NUM_LIT:1> ] <EOL> def euclideanDistance ( point1 , point2 ) : <EOL> """<STR_LIT>""" <EOL> match = ( ( point1 [ : , <NUM_LIT:0> ] - point2 [ : , <NUM_LIT:0> ] ) ** <NUM_LIT:2> + ( point1 [ : , <NUM_LIT:1> ] - point2 [ : , <NUM_LIT:1> ] ) ** <NUM_LIT:2> ) ** <NUM_LIT:0.5> <EOL> return match <EOL> def normCrossCorrelation ( img1 , img2 , pt0 , pt1 , status , winsize , method = cv2 . cv . CV_TM_CCOEFF_NORMED ) : <EOL> """<STR_LIT>""" <EOL> nPts = len ( pt0 ) <EOL> match = np . zeros ( nPts ) <EOL> for i in np . argwhere ( status ) : <EOL> i = i [ <NUM_LIT:0> ] <EOL> patch1 = cv2 . getRectSubPix ( img1 , ( winsize , winsize ) , tuple ( pt0 [ i ] ) ) <EOL> patch2 = cv2 . getRectSubPix ( img2 , ( winsize , winsize ) , tuple ( pt1 [ i ] ) ) <EOL> match [ i ] = cv2 . matchTemplate ( patch1 , patch2 , method ) <EOL> return match <EOL> from SimpleCV . Tracking import MFTrack </s>
