modules/motion.py
def register_stack_ecc(imstack, full_res, method=cv2.MOTION_EUCLIDEAN):
    '''
        Register a stack of images and get coordinates
        
        Inputs:
            imstack: nimg x H x W stack of images
            full_res: Resolution at which images will be super resolved
            method: Method to use for ECC registration
            
        Outputs:
            Xstack: X Coordinates for registration
            Ystack: Y Coordinates for registration
            mask: (nimg, ) dimensional mask for images that were successfully
                registered
            alignment_err: (nimg, ) dimensional array of alignment error
    '''
    nimg, H, W = imstack.shape
    Hr, Wr = full_res
    
    mask = np.zeros(nimg)
    alignment_err = np.zeros(nimg)
    
    Xstack = np.zeros((nimg, H, W), dtype=np.float32)
    Ystack = np.zeros((nimg, H, W), dtype=np.float32)
    
    imref = cv2.resize(imstack[0, ...], (Wr, Hr),
                       interpolation=cv2.INTER_LINEAR)
    mask[0] = 1
    
    Y, X = np.mgrid[:Hr, :Wr]
    X = 2*X/Wr - 1
    Y = 2*Y/Hr - 1
    
    Xstack[0, ...] = cv2.resize(X, (W, H), interpolation=cv2.INTER_LINEAR)
    Ystack[0, ...] = cv2.resize(Y, (W, H), interpolation=cv2.INTER_LINEAR)
    
    if method == cv2.MOTION_HOMOGRAPHY:
        ecc_mats = np.zeros((nimg, 3, 3))
        ecc_mats[0, 2, 2] = 1
    else:
        ecc_mats = np.zeros((nimg, 2, 3))
        
    # First image is registered ... to itself
    ecc_mats[0, 0, 0] = 1
    ecc_mats[0, 1, 1] = 1
    
    for idx in tqdm.tqdm(range(1, nimg)):
        im2 = cv2.resize(imstack[idx, ...], (Wr, Hr),
                         interpolation=cv2.INTER_LINEAR)
        try:
            mat, im2_aligned, flow = ecc_flow(imref, im2, warp_mode=method)
            mask[idx] = 1
            ecc_mats[idx, :] = mat
            Xstack[idx, ...] = cv2.resize(X - flow[..., 0], (W, H),
                                          interpolation=cv2.INTER_LINEAR)
            
            Ystack[idx, ...] = cv2.resize(Y - flow[..., 1], (W, H),
                                          interpolation=cv2.INTER_LINEAR)
            
            spatial_mask = (im2_aligned != 0)
            alignment_err[idx] = abs((imref - im2_aligned)*spatial_mask).mean()
        except:
            mask[idx] = 0
            continue
    
    # Now return the coordinates
    return Xstack, Ystack, mask, ecc_mats, alignment_err

        Register a stack of images and get coordinates
        
        Inputs:
            imstack: nimg x H x W stack of images
            full_res: Resolution at which images will be super resolved
            method: Method to use for ECC registration
            
        Outputs:
            Xstack: X Coordinates for registration
            Ystack: Y Coordinates for registration
            mask: (nimg, ) dimensional mask for images that were successfully
                registered
            alignment_err: (nimg, ) dimensional array of alignment error
    
Register a stack of images and get coordinates
Inputs:
imstack: nimg x H x W stack of images
full_res: Resolution at which images will be super resolved
method: Method to use for ECC registration

X Coordinates for registration
Ystack: Y Coordinates for registration
mask: (nimg, ) dimensional mask for images that were successfully
registered
alignment_err: (nimg, ) dimensional array of alignment error
Register a stack of images and get coordinates Inputs : imstack : nimg x H x W stack of images full_res : Resolution at which images will be super resolved method : Method to use for ECC registration X Coordinates for registration Ystack : Y Coordinates for registration mask : ( nimg ) dimensional mask for images that were successfully registered alignment_err : ( nimg ) dimensional array of alignment error
def register_stack_ecc(imstack, full_res, method=cv2.MOTION_EUCLIDEAN):
    nimg, H, W = imstack.shape
    Hr, Wr = full_res
    mask = np.zeros(nimg)
    alignment_err = np.zeros(nimg)
    Xstack = np.zeros((nimg, H, W), dtype=np.float32)
    Ystack = np.zeros((nimg, H, W), dtype=np.float32)
    imref = cv2.resize(imstack[0, ...], (Wr, Hr),
                       interpolation=cv2.INTER_LINEAR)
    mask[0] = 1
    Y, X = np.mgrid[:Hr, :Wr]
    X = 2*X/Wr - 1
    Y = 2*Y/Hr - 1
    Xstack[0, ...] = cv2.resize(X, (W, H), interpolation=cv2.INTER_LINEAR)
    Ystack[0, ...] = cv2.resize(Y, (W, H), interpolation=cv2.INTER_LINEAR)
    if method == cv2.MOTION_HOMOGRAPHY:
        ecc_mats = np.zeros((nimg, 3, 3))
        ecc_mats[0, 2, 2] = 1
    else:
        ecc_mats = np.zeros((nimg, 2, 3))
    ecc_mats[0, 0, 0] = 1
    ecc_mats[0, 1, 1] = 1
    for idx in tqdm.tqdm(range(1, nimg)):
        im2 = cv2.resize(imstack[idx, ...], (Wr, Hr),
                         interpolation=cv2.INTER_LINEAR)
        try:
            mat, im2_aligned, flow = ecc_flow(imref, im2, warp_mode=method)
            mask[idx] = 1
            ecc_mats[idx, :] = mat
            Xstack[idx, ...] = cv2.resize(X - flow[..., 0], (W, H),
                                          interpolation=cv2.INTER_LINEAR)
            Ystack[idx, ...] = cv2.resize(Y - flow[..., 1], (W, H),
                                          interpolation=cv2.INTER_LINEAR)
            spatial_mask = (im2_aligned != 0)
            alignment_err[idx] = abs((imref - im2_aligned)*spatial_mask).mean()
        except:
            mask[idx] = 0
            continue
    return Xstack, Ystack, mask, ecc_mats, alignment_err
def register_stack_ecc ( imstack , full_res , method = cv2 . MOTION_EUCLIDEAN ) : nimg , H , W = imstack . shape Hr , Wr = full_res mask = np . zeros ( nimg ) alignment_err = np . zeros ( nimg ) Xstack = np . zeros ( ( nimg , H , W ) , dtype = np . float32 ) Ystack = np . zeros ( ( nimg , H , W ) , dtype = np . float32 ) imref = cv2 . resize ( imstack [ 0 , ... ] , ( Wr , Hr ) , interpolation = cv2 . INTER_LINEAR ) mask [ 0 ] = 1 Y , X = np . mgrid [ : Hr , : Wr ] X = 2 * X / Wr - 1 Y = 2 * Y / Hr - 1 Xstack [ 0 , ... ] = cv2 . resize ( X , ( W , H ) , interpolation = cv2 . INTER_LINEAR ) Ystack [ 0 , ... ] = cv2 . resize ( Y , ( W , H ) , interpolation = cv2 . INTER_LINEAR ) if method == cv2 . MOTION_HOMOGRAPHY : ecc_mats = np . zeros ( ( nimg , 3 , 3 ) ) ecc_mats [ 0 , 2 , 2 ] = 1 else : ecc_mats = np . zeros ( ( nimg , 2 , 3 ) ) ecc_mats [ 0 , 0 , 0 ] = 1 ecc_mats [ 0 , 1 , 1 ] = 1 for idx in tqdm . tqdm ( range ( 1 , nimg ) ) : im2 = cv2 . resize ( imstack [ idx , ... ] , ( Wr , Hr ) , interpolation = cv2 . INTER_LINEAR ) try : mat , im2_aligned , flow = ecc_flow ( imref , im2 , warp_mode = method ) mask [ idx ] = 1 ecc_mats [ idx , : ] = mat Xstack [ idx , ... ] = cv2 . resize ( X - flow [ ... , 0 ] , ( W , H ) , interpolation = cv2 . INTER_LINEAR ) Ystack [ idx , ... ] = cv2 . resize ( Y - flow [ ... , 1 ] , ( W , H ) , interpolation = cv2 . INTER_LINEAR ) spatial_mask = ( im2_aligned != 0 ) alignment_err [ idx ] = abs ( ( imref - im2_aligned ) * spatial_mask ) . mean ( ) except : mask [ idx ] = 0 continue return Xstack , Ystack , mask , ecc_mats , alignment_err
Register a stack of images and get coordinates
Inputs:
imstack: nimg x H x W stack of images
full_res: Resolution at which images will be super resolved
method: Method to use for ECC registration
Register a stack of images and get coordinates Inputs : imstack : nimg x H x W stack of images full_res : Resolution at which images will be super resolved method : Method to use for ECC registration
'''
        Register a stack of images and get coordinates
        
        Inputs:
            imstack: nimg x H x W stack of images
            full_res: Resolution at which images will be super resolved
            method: Method to use for ECC registration
            
        Outputs:
            Xstack: X Coordinates for registration
            Ystack: Y Coordinates for registration
            mask: (nimg, ) dimensional mask for images that were successfully
                registered
            alignment_err: (nimg, ) dimensional array of alignment error
    ''' # First image is registered ... to itself # Now return the coordinates
imstack
full_res
method