def warp ( self , warp_matrix , img , iflag = cv2 . INTER_NEAREST ) : height , width = img . shape [ : 2 ] warped_img = np . zeros_like ( img , dtype = img . dtype ) # Check if image to warp is 2D or 3D. If 3D need to loop over channels if ( self . interpolation_type == InterpolationType . LINEAR ) or img . ndim == 2 : warped_img = cv2 . warpAffine ( img . astype ( np . float32 ) , warp_matrix , ( width , height ) , flags = iflag ) . astype ( img . dtype ) elif img . ndim == 3 : for idx in range ( img . shape [ - 1 ] ) : warped_img [ ... , idx ] = cv2 . warpAffine ( img [ ... , idx ] . astype ( np . float32 ) , warp_matrix , ( width , height ) , flags = iflag ) . astype ( img . dtype ) else : raise ValueError ( 'Image has incorrect number of dimensions: {}' . format ( img . ndim ) ) return warped_img
python opencv2 warpaffine center
Function to warp input image given an estimated 2D linear transformation
def warp(self, warp_matrix, img, iflag=cv2.INTER_NEAREST):
        """ Function to warp input image given an estimated 2D linear transformation

        :param warp_matrix: Linear 2x3 matrix to use to linearly warp the input images
        :type warp_matrix: ndarray
        :param img: Image to be warped with estimated transformation
        :type img: ndarray
        :param iflag: Interpolation flag, specified interpolation using during resampling of warped image
        :type iflag: cv2.INTER_*
        :return: Warped image using the linear matrix
        """

        height, width = img.shape[:2]
        warped_img = np.zeros_like(img, dtype=img.dtype)

        # Check if image to warp is 2D or 3D. If 3D need to loop over channels
        if (self.interpolation_type == InterpolationType.LINEAR) or img.ndim == 2:
            warped_img = cv2.warpAffine(img.astype(np.float32), warp_matrix, (width, height),
                                        flags=iflag).astype(img.dtype)

        elif img.ndim == 3:
            for idx in range(img.shape[-1]):
                warped_img[..., idx] = cv2.warpAffine(img[..., idx].astype(np.float32), warp_matrix, (width, height),
                                                      flags=iflag).astype(img.dtype)
        else:
            raise ValueError('Image has incorrect number of dimensions: {}'.format(img.ndim))

        return warped_img