Python cv2.estimateAffinePartial2D() Examples

The following are 5 code examples of cv2.estimateAffinePartial2D(). 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: image_utils.py    From TensorflowFramework with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def align_to_shape(image, labels, target, extend=1.0, rotate=False):
  image = np.array(image)
  labels = np.array(labels).astype(np.float32)
  target = np.array(target).astype(np.float32)
  m, _ = cv2.estimateAffinePartial2D(labels, target)
  image_t = cv2.warpAffine(image, m, (128, 128))
  labels_t = np.dot(labels, m[:,:2].T) + m[np.newaxis,:,2]
  return image_t, labels_t 
Example #2
Source File: distance_ransac_orb.py    From douglas-quaid with GNU General Public License v3.0 5 votes vote down vote up
def find_homography(keypoints_pic1, keypoints_pic2, matches) -> (List, np.float32, np.float32):
        # Find an Homography matrix between two pictures
        # From two list of keypoints and a list of matches, extrat
        # A list of good matches found by RANSAC and two transformation matrix (an homography and a rigid homography/affine)

        # Instanciate outputs
        good = []

        # Transforming keypoints to list of points
        src_pts = np.float32([keypoints_pic1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2)
        dst_pts = np.float32([keypoints_pic2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2)

        # Find the transformation between points
        transformation_matrix, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)

        # Compute a rigid transformation (without depth, only scale + rotation + translation)
        transformation_rigid_matrix, rigid_mask = cv2.estimateAffinePartial2D(src_pts, dst_pts)

        # Get a mask list for matches = A list that says "This match is an in/out-lier"
        matchesMask = mask.ravel().tolist()

        # Filter the matches list thanks to the mask
        for i, element in enumerate(matchesMask):
            if element == 1:
                good.append(matches[i])

        return good, transformation_matrix, transformation_rigid_matrix 
Example #3
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 #4
Source File: smart.py    From ImageAnalysis with MIT License 5 votes vote down vote up
def find_affine(i1, i2):
    # quick sanity checks
    if i1 == i2:
        return None
    if not i2.name in i1.match_list:
        return None
    if len(i1.match_list[i2.name]) == 0:
        return None

    if not i1.kp_list or not len(i1.kp_list):
        i1.load_features()
    if not i2.kp_list or not len(i2.kp_list):
        i2.load_features()

    # affine transformation from i2 uv coordinate system to i1
    uv1 = []; uv2 = []; indices = []
    for pair in i1.match_list[i2.name]:
        uv1.append( i1.kp_list[ pair[0] ].pt )
        uv2.append( i2.kp_list[ pair[1] ].pt )
    uv1 = np.float32([uv1])
    uv2 = np.float32([uv2])
    affine, status = \
        cv2.estimateAffinePartial2D(uv2, uv1)
    return affine

# return individual components of affine transform: rot, tx, ty, sx,
# sy (units are degrees and pixels) 
Example #5
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