opencv 对齐两幅图片

Posted MrCharles

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv 对齐两幅图片相关的知识,希望对你有一定的参考价值。

https://learnopencv.com/feature-based-image-alignment-using-opencv-c-python/

def alignImages(im1, im2):

  # Convert images to grayscale
  im1Gray = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)
  im2Gray = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)
  
  # Detect ORB features and compute descriptors.
  orb = cv2.ORB_create(MAX_MATCHES)
  keypoints1, descriptors1 = orb.detectAndCompute(im1Gray, None)
  keypoints2, descriptors2 = orb.detectAndCompute(im2Gray, None)
  
  # Match features.
  matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
  matches = matcher.match(descriptors1, descriptors2, None)
  
  # Sort matches by score
  matches.sort(key=lambda x: x.distance, reverse=False)

  # Remove not so good matches
  numGoodMatches = int(len(matches) * GOOD_MATCH_PERCENT)
  matches = matches[:numGoodMatches]

  # Draw top matches
  imMatches = cv2.drawMatches(im1, keypoints1, im2, keypoints2, matches, None)
  cv2.imwrite("matches.jpg", imMatches)
  
  # Extract location of good matches
  points1 = np.zeros((len(matches), 2), dtype=np.float32)
  points2 = np.zeros((len(matches), 2), dtype=np.float32)

  for i, match in enumerate(matches):
    points1[i, :] = keypoints1[match.queryIdx].pt
    points2[i, :] = keypoints2[match.trainIdx].pt
  
  # Find homography
  h, mask = cv2.findHomography(points1, points2, cv2.RANSAC)

  # Use homography
  height, width, channels = im2.shape
  im1Reg = cv2.warpPerspective(im1, h, (width, height))
  
  return im1Reg, h

As mentioned earlier, a homography is nothing but a 3 × 3 3 \\times 3 3×3 matrix as shown below.
H = [ h 00 h 01 h 02 h 10 h 11 h 12 h 20 h 21 h 22 ] H=\\left[\\beginarraylll h_00 & h_01 & h_02 \\\\ h_10 & h_11 & h_12 \\\\ h_20 & h_21 & h_22 \\endarray\\right] H=h00h10h20h01h11h21h02h12h22
Let ( x 1 , y 1 ) \\left(x_1, y_1\\right) (x1,y1) be a point in the first image and ( x 2 , y 2 ) \\left(x_2, y_2\\right) (x2,y2) be the coordinates of the same physical point in the second image. Then, the Homography H H H relates them in the following way
[ x 1 y 1 1 ] = H [ x 2 y 2 1 ] = [ h 00 h 01 h 02 h 10 h 11 h 12 h 20 h 21 h 22 ] [ x 2 y 2 1 ] \\left[\\beginarrayc x_1 \\\\ y_1 \\\\ 1 \\endarray\\right]=H\\left[\\beginarrayc x_2 \\\\ y_2 \\\\ 1 \\endarray\\right]=\\left[\\beginarraylll h_00 & h_01 & h_02 \\\\ h_10 & h_11 & h_12 \\\\ h_20 & h_21 & h_22 \\endarray\\right]\\left[\\beginarrayc x_2 \\\\ y_2 \\\\ 1 \\endarray\\right] x1y11=Hx2y21=h00h10h20h01h11h21h02h12h22x2y21
If we knew the homography, we could apply it to all the pixels of one image to obtain a warped image that is aligned with the second image.

以上是关于opencv 对齐两幅图片的主要内容,如果未能解决你的问题,请参考以下文章

opencv如何将两幅图片拼接

OpenCV实践- 叠加两幅图片

qt+opencv对两幅图片进行融合

opencv如何计算两幅图像的相似度

opencv 模板匹配

openCV 特征点识别 与findHomography算法过滤