Python OpenCV 角点检测和特征匹配:实现图像配准

本文将介绍如何使用 OpenCV 库在 Python 中实现图像角点检测和特征匹配。我们将使用 Harris 角点检测器来检测图像中的角点,并使用归一化互相关方法来匹配两幅图像中的对应角点。

1. 导入必要的库

首先,我们需要导入必要的库:

import cv2
import numpy as np
from matplotlib import pyplot as plt

2. 加载图像

接下来,我们需要加载要进行匹配的两幅图像:

img1 = cv2.imread('1.png')
img2 = cv2.imread('4.png')

3. 将图像转换为灰度图像

我们将图像转换为灰度图像,因为角点检测算法通常在灰度图像上执行得更好:

gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

4. 归一化像素值

在使用归一化互相关时,需要将像素值归一化到0~1范围内,而原始的灰度图像像素值范围为0~255。因此,我们需要将像素值除以255.0:

gray1 = np.float32(gray1) / 255.0
gray2 = np.float32(gray2) / 255.0

5. 使用 Harris 角点检测器检测角点

我们将使用 OpenCV 的 cv2.cornerHarris() 函数来检测图像中的角点。该函数接受以下参数:

  • gray: 灰度图像
  • blockSize: 用于计算梯度幅度的邻域大小
  • ksize: 用于计算 Sobel 导数的核大小
  • k: 用于计算 Harris 响应的常数
dst1 = cv2.cornerHarris(gray1, 2, 3, 0.04)
dst2 = cv2.cornerHarris(gray2, 2, 23, 0.04)

6. 提取角点坐标

我们将使用 get_harris_points() 函数来提取角点坐标。该函数接受以下参数:

  • harrisim: Harris 响应图像
  • min_dist: 分割角点和图像边界的最少像素数目
  • threshold: 用于过滤角点的阈值
# 从一幅Harris响应图像中返回角点,min_dist为分割角点和图像边界的最少像素数目
def get_harris_points(harrisim, min_dist=10, threshold=0.1):
    # 寻找高于阈值的候选角点
    corner_threshold = harrisim.max() * threshold
    harrisim_t = (harrisim > corner_threshold) * 1
    # 得到候选点的坐标
    coords = np.array(harrisim_t.nonzero()).T
    # 以及它们的 Harris 响应值
    candidate_values = [harrisim[c[0], c[1]] for c in coords]
    # 对候选点按照 Harris 响应值进行排序
    index = np.argsort(candidate_values)[::-1]
    # 将可行点的位置保存到数组中
    allowed_locations = np.zeros(harrisim.shape)
    allowed_locations[min_dist:-min_dist, min_dist:-min_dist] = 1
    # 按照 min_distance 原则,选择最佳 Harris 点
    filtered_coords = []
    for i in index:
        if allowed_locations[coords[i, 0], coords[i, 1]] == 1:
            filtered_coords.append(coords[i])
            allowed_locations[(coords[i, 0] - min_dist):(coords[i, 0] + min_dist),
                        (coords[i, 1] - min_dist):(coords[i, 1] + min_dist)] = 0
    return filtered_coords

# 提取角点坐标
filtered_coords1 = get_harris_points(dst1, wid + 1, 0.1)
filtered_coords2 = get_harris_points(dst2, wid + 1, 0.1)

7. 提取角点描述子

我们将使用 get_descriptors() 函数来提取角点描述子。该函数接受以下参数:

  • image: 图像
  • filtered_coords: 角点坐标
  • wid: 描述子窗口大小
# 对于每个返回的点,返回点周围2*wid+1个像素的值(假设选取点的min_distance > wid)
def get_descriptors(image, filtered_coords, wid=5):
    desc = []
    for coords in filtered_coords:
        patch = image[coords[0] - wid:coords[0] + wid + 1,
                coords[1] - wid:coords[1] + wid + 1].flatten()
        desc.append(patch)
    return desc

# 提取角点描述子
d1 = get_descriptors(img1, filtered_coords1, wid)
d2 = get_descriptors(img2, filtered_coords2, wid)

8. 使用归一化互相关匹配角点

我们将使用 match_twosided() 函数来匹配角点。该函数接受以下参数:

  • desc1: 第一幅图像的角点描述子
  • desc2: 第二幅图像的角点描述子
  • threshold: 用于过滤匹配的阈值
# 对于第一幅图像中的每个角点描述子,使用归一化互相关,选取它在第二幅图像中的匹配角点
def match(desc1, desc2, threshold=0.5):
    n = len(desc1[0])
    # 点对的距离
    d = -np.ones((len(desc1), len(desc2)))
    for i in range(len(desc1)):
        for j in range(len(desc2)):
            d1 = (desc1[i] - np.mean(desc1[i])) / np.std(desc1[i])
            d2 = (desc2[j] - np.mean(desc2[j])) / np.std(desc2[j])
            ncc_value = sum(d1 * d2) / (n - 1)
            if ncc_value > threshold:
                d[i, j] = ncc_value
    ndx = np.argsort(-d)   # 从大到小排序
    matchscores = ndx[:, 0]   # 最大一个数的位置坐标
    return matchscores

# 两边对称版本的match()
def match_twosided(desc1, desc2, threshold=0.5):
    matches_12 = match(desc1, desc2, threshold)
    matches_21 = match(desc2, desc1, threshold)
    ndx_12 = np.where(matches_12 >= 0)[0]
    # 去除非对称的匹配
    for n in ndx_12:
        if matches_21[matches_12[n]] != n:
            matches_12[n] = -1
    return matches_12

# 匹配角点
matches = match_twosided(d1, d2, 0.8)

9. 显示匹配结果

我们将使用 plot_matches() 函数来显示匹配结果。该函数接受以下参数:

  • img1: 第一幅图像
  • img2: 第二幅图像
  • locs1: 第一幅图像的角点坐标
  • locs2: 第二幅图像的角点坐标
  • matchscores: 匹配结果
  • show_below: 是否将匹配结果显示在图像下方
# 返回将两幅图像并排拼接成的一幅新图像
def appendimages(im1, im2):
    row1 = im1.shape[0]
    row2 = im2.shape[0]
    if row1 < row2:
        im1 = np.concatenate((im1, np.zeros((row2 - row1, im1.shape[1], im1.shape[2]))), axis=0)
    elif row1 > row2:
        im2 = np.concatenate((im2, np.zeros((row1 - row2, im2.shape[1], im2.shape[2]))), axis=0)
    return np.concatenate((im1, im2), axis=1)


# 显示一幅带有连接匹配之间连线的图片
# 输入:im1,im2(数组图像),locs1,locs2(特征位置),matchscores(match的输出),
def plot_matches(img1, img2, locs1, locs2, matchscores, show_below=False):
    img3 = appendimages(img1, img2)
    if show_below:
        img3 = np.vstack((img3, img3))

    plt.imshow(img3)

    cols1 = img1.shape[1]
    for i, m in enumerate(matchscores):
        if m > 0:
            plt.plot([locs1[i][1], locs2[m][1] + cols1], [locs1[i][0], locs2[m][0]], 'c')
    plt.axis('off')

# 显示匹配结果
plt.figure(figsize=(30, 20))
plot_matches(img1, img2, filtered_coords1, filtered_coords2, matches)
plt.show()

代码示例

import cv2
import numpy as np
from matplotlib import pyplot as plt

img1 = cv2.imread('1.png')
img2 = cv2.imread('4.png')

gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
gray1 = np.float32(gray1) / 255.0
gray2 = np.float32(gray2) / 255.0
dst1 = cv2.cornerHarris(gray1, 2, 3, 0.04)
dst2 = cv2.cornerHarris(gray2, 2, 23, 0.04)

# 从一幅Harris响应图像中返回角点,min_dist为分割角点和图像边界的最少像素数目
def get_harris_points(harrisim, min_dist=10, threshold=0.1):
    # 寻找高于阈值的候选角点
    corner_threshold = harrisim.max() * threshold
    harrisim_t = (harrisim > corner_threshold) * 1
    # 得到候选点的坐标
    coords = np.array(harrisim_t.nonzero()).T
    # 以及它们的 Harris 响应值
    candidate_values = [harrisim[c[0], c[1]] for c in coords]
    # 对候选点按照 Harris 响应值进行排序
    index = np.argsort(candidate_values)[::-1]
    # 将可行点的位置保存到数组中
    allowed_locations = np.zeros(harrisim.shape)
    allowed_locations[min_dist:-min_dist, min_dist:-min_dist] = 1
    # 按照 min_distance 原则,选择最佳 Harris 点
    filtered_coords = []
    for i in index:
        if allowed_locations[coords[i, 0], coords[i, 1]] == 1:
            filtered_coords.append(coords[i])
            allowed_locations[(coords[i, 0] - min_dist):(coords[i, 0] + min_dist),
                        (coords[i, 1] - min_dist):(coords[i, 1] + min_dist)] = 0
    return filtered_coords

# 对于每个返回的点,返回点周围2*wid+1个像素的值(假设选取点的min_distance > wid)
def get_descriptors(image, filtered_coords, wid=5):
    desc = []
    for coords in filtered_coords:
        patch = image[coords[0] - wid:coords[0] + wid + 1,
                coords[1] - wid:coords[1] + wid + 1].flatten()
        desc.append(patch)
    return desc

# 对于第一幅图像中的每个角点描述子,使用归一化互相关,选取它在第二幅图像中的匹配角点
def match(desc1, desc2, threshold=0.5):
    n = len(desc1[0])
    # 点对的距离
    d = -np.ones((len(desc1), len(desc2)))
    for i in range(len(desc1)):
        for j in range(len(desc2)):
            d1 = (desc1[i] - np.mean(desc1[i])) / np.std(desc1[i])
            d2 = (desc2[j] - np.mean(desc2[j])) / np.std(desc2[j])
            ncc_value = sum(d1 * d2) / (n - 1)
            if ncc_value > threshold:
                d[i, j] = ncc_value
    ndx = np.argsort(-d)   # 从大到小排序
    matchscores = ndx[:, 0]   # 最大一个数的位置坐标
    return matchscores

# 两边对称版本的match()
def match_twosided(desc1, desc2, threshold=0.5):
    matches_12 = match(desc1, desc2, threshold)
    matches_21 = match(desc2, desc1, threshold)
    ndx_12 = np.where(matches_12 >= 0)[0]
    # 去除非对称的匹配
    for n in ndx_12:
        if matches_21[matches_12[n]] != n:
            matches_12[n] = -1
    return matches_12

# 返回将两幅图像并排拼接成的一幅新图像
def appendimages(im1, im2):
    row1 = im1.shape[0]
    row2 = im2.shape[0]
    if row1 < row2:
        im1 = np.concatenate((im1, np.zeros((row2 - row1, im1.shape[1], im1.shape[2]))), axis=0)
    elif row1 > row2:
        im2 = np.concatenate((im2, np.zeros((row1 - row2, im2.shape[1], im2.shape[2]))), axis=0)
    return np.concatenate((im1, im2), axis=1)


# 显示一幅带有连接匹配之间连线的图片
# 输入:im1,im2(数组图像),locs1,locs2(特征位置),matchscores(match的输出),
def plot_matches(img1, img2, locs1, locs2, matchscores, show_below=False):
    img3 = appendimages(img1, img2)
    if show_below:
        img3 = np.vstack((img3, img3))

    plt.imshow(img3)

    cols1 = img1.shape[1]
    for i, m in enumerate(matchscores):
        if m > 0:
            plt.plot([locs1[i][1], locs2[m][1] + cols1], [locs1[i][0], locs2[m][0]], 'c')
    plt.axis('off')

wid = 9  # 比较像素点数目
filtered_coords1 = get_harris_points(dst1, wid + 1, 0.1)  # 图1大于阈值的坐标
filtered_coords2 = get_harris_points(dst2, wid + 1, 0.1)  # 图2大于阈值的坐标
d1 = get_descriptors(img1, filtered_coords1, wid)
d2 = get_descriptors(img2, filtered_coords2, wid)
matches = match_twosided(d1, d2, 0.8)  # 图1的阈值点与图二哪个阈值点相关度最高,输出与图一相关性最大点的坐标
plt.figure(figsize=(30, 20))
plot_matches(img1, img2, filtered_coords1, filtered_coords2, matches)
plt.show()

总结

本文介绍了如何使用 OpenCV 库在 Python 中实现图像角点检测和特征匹配。我们将使用 Harris 角点检测器来检测图像中的角点,并使用归一化互相关方法来匹配两幅图像中的对应角点。该方法可以用于图像配准、目标识别等应用。

Python OpenCV 角点检测和特征匹配:实现图像配准

原文地址: https://www.cveoy.top/t/topic/kntq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录