基于特征的图像对齐(SIFT或ORB)

一. 效果图

什么是图像对齐或者图像配准?
图像对齐(或者图像配准)可以扭曲旋转(其实是仿射变换)一张图使它和另一个图可以很完美的对齐。
下图为SIFT特征匹配的图。
在这里插入图片描述
下图为上面左边图矫正的结果。
在这里插入图片描述

二. 基于特征的图像对齐步骤

下面以ORB特征为例说明,SIFT特征的步骤与ORB几乎相同。

1. 读取图片到内存

2. 检测特征点

为两张图检测ORB特征点。为了计算单应性矩阵4个就够了,但是一般会检测到成百上千的特征点。可以使用参数MAX_FEATURES 来控制检测的特征点的数量。检测特征点并计算描述子的函数是detectAndCompute。

3. 特征匹配

找到两图中匹配的特征点,并按照匹配度排列,保留最匹配的一小部分。然后把匹配的特征点画出来并保存图片到磁盘。我们使用汉明距离来度量两个特征点描述子的相似度。下图中把匹配的特征点用线连起来了。注意,有很多错误匹配的特征点,所以下一步要用一个健壮的算法来计算单应性矩阵。

4. 计算单应性矩阵

上一步产生的匹配的特征点不是100%正确的,就算只有20~30%的匹配是正确的也不罕见。findHomography 函数使用一种被称为随机抽样一致算法(Random Sample Consensus )的技术在大量匹配错误的情况下计算单应性矩阵。

5. 扭转图片(Warping image)

有了精确的单应性矩阵,就可以把一张图片的所有像素映射到另一个图片。warpPerspective 函数用来完成这个功能。

三. 代码实现

from __future__ import print_function
import cv2
import numpy as np


MAX_FEATURES = 500
GOOD_MATCH_PERCENT = 0.15


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_FEATURES)
  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



if __name__ == '__main__':

  # Read reference image
  refFilename = "img1.png"
  print("Reading reference image : ", refFilename)
  imReference = cv2.imread(refFilename, cv2.IMREAD_COLOR)

  # Read image to be aligned
  imFilename = "img2.png"
  print("Reading image to align : ", imFilename);  
  im = cv2.imread(imFilename, cv2.IMREAD_COLOR)

  print("Aligning images ...")
  # Registered image will be resotred in imReg. 
  # The estimated homography will be stored in h. 
  imReg, h = alignImages(im, imReference)

  # Write aligned image to disk. 
  outFilename = "aligned.jpg"
  print("Saving aligned image : ", outFilename); 
  cv2.imwrite(outFilename, imReg)

  # Print estimated homography
  print("Estimated homography : \n",  h)

每天进步一点,加油!!!
欢迎技术交流,如果想用SIFT特征实现,请联系作者。
在这里插入图片描述

### 图像对齐技术概述 图像对齐是指将两幅多幅不同视角、时间传感器获取的图像进行空间上的校正,使得它们能够在相同的坐标系下表示。这一过程通常涉及以下几个主要步骤: - **特征提取**:从源图像和目标图像中提取具有代表性的特征点。常用的特征检测算子有SIFT、SURF、ORB等[^1]。 - **特征匹配**:利用描述符来衡量两个特征之间的相似度,并建立对应关系。这一步骤可以通过暴力匹配者FLANN(Fast Library for Approximate Nearest Neighbors)等方式完成。 - **几何变换估计**:根据匹配好的特征点集计算出两者间的转换矩阵,常见的形式包括平移、旋转、缩放以及仿射变换等。RANSAC算法常被用来排除错误匹配并求解最优参数。 - **重采样与插值**:应用上述得到的变换矩阵调整输入图片的位置,使其与其他图像相吻合;对于新位置处不存在原始像素的情况,则需采用双线性插值其他方法填补空白区域。 #### 实现案例分析 为了更好地理解这些概念,在文献中有这样一个实例说明了如何使用MATLAB实现基本的图像配准流程。此例子不仅涵盖了前面提到的关键环节——即特征提取、匹配及变换估算,而且还提供了具体的编程指导和支持材料供学习者参考实践。 ```matlab % 假设Iref 是参照图像, Iinput是要注册的图像 fixedPoints = detectSURFFeatures(Iref); movingPoints = detectSURFFFeatures(Iinput); [fFixed, fMoving] = extractFeatures(Iref, fixedPoints), ... extractFeatures(Iinput, movingPoints); indexPairs = matchFeatures(fFixed, fMoving); matchedPairIndices = indexPairs(:, [2 1]); tform = estimateGeometricTransform(matchedPairIndices.Location, 'affine'); outputView = imref2d(size(Iref)); IR_registered = imwarp(Iinput,tform,'OutputView', outputView); ``` 这段代码片段展示了如何运用SURF特征来进行初步的图像配准操作。通过`detectSURFFeatures()`函数识别关键点,再借助`extractFeatures()`获得对应的局部描述向量,最后依靠`estimateGeometricTransform()`得出最佳拟合模型用于最终映射。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值