OpenCV-Python ORB-Funktionsabgleich

Der erste Schritt: Bibliothek, Bild importieren, ORB-Objekt erstellen

Importieren Sie die OpenCV-Bibliothek.

import cv2 as cv

Bevor wir Features extrahieren, müssen wir zwei Bilder einlesen (ich werde keine Bilder bereitstellen).

image1 = cv.imread('1.png')
image2 = cv.imread('2.png')

Hier verwenden wir die ORB-Funktion, also erstellen Sie ein ORB-Objekt: 

# 初始化ORB
orb = cv.ORB_create()

Schritt 2: Finden Sie wichtige Punkte und Deskriptoren

Nach der Erstellung geht es hauptsächlich darum, Schlüsselpunkte zu finden und Deskriptoren zu berechnen:

 # 寻找关键点
 kp1 = orb.detect(img1)
 kp2 = orb.detect(img2)

 # 计算描述符
 kp1, des1 = orb.compute(img1, kp1) # 计算哪张图片的用哪张图片的关键点。
 kp2, des2 = orb.compute(img2, kp2)

Nachdem Sie die Schlüsselpunkte gefunden haben, können Sie die Schlüsselpunkte zeichnen und einen Blick darauf werfen:

# 画出关键点
outimg1 = cv.drawKeypoints(img1, keypoints=kp1, outImage=None)
outimg2 = cv.drawKeypoints(img2, keypoints=kp2, outImage=None)

# 这里是把两张图片在同一个窗口中显示。
import numpy as np
outimg3 = np.hstack([outimg1, outimg2])
cv.imshow("Key Points", outimg3)
cv.waitKey(0)

Schritt drei: Machen Sie eine Übereinstimmung

# 初始化 BFMatcher
 bf = cv.BFMatcher(cv.NORM_HAMMING)

 # 对描述子进行匹配
 matches = bf.match(des1, des2)

Schritt 4: Filtern Sie die passenden Punkte

# 计算最大距离和最小距离
min_distance = matches[0].distance
max_distance = matches[0].distance
for x in matches:
    if x.distance < min_distance:
        min_distance = x.distance
    if x.distance > max_distance:
        max_distance = x.distance
       
'''
    当描述子之间的距离大于两倍的最小距离时,认为匹配有误。
    但有时候最小距离会非常小,所以设置一个经验值30作为下限。
'''
 good_match = []
 for x in matches:
     if x.distance <= max(2 * min_distance, 30):
         good_match.append(x)

Schritt 5: Zeichnen Sie das Ergebnis grafisch auf

Hier zeichnen wir die Ergebnisse separat aus:

    draw_match(img1, img2, kp1, kp2, good_match)


def draw_match(img1, img2, kp1, kp2, match):
    outimage = cv.drawMatches(img1, kp1, img2, kp2, match, outImg=None)
    cv.imshow("Match Result", outimage)
    cv.waitKey(0)

Vollständiger Code

import cv2 as cv

def ORB_Feature(img1, img2):

    # 初始化ORB
    orb = cv.ORB_create()

    # 寻找关键点
    kp1 = orb.detect(img1)
    kp2 = orb.detect(img2)

    # 计算描述符
    kp1, des1 = orb.compute(img1, kp1)
    kp2, des2 = orb.compute(img2, kp2)

    # 画出关键点
    outimg1 = cv.drawKeypoints(img1, keypoints=kp1, outImage=None)
    outimg2 = cv.drawKeypoints(img2, keypoints=kp2, outImage=None)
   
    # 显示关键点
    # import numpy as np
    # outimg3 = np.hstack([outimg1, outimg2])
    # cv.imshow("Key Points", outimg3)
    # cv.waitKey(0)

    # 初始化 BFMatcher
    bf = cv.BFMatcher(cv.NORM_HAMMING)

    # 对描述子进行匹配
    matches = bf.match(des1, des2)

    # 计算最大距离和最小距离
    min_distance = matches[0].distance
    max_distance = matches[0].distance
    for x in matches:
        if x.distance < min_distance:
            min_distance = x.distance
        if x.distance > max_distance:
            max_distance = x.distance

    # 筛选匹配点
    '''
        当描述子之间的距离大于两倍的最小距离时,认为匹配有误。
        但有时候最小距离会非常小,所以设置一个经验值30作为下限。
    '''
    good_match = []
    for x in matches:
        if x.distance <= max(2 * min_distance, 30):
            good_match.append(x)

    # 绘制匹配结果
    draw_match(img1, img2, kp1, kp2, good_match)

def draw_match(img1, img2, kp1, kp2, match):
    outimage = cv.drawMatches(img1, kp1, img2, kp2, match, outImg=None)
    cv.imshow("Match Result", outimage)
    cv.waitKey(0)

if __name__ == '__main__':
    # 读取图片
    image1 = cv.imread('1.png')
    image2 = cv.imread('2.png')
    ORB_Feature(image1, image2)

おすすめ

転載: blog.csdn.net/u013590327/article/details/126592894