绘制匹配关键点时断言失败 BF BRISK+FREAK
Assertion failed while drawing match Key Points BF BRISK+FREAK
我正在尝试使用 BRISK+FREAK
拼接两张图片
这是代码,当我尝试绘制火柴时出现错误
error: OpenCV(4.1.2) /io/opencv/modules/features2d/src/draw.cpp:225: error: (-215:Assertion failed) i1 >= 0 && i1 < static_cast(keypoints1.size()) in function 'drawMatches'
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()
这是我得到的完整错误
Using: bf feature matcher Raw matches (Brute force): 1967
--------------------------------------------------------------------------- error Traceback (most recent call
last) in ()
4
5 matches = 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
error: OpenCV(4.1.2) /io/opencv/modules/features2d/src/draw.cpp:225:
error: (-215:Assertion failed) i1 >= 0 && i1 <
static_cast(keypoints1.size()) in function 'drawMatches'
似乎不知道这里出了什么问题,发现了这个OpenCV Sift/Surf/Orb : drawMatch function is not working well无法理解如何更正这个
您正在混合来自 FREAK 的 keypoints
和来自 BRISK 的 features
:
仔细看一下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
- Returns
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)
我正在尝试使用 BRISK+FREAK
这是代码,当我尝试绘制火柴时出现错误
error: OpenCV(4.1.2) /io/opencv/modules/features2d/src/draw.cpp:225: error: (-215:Assertion failed) i1 >= 0 && i1 < static_cast(keypoints1.size()) in function 'drawMatches'
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()
这是我得到的完整错误
Using: bf feature matcher Raw matches (Brute force): 1967 --------------------------------------------------------------------------- error Traceback (most recent call last) in () 4 5 matches = 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
error: OpenCV(4.1.2) /io/opencv/modules/features2d/src/draw.cpp:225: error: (-215:Assertion failed) i1 >= 0 && i1 < static_cast(keypoints1.size()) in function 'drawMatches'
似乎不知道这里出了什么问题,发现了这个OpenCV Sift/Surf/Orb : drawMatch function is not working well无法理解如何更正这个
您正在混合来自 FREAK 的 keypoints
和来自 BRISK 的 features
:
仔细看一下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
- Returns
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)