modules/motion.py
def param2theta(params, w, h):
    '''
        Convert affine matrix to parameter that torch can use
        
        Inputs:
            params: nimg x 2 x 3 affine matrices
            w, h: Width and height of the image
            
        Outputs:
            theta: Matrix to use with grid_sample (for example)
            
        Reference:
        https://discuss.pytorch.org/t/how-to-convert-an-affine-transform-matrix-into-theta-to-use-torch-nn-functional-affine-grid/24315/4
    '''
    last_row = np.zeros((1, 3), dtype=np.float32)
    last_row[0, 2] = 1
    theta = np.zeros_like(params)
    
    for idx in range(params.shape[0]):
        param = np.vstack((params[idx, ...], last_row))
        param = np.linalg.inv(param)
        theta[idx,0,0] = param[0,0]
        theta[idx,0,1] = param[0,1]*h/w
        theta[idx,0,2] = param[0,2]*2/w + theta[idx,0,0] + theta[idx,0,1] - 1
        #theta[idx, 0, 2] = param[0, 2]*2/w + param[0, 0] + param[0, 1] - 1
        theta[idx,1,0] = param[1,0]*w/h
        theta[idx,1,1] = param[1,1]
        theta[idx,1,2] = param[1,2]*2/h + theta[idx,1,0] + theta[idx,1,1] - 1
        #theta[idx, 1, 2] = param[1, 2]*2/h + param[1, 0] + param[1, 1] - 1

    return theta

        Convert affine matrix to parameter that torch can use
        
        Inputs:
            params: nimg x 2 x 3 affine matrices
            w, h: Width and height of the image
            
        Outputs:
            theta: Matrix to use with grid_sample (for example)
            
        Reference:
        https://discuss.pytorch.org/t/how-to-convert-an-affine-transform-matrix-into-theta-to-use-torch-nn-functional-affine-grid/24315/4
    
Convert affine matrix to parameter that torch can use
Inputs:
params: nimg x 2 x 3 affine matrices
w, h: Width and height of the image

Matrix to use with grid_sample (for example)


Convert affine matrix to parameter that torch can use Inputs : params : nimg x 2 x 3 affine matrices w h : Width and height of the image Matrix to use with grid_sample ( for example )
def param2theta(params, w, h):
    last_row = np.zeros((1, 3), dtype=np.float32)
    last_row[0, 2] = 1
    theta = np.zeros_like(params)
    for idx in range(params.shape[0]):
        param = np.vstack((params[idx, ...], last_row))
        param = np.linalg.inv(param)
        theta[idx,0,0] = param[0,0]
        theta[idx,0,1] = param[0,1]*h/w
        theta[idx,0,2] = param[0,2]*2/w + theta[idx,0,0] + theta[idx,0,1] - 1
        theta[idx,1,0] = param[1,0]*w/h
        theta[idx,1,1] = param[1,1]
        theta[idx,1,2] = param[1,2]*2/h + theta[idx,1,0] + theta[idx,1,1] - 1
    return theta
def param2theta ( params , w , h ) : last_row = np . zeros ( ( 1 , 3 ) , dtype = np . float32 ) last_row [ 0 , 2 ] = 1 theta = np . zeros_like ( params ) for idx in range ( params . shape [ 0 ] ) : param = np . vstack ( ( params [ idx , ... ] , last_row ) ) param = np . linalg . inv ( param ) theta [ idx , 0 , 0 ] = param [ 0 , 0 ] theta [ idx , 0 , 1 ] = param [ 0 , 1 ] * h / w theta [ idx , 0 , 2 ] = param [ 0 , 2 ] * 2 / w + theta [ idx , 0 , 0 ] + theta [ idx , 0 , 1 ] - 1 theta [ idx , 1 , 0 ] = param [ 1 , 0 ] * w / h theta [ idx , 1 , 1 ] = param [ 1 , 1 ] theta [ idx , 1 , 2 ] = param [ 1 , 2 ] * 2 / h + theta [ idx , 1 , 0 ] + theta [ idx , 1 , 1 ] - 1 return theta
Convert affine matrix to parameter that torch can use
Inputs:
params: nimg x 2 x 3 affine matrices
w, h: Width and height of the image
Convert affine matrix to parameter that torch can use Inputs : params : nimg x 2 x 3 affine matrices w h : Width and height of the image
'''
        Convert affine matrix to parameter that torch can use
        
        Inputs:
            params: nimg x 2 x 3 affine matrices
            w, h: Width and height of the image
            
        Outputs:
            theta: Matrix to use with grid_sample (for example)
            
        Reference:
        https://discuss.pytorch.org/t/how-to-convert-an-affine-transform-matrix-into-theta-to-use-torch-nn-functional-affine-grid/24315/4
    ''' #theta[idx, 0, 2] = param[0, 2]*2/w + param[0, 0] + param[0, 1] - 1 #theta[idx, 1, 2] = param[1, 2]*2/h + param[1, 0] + param[1, 1] - 1
params
w
h