modules/motion.py
def ecc_flow(im1, im2, warp_mode=cv2.MOTION_HOMOGRAPHY, niters=1000, eps=1e-8):
    '''
        Register images using Opencv intensity based image alignment approach.
        
        Inputs:
            im1, im2: Images to register. im2 will be registered to im1.
            method: One of cv2.MOTION_*** . Default is MOTION_HOMOGRAPRHY
            niters: Number of ECC iterations
            eps: Stopping tolerance
            
        Outputs:
            warp_matrix: Warping matrix
            im2_aligned: Second image warped to first image's coordinates
            flow: Flow coordinates to go from im2 to im1
            
        https://learnopencv.com/image-alignment-ecc-in-opencv-c-python/
    '''
    # Find size of image1
    sz = im1.shape

    # Define 2x3 or 3x3 matrices and initialize the matrix to identity
    if warp_mode == cv2.MOTION_HOMOGRAPHY :
        warp_matrix = np.eye(3, 3, dtype=np.float32)
    else :
        warp_matrix = np.eye(2, 3, dtype=np.float32)

    # Define termination criteria
    criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, niters, eps)

    # Run the ECC algorithm. The results are stored in warp_matrix.
    (cc, warp_matrix) = cv2.findTransformECC(im1, im2, warp_matrix, warp_mode, 
                                             criteria=criteria, inputMask=None,
                                             gaussFiltSize=5)

    if warp_mode == cv2.MOTION_HOMOGRAPHY :
        # Use warpPerspective for Homography 
        flags = cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP
        im2_aligned = cv2.warpPerspective(im2, warp_matrix, (sz[1],sz[0]), 
                                          flags=flags)
    else :
        # Use warpAffine for Translation, Euclidean and Affine
        flags = cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP
        im2_aligned = cv2.warpAffine(im2, warp_matrix, (sz[1],sz[0]),
                                     flags=flags)
        
    # Create flow coordinates
    Y, X = np.mgrid[:sz[0], :sz[1]]
    coords = np.ones((3, sz[0]*sz[1]))
    coords[0, :] = X.reshape(1, -1)
    coords[1, :] = Y.reshape(1, -1)
    
    coords_new = warp_matrix.dot(coords)
    
    if warp_mode == cv2.MOTION_HOMOGRAPHY:
        coords_new = coords_new[:2, :]/coords_new[2, :]
    
    flow = np.zeros((sz[0], sz[1], 2), dtype=np.float32)
    flow[..., 0] = (coords_new[0, :] - coords[0, :]).reshape(sz)*(2/sz[1])
    flow[..., 1] = (coords_new[1, :] - coords[1, :]).reshape(sz)*(2/sz[0])
    
    return warp_matrix, im2_aligned, flow

        Register images using Opencv intensity based image alignment approach.
        
        Inputs:
            im1, im2: Images to register. im2 will be registered to im1.
            method: One of cv2.MOTION_*** . Default is MOTION_HOMOGRAPRHY
            niters: Number of ECC iterations
            eps: Stopping tolerance
            
        Outputs:
            warp_matrix: Warping matrix
            im2_aligned: Second image warped to first image's coordinates
            flow: Flow coordinates to go from im2 to im1
            
        https://learnopencv.com/image-alignment-ecc-in-opencv-c-python/
    
Register images using Opencv intensity based image alignment approach.
Inputs:
im1, im2: Images to register. im2 will be registered to im1.

Warping matrix
im2_aligned: Second image warped to first image's coordinates
flow: Flow coordinates to go from im2 to im1


Register images using Opencv intensity based image alignment approach . Inputs : im1 im2 : Images to register . im2 will be registered to im1 . Warping matrix im2_aligned : Second image warped to first image ' s coordinates flow : Flow coordinates to go from im2 to im1
def ecc_flow(im1, im2, warp_mode=cv2.MOTION_HOMOGRAPHY, niters=1000, eps=1e-8):
    sz = im1.shape
    if warp_mode == cv2.MOTION_HOMOGRAPHY :
        warp_matrix = np.eye(3, 3, dtype=np.float32)
    else :
        warp_matrix = np.eye(2, 3, dtype=np.float32)
    criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, niters, eps)
    (cc, warp_matrix) = cv2.findTransformECC(im1, im2, warp_matrix, warp_mode, 
                                             criteria=criteria, inputMask=None,
                                             gaussFiltSize=5)
    if warp_mode == cv2.MOTION_HOMOGRAPHY :
        flags = cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP
        im2_aligned = cv2.warpPerspective(im2, warp_matrix, (sz[1],sz[0]), 
                                          flags=flags)
    else :
        flags = cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP
        im2_aligned = cv2.warpAffine(im2, warp_matrix, (sz[1],sz[0]),
                                     flags=flags)
    Y, X = np.mgrid[:sz[0], :sz[1]]
    coords = np.ones((3, sz[0]*sz[1]))
    coords[0, :] = X.reshape(1, -1)
    coords[1, :] = Y.reshape(1, -1)
    coords_new = warp_matrix.dot(coords)
    if warp_mode == cv2.MOTION_HOMOGRAPHY:
        coords_new = coords_new[:2, :]/coords_new[2, :]
    flow = np.zeros((sz[0], sz[1], 2), dtype=np.float32)
    flow[..., 0] = (coords_new[0, :] - coords[0, :]).reshape(sz)*(2/sz[1])
    flow[..., 1] = (coords_new[1, :] - coords[1, :]).reshape(sz)*(2/sz[0])
    return warp_matrix, im2_aligned, flow
def ecc_flow ( im1 , im2 , warp_mode = cv2 . MOTION_HOMOGRAPHY , niters = 1000 , eps = 1e-8 ) : sz = im1 . shape if warp_mode == cv2 . MOTION_HOMOGRAPHY : warp_matrix = np . eye ( 3 , 3 , dtype = np . float32 ) else : warp_matrix = np . eye ( 2 , 3 , dtype = np . float32 ) criteria = ( cv2 . TERM_CRITERIA_EPS | cv2 . TERM_CRITERIA_COUNT , niters , eps ) ( cc , warp_matrix ) = cv2 . findTransformECC ( im1 , im2 , warp_matrix , warp_mode , criteria = criteria , inputMask = None , gaussFiltSize = 5 ) if warp_mode == cv2 . MOTION_HOMOGRAPHY : flags = cv2 . INTER_LINEAR + cv2 . WARP_INVERSE_MAP im2_aligned = cv2 . warpPerspective ( im2 , warp_matrix , ( sz [ 1 ] , sz [ 0 ] ) , flags = flags ) else : flags = cv2 . INTER_LINEAR + cv2 . WARP_INVERSE_MAP im2_aligned = cv2 . warpAffine ( im2 , warp_matrix , ( sz [ 1 ] , sz [ 0 ] ) , flags = flags ) Y , X = np . mgrid [ : sz [ 0 ] , : sz [ 1 ] ] coords = np . ones ( ( 3 , sz [ 0 ] * sz [ 1 ] ) ) coords [ 0 , : ] = X . reshape ( 1 , - 1 ) coords [ 1 , : ] = Y . reshape ( 1 , - 1 ) coords_new = warp_matrix . dot ( coords ) if warp_mode == cv2 . MOTION_HOMOGRAPHY : coords_new = coords_new [ : 2 , : ] / coords_new [ 2 , : ] flow = np . zeros ( ( sz [ 0 ] , sz [ 1 ] , 2 ) , dtype = np . float32 ) flow [ ... , 0 ] = ( coords_new [ 0 , : ] - coords [ 0 , : ] ) . reshape ( sz ) * ( 2 / sz [ 1 ] ) flow [ ... , 1 ] = ( coords_new [ 1 , : ] - coords [ 1 , : ] ) . reshape ( sz ) * ( 2 / sz [ 0 ] ) return warp_matrix , im2_aligned , flow
Register images using Opencv intensity based image alignment approach.
Register images using Opencv intensity based image alignment approach .
'''
        Register images using Opencv intensity based image alignment approach.
        
        Inputs:
            im1, im2: Images to register. im2 will be registered to im1.
            method: One of cv2.MOTION_*** . Default is MOTION_HOMOGRAPRHY
            niters: Number of ECC iterations
            eps: Stopping tolerance
            
        Outputs:
            warp_matrix: Warping matrix
            im2_aligned: Second image warped to first image's coordinates
            flow: Flow coordinates to go from im2 to im1
            
        https://learnopencv.com/image-alignment-ecc-in-opencv-c-python/
    ''' # Find size of image1 # Define 2x3 or 3x3 matrices and initialize the matrix to identity # Define termination criteria # Run the ECC algorithm. The results are stored in warp_matrix. # Use warpPerspective for Homography  # Use warpAffine for Translation, Euclidean and Affine # Create flow coordinates
im1
im2
warp_mode
niters
eps