Python cv2.estimateRigidTransform() Examples

The following are 13 code examples of cv2.estimateRigidTransform(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cv2 , or try the search function .
Example #1
Source File: vidstab_utils.py    From python_video_stab with MIT License 6 votes vote down vote up
def estimate_partial_transform(matched_keypoints):
    """Wrapper of cv2.estimateRigidTransform for convenience in vidstab process

    :param matched_keypoints: output of match_keypoints util function; tuple of (cur_matched_kp, prev_matched_kp)
    :return: transform as list of [dx, dy, da]
    """
    cur_matched_kp, prev_matched_kp = matched_keypoints

    transform = cv2_estimateRigidTransform(np.array(prev_matched_kp),
                                           np.array(cur_matched_kp),
                                           False)
    if transform is not None:
        # translation x
        dx = transform[0, 2]
        # translation y
        dy = transform[1, 2]
        # rotation
        da = np.arctan2(transform[1, 0], transform[0, 0])
    else:
        dx = dy = da = 0

    return [dx, dy, da] 
Example #2
Source File: face_rectify.py    From Face-Sketch-Wild with MIT License 6 votes vote down vote up
def similarityTransform(inPoints, outPoints) :
    """
    Calculate similarity transform:
    Input:
        (left eye, right eye) in (x, y)
        inPoints: (2, 2), numpy array. 
        outPoints: (2, 2), numpy array
    Return:
        A partial affine transform.
    """
    s60 = math.sin(60*math.pi/180) 
    c60 = math.cos(60*math.pi/180) 

    inPts  = np.copy(inPoints).tolist() 
    outPts = np.copy(outPoints).tolist() 
    xin = c60*(inPts[0][0] - inPts[1][0]) - s60*(inPts[0][1] - inPts[1][1]) + inPts[1][0] 
    yin = s60*(inPts[0][0] - inPts[1][0]) + c60*(inPts[0][1] - inPts[1][1]) + inPts[1][1] 
    inPts.append([np.int(xin), np.int(yin)]) 

    xout = c60*(outPts[0][0] - outPts[1][0]) - s60*(outPts[0][1] - outPts[1][1]) + outPts[1][0] 
    yout = s60*(outPts[0][0] - outPts[1][0]) + c60*(outPts[0][1] - outPts[1][1]) + outPts[1][1] 
    outPts.append([np.int(xout), np.int(yout)]) 
    tform = cv.estimateRigidTransform(np.array([inPts]), np.array([outPts]), False) 

    return tform 
Example #3
Source File: utilities_CVbasic_v2.py    From MachineLearningSamples-ImageClassificationUsingCntk with MIT License 5 votes vote down vote up
def imRigidTransform(img, srcPts, dstPts):
    srcPts = np.array([srcPts], np.int)
    dstPts = np.array([dstPts], np.int)
    M = cv2.estimateRigidTransform(srcPts, dstPts, False)
    if transformation is not None:
        return cv2.warpAffine(img, M)
    else:
        return None 
Example #4
Source File: cv2_utils.py    From python_video_stab with MIT License 5 votes vote down vote up
def cv2_estimateRigidTransform(from_pts, to_pts, full=False):
    """Estimate transforms in OpenCV 3 or OpenCV 4"""
    if not from_pts.shape[0] or not to_pts.shape[0]:
        return None

    if imutils.is_cv4():
        transform = cv2.estimateAffinePartial2D(from_pts, to_pts)[0]
    else:
        # noinspection PyUnresolvedReferences
        transform = cv2.estimateRigidTransform(from_pts, to_pts, full)

    return transform 
Example #5
Source File: AverageFace.py    From Machine-Learning-Study-Notes with Apache License 2.0 5 votes vote down vote up
def similarityTransform(input, output):
        s60 = np.sin(60 * np.pi / 180.0)
        c60 = np.cos(60 * np.pi / 180.0)
        inPts = np.copy(input).tolist()
        outPts = np.copy(output).tolist()
        xin = c60 * (inPts[0][0] - inPts[1][0]) - s60 * (inPts[0][1] - inPts[1][1]) + inPts[1][0]
        yin = s60 * (inPts[0][0] - inPts[1][0]) - c60 * (inPts[0][1] - inPts[1][1]) + inPts[1][1]
        inPts.append([np.int(xin), np.int(yin)])
        xout = c60 * (outPts[0][0] - outPts[1][0]) - s60 * (outPts[0][1] - outPts[1][1]) + outPts[1][0]
        yout = s60 * (outPts[0][0] - outPts[1][0]) - c60 * (outPts[0][1] - outPts[1][1]) + outPts[1][1]
        outPts.append([np.int(xout), np.int(yout)])
        return cv2.estimateRigidTransform(np.array([inPts], dtype=np.int32), 
                                          np.array([outPts], dtype=np.int32), True) 
Example #6
Source File: face_align_util.py    From MaskInsightface with Apache License 2.0 5 votes vote down vote up
def align_dlib_cpp(self, rgbImg, landmarks=None):
        '''
        @brief: 与dlib C++版本实现的裁剪对齐方法一致。
        @attention
        '''
        assert rgbImg is not None

        npLandmarks = np.array(landmarks)[:, :2]
        shape_x = [npLandmarks[i][0] for i in range(68)]
        shape_y = [npLandmarks[i][1] for i in range(68)]
        from_points = []
        to_points = []
        for i in range(17, 68):
            # 忽略掉低于嘴唇的部分
            if i >= 55 and i <= 59:
                continue
            # 忽略眉毛部分
            if i >= 17 and i <= 26:
                continue
            # 上下左右都padding
            new_ref_x = (self.padding + self.mean_shape_x[i - 17]) / (2 * self.padding + 1)
            new_ref_y = (self.padding + self.mean_shape_y[i - 17]) / (2 * self.padding + 1)

            from_points.append((shape_x[i], shape_y[i]))
            to_points.append((self.image_size * new_ref_x, self.image_size * new_ref_y))

        source = np.array(from_points).astype(np.int)
        target = np.array(to_points, ).astype(np.int)
        source = np.reshape(source, (1, 36, 2))
        target = np.reshape(target, (1, 36, 2))
        H = cv2.estimateRigidTransform(source, target, False)
        if H is None:
            return None
        else:
            aligned_face = cv2.warpAffine(rgbImg, H, (self.image_size, self.image_size))
            return aligned_face 
Example #7
Source File: Placer.py    From ImageAnalysis with MIT License 5 votes vote down vote up
def findAffine(self, i1, i2, pairs, fullAffine=False):
        src = []
        dst = []
        for pair in pairs:
            c1 = i1.coord_list[pair[0]]
            c2 = i2.coord_list[pair[1]]
            src.append( c1 )
            dst.append( c2 )
        #print "src = %s" % str(src)
        #print "dst = %s" % str(dst)
        affine = cv2.estimateRigidTransform(np.array([src]).astype(np.float32),
                                            np.array([dst]).astype(np.float32),
                                            fullAffine)
        #print str(affine)
        return affine 
Example #8
Source File: Placer.py    From ImageAnalysis with MIT License 5 votes vote down vote up
def findGroupAffine(self, i1, fullAffine=False):
        # find the affine transform matrix representing the best fit
        # against all the placed neighbors.  Builds a cumulative
        # src/dest list with our src points listed once for each image
        # pair.

        src = []
        dst = []
        for i, pairs in enumerate(i1.match_list):
            if len(pairs) < 3:
                # can't compute affine transform on < 3 points
                continue
            i2 = self.image_list[i]
            if not i2.placed:
                # don't consider non-yet-placed neighbors
                continue
            # add coordinate matches for this image pair
            for pair in pairs:
                c1 = i1.coord_list[pair[0]]
                c2 = i2.coord_list[pair[1]]
                src.append( c1 )
                dst.append( c2 )

        if len(src) < 3:
            # not enough points to compute affine transformation
            return np.array( [ [1.0, 0.0, 0.0 ], [0.0, 1.0, 0.0] ] )

        # find the affine matrix on the communlative set of all
        # matching coordinates for all matching image pairs
        # simultaneously...
        affine = cv2.estimateRigidTransform(np.array([src]).astype(np.float32),
                                            np.array([dst]).astype(np.float32),
                                            fullAffine)
        if affine == None:
            # it's possible given a degenerate point set, the affine
            # estimator will return None, so return the identity
            affine = np.array( [ [1.0, 0.0, 0.0 ], [0.0, 1.0, 0.0] ] )
        return affine
    
    # compare against best 'placed' image (averaging transform
    # matrices together directly doesn't do what we want) 
Example #9
Source File: 1a-est-gyro-rates.py    From ImageAnalysis with MIT License 5 votes vote down vote up
def findAffine(src, dst, fullAffine=False):
    #print("src:", src)
    #print("dst:", dst)
    if len(src) >= affine_minpts:
        # affine = cv2.estimateRigidTransform(np.array([src]), np.array([dst]), fullAffine)
        affine, status = \
            cv2.estimateAffinePartial2D(np.array([src]).astype(np.float32),
                                        np.array([dst]).astype(np.float32))
    else:
        affine = None
    #print str(affine)
    return affine 
Example #10
Source File: 1a-est-gyro-rates.py    From ImageAnalysis with MIT License 5 votes vote down vote up
def findAffine(src, dst, fullAffine=False):
    #print "src = %s" % str(src)
    #print "dst = %s" % str(dst)
    if len(src) >= affine_minpts:
        affine = cv2.estimateRigidTransform(np.array([src]), np.array([dst]),
                                            fullAffine)
    else:
        affine = None
    #print str(affine)
    return affine 
Example #11
Source File: 1b-est-gyro-rates.py    From ImageAnalysis with MIT License 5 votes vote down vote up
def findAffine(src, dst, fullAffine=False):
    #print "src = %s" % str(src)
    #print "dst = %s" % str(dst)
    if len(src) >= affine_minpts:
        affine = cv2.estimateRigidTransform(np.array([src]), np.array([dst]),
                                            fullAffine)
    else:
        affine = None
    #print str(affine)
    return affine 
Example #12
Source File: __init__.py    From sima with GNU General Public License v2.0 4 votes vote down vote up
def estimate_array_transform(source, target, method='affine'):
    """Calculate an affine transformation from source array to target array.

    Parameters
    ----------
    source : array
        The image to transform
    target : array
        The image used as the template for the transformation
    method : string, optional
        Method to use for transform estimation.

    Returns
    -------
    transform : skimage.transform._geometric.GeometricTransform
        An skimage transform object.

    See Also
    --------
    cv2.estimateRigidTransform
    skimage.transform

    """

    if method == 'affine':
        if not cv2_available:
            raise ImportError('OpenCV >= 2.4.8 required')

        slice_ = tuple(slice(0, min(source.shape[i], target.shape[i]))
                       for i in range(2))
        transform = cv2.estimateRigidTransform(
            to8bit(source[slice_]),
            to8bit(target[slice_]), True)

        if transform is None:
            raise TransformError('Cannot calculate affine transformation ' +
                                 'from source to target')
        else:
            # TODO: make sure the order is correct
            transform_matrix = np.vstack((transform, [0, 0, 1]))
            return tf.AffineTransform(matrix=transform_matrix)
    else:
        raise ValueError('Unrecognized transform method: {}'.format(method)) 
Example #13
Source File: coregistration.py    From eo-learn with MIT License 4 votes vote down vote up
def register(self, src, trg, trg_mask=None, src_mask=None):
        """ Implementation of pair-wise registration and warping using point-based matching

        This function estimates a number of transforms (Euler, PartialAffine and Homography) using point-based matching.
        Features descriptor are first extracted from the pair of images using either SIFT or SURF descriptors. A
        brute-force point-matching algorithm estimates matching points and a transformation is computed. All
        transformations use RANSAC to robustly fit a tranform to the matching points. However, the feature extraction
        and point matching estimation can be very poor and unstable. In those cases, an identity transform is used
        to warp the images instead.

        :param src: 2D single channel source moving image
        :param trg: 2D single channel target reference image
        :param trg_mask: Mask of target image. Not used in this method.
        :param src_mask: Mask of source image. Not used in this method.
        :return: Estimated 2D transformation matrix of shape 2x3
        """
        # Initialise matrix and failed registrations flag
        warp_matrix = None
        # Initiate point detector
        ptdt = cv2.xfeatures2d.SIFT_create() if self.params['Descriptor'] == 'SIFT' else cv2.xfeatures2d.SURF_create()
        # create BFMatcher object
        bf_matcher = cv2.BFMatcher(cv2.NORM_L1, crossCheck=True)
        # find the keypoints and descriptors with SIFT
        kp1, des1 = ptdt.detectAndCompute(self.rescale_image(src), None)
        kp2, des2 = ptdt.detectAndCompute(self.rescale_image(trg), None)
        # Match descriptors if any are found
        if des1 is not None and des2 is not None:
            matches = bf_matcher.match(des1, des2)
            # Sort them in the order of their distance.
            matches = sorted(matches, key=lambda x: x.distance)
            src_pts = np.asarray([kp1[m.queryIdx].pt for m in matches], dtype=np.float32).reshape(-1, 2)
            trg_pts = np.asarray([kp2[m.trainIdx].pt for m in matches], dtype=np.float32).reshape(-1, 2)
            # Parse model and estimate matrix
            if self.params['Model'] == 'PartialAffine':
                warp_matrix = cv2.estimateRigidTransform(src_pts, trg_pts, fullAffine=False)
            elif self.params['Model'] == 'Euler':
                model = EstimateEulerTransformModel(src_pts, trg_pts)
                warp_matrix = ransac(src_pts.shape[0], model, 3, self.params['MaxIters'], 1, 5)
            elif self.params['Model'] == 'Homography':
                warp_matrix, _ = cv2.findHomography(src_pts, trg_pts, cv2.RANSAC,
                                                    ransacReprojThreshold=self.params['RANSACThreshold'],
                                                    maxIters=self.params['MaxIters'])
                if warp_matrix is not None:
                    warp_matrix = warp_matrix[:2, :]
        return warp_matrix