user3363298
user3363298

Reputation: 211

Align images in opencv

I have images of same scene focused at different distances. I want to align the images such that every feature is in exactly same position in every image. How can i do this using opencv?

Upvotes: 21

Views: 38521

Answers (4)

Knok
Knok

Reputation: 1

def AlignImage(reference, input_image):
    # Gray scale
    ref_gray = cv2.cvtColor(reference, cv2.COLOR_BGR2GRAY)
    input_gray = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)

    # SIFT
    sift = cv2.SIFT_create()
    kp1, des1 = sift.detectAndCompute(ref_gray, None)
    kp2, des2 = sift.detectAndCompute(input_gray, None)

    matcher = cv2.BFMatcher(cv2.NORM_L2, crossCheck=True)
    matches = matcher.match(des1, des2)
    matches = sorted(matches, key=lambda x: x.distance)

    # Get key points
    points1 = np.float32([kp1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2)
    points2 = np.float32([kp2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2)

    # Affine matrix
    matrix, _ = cv2.estimateAffine2D(points2, points1, method=cv2.RANSAC)

    # Align
    aligned_image = cv2.warpAffine(input_image, matrix, (reference.shape[1], reference.shape[0]))
    return aligned_image

This is my code for image alignment. Easy to use but a bit slow :)

Upvotes: 0

Vlad
Vlad

Reputation: 4525

First you have determine the transformation between your images. It can be as simple as shift or as complex as non-linear warping. Focusing operation can not only shift but also slightly scale and even shear the images. In this case you have to use Homography to match them.

Second, a simplest way that you have to try is to manually select at least 4 paris of corresponding points, record their coordinates and feed them into findHomography() function. The resulting 3x3 transformation can be used in warpPerspective() to match the images. If the outcome is good you can automate the process of finding correspondences by using, say, SURF points and matching their descriptors.

Finally, if the result is unsatisfactory you have to have a more general transformation than homography. I would try a piece-wise Affine trying to match pieces of images separately. Anyway, more info about your input and a final goal will help to solve the problem properly.

Upvotes: 13

Olivier A
Olivier A

Reputation: 842

You can use image stitching or feature machting algorithms of openCV.

The main steps of feature matching are :

  • Extract features (SURF or SIFT or others...)
  • Match the features (FLANN or BruteForce...) and filter the matchings
  • Find the geometrical transformation (RANSAC or LMeds...)

Upvotes: 6

herohuyongtao
herohuyongtao

Reputation: 50717

The basic idea is to in-plane rotate the images and its 2D translation in X and Y directions to based on their matched feature points.

Check out Image Alignment Algorithms where you can find two approaches to do this using OpenCV (with code).

Upvotes: 5

Related Questions