首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当绘制匹配关键点BF BRISK+FREAK时断言失败

当绘制匹配关键点BF BRISK+FREAK时断言失败
EN

Stack Overflow用户
提问于 2021-06-17 18:03:51
回答 1查看 98关注 0票数 1

我正试图用BRISK+FREAK缝制两幅图像

这是代码,当我试图绘制匹配时,我会得到一个错误

/io/opencv/modules/features2d/src/draw.cpp:225:错误:(-215:断言失败)函数'drawMatches‘中的i1 >= 0 && i1

代码语言:javascript
复制
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)

错误: /io/opencv/modules/features2d/src/draw.cpp:225:错误:(-215:断言失败)函数'drawMatches‘中的i1 >= 0& i1

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-17 20:39:54

你把keypoints和的features混合在一起

仔细看一下detectAndDescribe的代码

代码语言:javascript
复制
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

FREAK

  • Returns freakExtractor.compute

的输出,

  • 检测关键点。

建议的执行:

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68024452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档