【问题标题】:Assertion failed while drawing match Key Points BF BRISK+FREAK绘制匹配关键点时断言失败 BF BRISK+FREAK
【发布时间】:2021-09-02 13:25:13
【问题描述】:

我正在尝试使用BRISK+FREAK 拼接两张图片

这是代码,当我尝试绘制匹配项时出现错误

错误:OpenCV(4.1.2) /io/opencv/modules/features2d/src/draw.cpp:225: 错误: (-215: 断言失败) i1 >= 0 && i1

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

trainImg = cv2.imread('/content/img1.JPG')
trainImg_gray = cv2.cvtColor(trainImg, cv2.COLOR_BGR2GRAY)

queryImg = cv2.imread('/content/img2.JPG')
queryImg_gray = cv2.cvtColor(queryImg, cv2.COLOR_BGR2GRAY)

def detectAndDescribe(image, method=None):
    """
    Compute key points and feature descriptors using an specific method
    """
    
    descriptor = cv2.BRISK_create()
    # get keypoints and descriptors
    (kps, features) = descriptor.detectAndCompute(image, None)

    freakExtractor = cv2.xfeatures2d.FREAK_create()
    keypoints,descriptors= freakExtractor.compute(image,kps)

    return (keypoints, features)

method = 'brisk'
feature_extractor = 'brisk'
feature_matching = 'bf'
kpsA, featuresA = detectAndDescribe(trainImg_gray, method=feature_extractor)
kpsB, featuresB = detectAndDescribe(queryImg_gray, method=feature_extractor)

"Create and return a Matcher Object"
createMatcher = lambda crossCheck :  cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=crossCheck)

def matchKeyPointsBF(featuresA, featuresB, method):
    bf = createMatcher(crossCheck=True)
        
    # Match descriptors.
    best_matches = bf.match(featuresA,featuresB)
    
    # Sort the features in order of distance.
    # The points with small distance (more similarity) are ordered first in the vector
    rawMatches = sorted(best_matches, key = lambda x:x.distance)
    print("Raw matches (Brute force):", len(rawMatches))
    return rawMatches

print("Using: {} feature matcher".format(feature_matching))

fig = plt.figure(figsize=(20,8))

matches = matchKeyPointsBF(featuresA, featuresB, method=feature_extractor)
img3 = cv2.drawMatches(trainImg,kpsA,queryImg,kpsB,matches,None,flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
    

plt.imshow(img3)
plt.show()

这是我得到的完整错误

使用:bf 特征匹配器原始匹配(蛮力):1967 -------------------------------------------------- -------------------------错误回溯(最近的调用 最后)在() 4 5 个匹配项 = matchKeyPointsBF(featuresA, featuresB, method=feature_extractor) ----> 6 img3 = cv2.drawMatches(trainImg,kpsA,queryImg,kpsB,matches,None,flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS) 7 8

错误:OpenCV(4.1.2) /io/opencv/modules/features2d/src/draw.cpp:225: 错误:(-215:断言失败)i1 >= 0 && i1

似乎不知道这里出了什么问题,发现此OpenCV Sift/Surf/Orb : drawMatch function is not working well 无法理解如何纠正此问题

【问题讨论】:

  • @ChristophRackwitz 是的,就是我。感觉最好同时发布两者以快速获得更清晰的图片

标签: python opencv image-stitching brisk freak


【解决方案1】:

您正在混合来自 FREAKkeypoints 和来自 BRISKfeatures

仔细看detectAndDescribe的代码:

def detectAndDescribe(image, method=None):
    """
    Compute key points and feature descriptors using an specific method
    """
    
    descriptor = cv2.BRISK_create()
    # get keypoints and descriptors
    (kps, features) = descriptor.detectAndCompute(image, None)

    freakExtractor = cv2.xfeatures2d.FREAK_create()
    keypoints,descriptors= freakExtractor.compute(image,kps)

    return (keypoints, features)  # <------ keypoints from freakExtractor.compute and features from descriptor.detectAndCompute

报告的异常看起来是随机的,所以很难找到问题...


您可以按如下方式实现detectAndDescribe

  • 用 BRISK 检测关键点
  • 将检测到的关键点传递给 FREAK
  • 返回freakExtractor.compute的输出

建议的实现:

def detectAndDescribe(image, method=None):
    descriptor = cv2.BRISK_create()
    kps = descriptor.detect(image) # kps, features = descriptor.detectAndCompute(image, None)
    freakExtractor = cv2.xfeatures2d.FREAK_create()
    keypoints, descriptors= freakExtractor.compute(image, kps)
    return (keypoints, descriptors)

【讨论】:

  • FREAK Descriptor with Opencv Python 这篇文章指定需要单独的特征描述符 BRIEF (Binary Robust Independent Elementary Features) 来获得我们需要输入到 FREAKkeypoints,因为必须检测到 kps 仍然是您的解决方案kps, features= freakExtractor.compute(image,kps)会弹出错误
  • @SanthoshDhaipuleChandrakanth 你是对的,(我没有收到错误,因为我的测试从未达到FREAK_create 部分)。我不知道所有的限制。该代码在使用 BRISK 检测关键点并将关键点作为输入传递给 FREAK 时起作用。我更新了帖子,谢谢。
猜你喜欢
  • 2021-10-09
  • 2020-08-09
  • 2018-03-11
  • 1970-01-01
  • 2014-10-09
  • 2013-03-17
  • 2019-11-23
  • 1970-01-01
  • 2016-11-04
相关资源
最近更新 更多