与 FLANN 进行特征匹配时出现 OpenCV 错误

OpenCV error while feature matching with FLANN

我有一段代码用于匹配影片剪辑和参考图像之间的特征。它通常运行良好,但有时会在剪辑中间抛出错误。因为它总是在同一个片段中,而且在同一时间,我猜它试图分析的帧有问题。

我的代码:

 cap = cv2.VideoCapture(clip_file)
 img1 = cv2.imread(ref_image,0)

 while(cap.isOpened()):

    # read the frame and convert to gray-scale
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Initiate ORB detector
    orb = cv2.ORB_create()

    # find the keypoints and descriptors with ORB
    kp1, des1 = orb.detectAndCompute(img1,None)
    kp2, des2 = orb.detectAndCompute(gray,None)

    # FLANN parameters
    FLANN_INDEX_LSH = 6
    index_params= dict(algorithm = FLANN_INDEX_LSH,
                table_number = 6, # 12
                key_size = 12,     # 20
                multi_probe_level = 1) #2
    search_params = dict(checks=50)   # or pass empty dictionary

    flann = cv2.FlannBasedMatcher(index_params,search_params)

    matches = flann.knnMatch(des1,des2,k=2)
    cv2.imshow('img3',frame)

播放片段时有时会抛出的错误:

Traceback (most recent call last):
 File "movie_test.py", line 81, in <module>
flann_movie('data/movie.avi','data/ref.jpg')
 File "movie_test.py", line 35, in flann_movie
matches = flann.knnMatch(des1,des2,k=1)
 cv2.error: OpenCV(3.4.2) C:\projects\opencv- 
  python\opencv\modules\flann\src\miniflann.cpp:317: error: (-5:Bad 
   argument) Only continuous arrays are supported in function 
   'cv::flann::buildIndex_'

任何关于错误原因的建议都将不胜感激。谢谢

我认为错误是由您的视频帧引起的,其中没有检测到原始特征模板的痕迹。检查每个帧的匹配中间结果是什么,然后如果这是原因,请更改 FLANN 的参数或在错误发生之前跳过这些帧。

您有以下内容:

matches = flann.knnMatch(des1,des2,k=2)

k=2 意味着每个元素需要有 2 个最近的邻居。因此,每个描述符列表都需要包含 2 个以上的元素:

if(des1 is not None and len(des1)>2 and des2 is not None and len(des2)>2):
   matches = flann.knnMatch(des1,des2,k=2)

(k-nearest neighbors algorithm)