(-215:断言失败)在 opencv 中绘制匹配时
(-215:Assertion failed) while drawing matches in opencv
我目前正在关注这篇文章:https://docs.opencv.org/4.5.2/dc/dc3/tutorial_py_matcher.html
绘制火柴时出现以下错误:
error: (-215:Assertion failed) i1 >= 0 && i1 < static_cast<int>(keypoints1.size()) in function 'cv::drawMatches'
通过经验和一些阅读,我发现当输入 image/matrix 出现问题时,通常会出现 -215 错误代码。我检查了我提供的两张图片中是否有一张以某种方式损坏,但我看不到问题所在。
这里是有问题的代码:
def j_feature_match(src, needle):
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(src, None)
kp2, des2 = orb.detectAndCompute(needle, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING2, crossCheck=False)
matches = bf.match(des2,des1)
matches = sorted(matches, key = lambda x:x.distance)
src_img = cv2.drawMatches(src,kp1,needle,kp2,matches[:10],None,flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
return(src_img, len(matches))
- 编辑:
如评论中所述,图像的分辨率如下:
源码 = 640x480
针 = 300x400
- EDIT2:我已经更改了图片以便分享它们。
- needle
- src
谢谢! :)
根据drawMatches
documentation,matches
参数是matches1to2
。
这意味着匹配应该从 src
到 needle
而不是从 needle
到 src
(顺序很重要)。
您可以将 matches = bf.match(des2,des1)
替换为
matches = bf.match(des1, des2)
或者改变cv2.drawMatches
参数的顺序:
import cv2
def j_feature_match(src, needle):
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(src, None)
kp2, des2 = orb.detectAndCompute(needle, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING2, crossCheck=False)
matches = bf.match(des2, des1)
matches = sorted(matches, key=lambda x: x.distance)
src_img = cv2.drawMatches(needle, kp2, src, kp1, matches[:10], None,
flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
return src_img, len(matches)
im1 = cv2.imread('src.jpg')
im2 = cv2.imread('needle.jpg')
res_im, n_matches = j_feature_match(im1, im2)
cv2.imshow('res_im', res_im)
cv2.waitKey()
cv2.destroyAllWindows()
结果:
我目前正在关注这篇文章:https://docs.opencv.org/4.5.2/dc/dc3/tutorial_py_matcher.html
绘制火柴时出现以下错误:
error: (-215:Assertion failed) i1 >= 0 && i1 < static_cast<int>(keypoints1.size()) in function 'cv::drawMatches'
通过经验和一些阅读,我发现当输入 image/matrix 出现问题时,通常会出现 -215 错误代码。我检查了我提供的两张图片中是否有一张以某种方式损坏,但我看不到问题所在。
这里是有问题的代码:
def j_feature_match(src, needle):
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(src, None)
kp2, des2 = orb.detectAndCompute(needle, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING2, crossCheck=False)
matches = bf.match(des2,des1)
matches = sorted(matches, key = lambda x:x.distance)
src_img = cv2.drawMatches(src,kp1,needle,kp2,matches[:10],None,flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
return(src_img, len(matches))
- 编辑:
如评论中所述,图像的分辨率如下: 源码 = 640x480 针 = 300x400 - EDIT2:我已经更改了图片以便分享它们。
- needle
- src
谢谢! :)
根据drawMatches
documentation,matches
参数是matches1to2
。
这意味着匹配应该从 src
到 needle
而不是从 needle
到 src
(顺序很重要)。
您可以将 matches = bf.match(des2,des1)
替换为
matches = bf.match(des1, des2)
或者改变cv2.drawMatches
参数的顺序:
import cv2
def j_feature_match(src, needle):
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(src, None)
kp2, des2 = orb.detectAndCompute(needle, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING2, crossCheck=False)
matches = bf.match(des2, des1)
matches = sorted(matches, key=lambda x: x.distance)
src_img = cv2.drawMatches(needle, kp2, src, kp1, matches[:10], None,
flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
return src_img, len(matches)
im1 = cv2.imread('src.jpg')
im2 = cv2.imread('needle.jpg')
res_im, n_matches = j_feature_match(im1, im2)
cv2.imshow('res_im', res_im)
cv2.waitKey()
cv2.destroyAllWindows()
结果: