0

I want to compare different algorithms in openCV for computation of fundamental matrix. I have used the code below to find a fundamental matrix for a corresponding pair of images. Is there a way to do the same thing for a dataset, without using a for loop?

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img1 = cv.imread('myleft.jpg',0)  #queryimage # left image
img2 = cv.imread('myright.jpg',0) #trainimage # right image
sift = cv.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)
# FLANN parameters
FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)
flann = cv.FlannBasedMatcher(index_params,search_params)
matches = flann.knnMatch(des1,des2,k=2)
pts1 = []
pts2 = []
# ratio test as per Lowe's paper
for i,(m,n) in enumerate(matches):
    if m.distance < 0.8*n.distance:
        pts2.append(kp2[m.trainIdx].pt)
        pts1.append(kp1[m.queryIdx].pt)
pts1 = np.int32(pts1)
pts2 = np.int32(pts2)
F, mask = cv.findFundamentalMat(pts1,pts2,cv.FM_LMEDS)
# We select only inlier points
pts1 = pts1[mask.ravel()==1]
pts2 = pts2[mask.ravel()==1]
Roxanya
  • 9
  • 1
  • odd question. the loop merely filters the matches using Lowe's distance ratio test, and then puts the matched keypoints into lists. what bothers you about that? – Christoph Rackwitz Sep 13 '21 at 17:04
  • My question wasn't about the loop in the code, it was if there was a way to do what the code does on a dataset of images, not just a pair – Roxanya Sep 13 '21 at 20:36
  • 1
    ... sure? run it on whatever and however many pairs of images you like? I don't see the challenge in that. if you see one and you'd like to solve it, please point it out. "without a for loop" will be kinda hard, because loops are how you apply the same code to multiple things, in your case images – Christoph Rackwitz Sep 13 '21 at 23:25

0 Answers0