當前位置: 首頁>>代碼示例>>Python>>正文


Python cv2.estimateRigidTransform方法代碼示例

本文整理匯總了Python中cv2.estimateRigidTransform方法的典型用法代碼示例。如果您正苦於以下問題:Python cv2.estimateRigidTransform方法的具體用法?Python cv2.estimateRigidTransform怎麽用?Python cv2.estimateRigidTransform使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在cv2的用法示例。


在下文中一共展示了cv2.estimateRigidTransform方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: estimate_partial_transform

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import estimateRigidTransform [as 別名]
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] 
開發者ID:AdamSpannbauer,項目名稱:python_video_stab,代碼行數:24,代碼來源:vidstab_utils.py

示例2: similarityTransform

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import estimateRigidTransform [as 別名]
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 
開發者ID:chaofengc,項目名稱:Face-Sketch-Wild,代碼行數:27,代碼來源:face_rectify.py

示例3: imRigidTransform

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import estimateRigidTransform [as 別名]
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 
開發者ID:Azure-Samples,項目名稱:MachineLearningSamples-ImageClassificationUsingCntk,代碼行數:10,代碼來源:utilities_CVbasic_v2.py

示例4: cv2_estimateRigidTransform

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import estimateRigidTransform [as 別名]
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 
開發者ID:AdamSpannbauer,項目名稱:python_video_stab,代碼行數:14,代碼來源:cv2_utils.py

示例5: similarityTransform

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import estimateRigidTransform [as 別名]
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) 
開發者ID:yhswjtuILMARE,項目名稱:Machine-Learning-Study-Notes,代碼行數:15,代碼來源:AverageFace.py

示例6: align_dlib_cpp

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import estimateRigidTransform [as 別名]
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 
開發者ID:bleakie,項目名稱:MaskInsightface,代碼行數:38,代碼來源:face_align_util.py

示例7: findAffine

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import estimateRigidTransform [as 別名]
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 
開發者ID:UASLab,項目名稱:ImageAnalysis,代碼行數:17,代碼來源:Placer.py

示例8: findGroupAffine

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import estimateRigidTransform [as 別名]
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) 
開發者ID:UASLab,項目名稱:ImageAnalysis,代碼行數:43,代碼來源:Placer.py

示例9: findAffine

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import estimateRigidTransform [as 別名]
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 
開發者ID:UASLab,項目名稱:ImageAnalysis,代碼行數:14,代碼來源:1a-est-gyro-rates.py

示例10: findAffine

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import estimateRigidTransform [as 別名]
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 
開發者ID:UASLab,項目名稱:ImageAnalysis,代碼行數:12,代碼來源:1a-est-gyro-rates.py

示例11: estimate_array_transform

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import estimateRigidTransform [as 別名]
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)) 
開發者ID:losonczylab,項目名稱:sima,代碼行數:45,代碼來源:__init__.py

示例12: register

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import estimateRigidTransform [as 別名]
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 
開發者ID:sentinel-hub,項目名稱:eo-learn,代碼行數:47,代碼來源:coregistration.py


注:本文中的cv2.estimateRigidTransform方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。