Python: OpenCV findHomography входы

1

Я пытаюсь найти матрицу гомографии для двух изображений rgb и rotated с помощью opencv в Python:

print(rgb.shape, rotated.shape)
H = cv2.findHomography(rgb, rotated)
print(H)

И я получаю ошибку

(1080, 1920, 3) (1080, 1920, 3)
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-37-26874dc47f1f> in <module>()
      1 print(rgb.shape, rotated.shape)
----> 2 H = cv2.findHomography(rgb, rotated)
      3 print(H)

error: OpenCV(3.4.1) C:\projects\opencv-python\opencv\modules\calib3d\src\fundam.cpp:372: error: (-5) The input arrays should be 2D or 3D point sets in function cv::findHomography

Я также попытался использовать cv2.findHomography(rgb[:,:,0], rotated[:,:,0]) чтобы узнать, вызвали ли каналы или упорядочение каналов какие-либо проблемы, но не работают даже для 2D-матрицы.

Как должен быть вход?

Теги:
opencv
homography

1 ответ

1
Лучший ответ

cv2.findHomography() не принимает два изображения и возвращает H

Если вам нужно найти H для двух изображений RGB как np.arrays:

import numpy as np
import cv2

def findHomography(img1, img2):

    # define constants
    MIN_MATCH_COUNT = 10
    MIN_DIST_THRESHOLD = 0.7
    RANSAC_REPROJ_THRESHOLD = 5.0

    # Initiate SIFT detector
    sift = cv2.xfeatures2d.SIFT_create()

    # find the keypoints and descriptors with SIFT
    kp1, des1 = sift.detectAndCompute(img1, None)
    kp2, des2 = sift.detectAndCompute(img2, None)

    # find matches
    FLANN_INDEX_KDTREE = 1
    index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    search_params = dict(checks=50)

    flann = cv2.FlannBasedMatcher(index_params, search_params)
    matches = flann.knnMatch(des1, des2, k=2)

    # store all the good matches as per Lowe ratio test.
    good = []
    for m, n in matches:
        if m.distance < MIN_DIST_THRESHOLD * n.distance:
            good.append(m)


    if len(good) > MIN_MATCH_COUNT:
        src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
        dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)

        H, _ = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, RANSAC_REPROJ_THRESHOLD)
        return H

    else: raise Exception("Not enough matches are found - {}/{}".format(len(good), MIN_MATCH_COUNT))

Замечания:

  • Протестировано на Python 3 и OpenCV 3.4
  • Вам нужен пакет opencv-contrib-python, потому что SIFT имеет проблемы с патентами и был удален из opencv-python
  • Это дает матрицу H для преобразования img1 и перекрывает ее на img2. Если вам интересно, как это сделать, здесь

Ещё вопросы

Сообщество Overcoder
Наверх
Меню