当前位置: 首页>>代码示例>>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;未经允许,请勿转载。