Unknown C++ exception while drawing matches using cv2.line_descriptor.drawLineMatches() in OpenCV Python

huangapple go评论72阅读模式
英文:

Unknown C++ exception while drawing matches using cv2.line_descriptor.drawLineMatches() in OpenCV Python

问题

我正在尝试使用OpenCV中的二进制描述符模块来匹配从两个图像中提取的线描述符,在Python中进行操作。但是,当使用cv2.line_descriptor.drawLineMatches()函数绘制匹配时,我遇到了以下错误:

img3 = cv2.line_descriptor.drawLineMatches(img,keylines1,field,keylines2,matches[:10],None)
cv2.error: 来自OpenCV代码的未知C++异常

我做错了什么?是否有任何建议?谢谢。

import cv2
import numpy as np 

# 读取灰度图像
img = cv2.imread("8.jpg", 0)
field = cv2.imread('field1.png', 0)

lsd = cv2.line_descriptor.LSDDetector.createLSDDetector()

# 检测图像中的线条
keylines1 = lsd.detect(img, scale=2, numOctaves=2)
keylines2 = lsd.detect(field, scale=2, numOctaves=2)

# 计算线条的描述符
bd = cv2.line_descriptor.BinaryDescriptor.createBinaryDescriptor()
k1, descr1 = bd.compute(img, keylines1)
k2, descr2 = bd.compute(field, keylines2)

bdm = cv2.line_descriptor.BinaryDescriptorMatcher()

matches = bdm.match(descr1, descr2)

matches = sorted(matches, key=lambda x: x.distance)

img3 = cv2.line_descriptor.drawLineMatches(img, keylines1, field, keylines2, matches[:10], None)

# 显示图像
cv2.imshow("LSD", img3)
cv2.waitKey(0)
英文:

I am trying to match line descriptors extracted from two images using the binary descriptor module of OpenCV in Python. But, when drawing the matches using cv2.line_descriptor.drawLineMatches() function, I am getting the below error

img3 = cv2.line_descriptor.drawLineMatches(img,keylines1,field,keylines2,matches[:10],None)

cv2.error: Unknown C++ exception from OpenCV code

What's wrong I am doing? Any suggestion would help. Thanks.

import cv2
import numpy as np 

#Read gray image
img = cv2.imread("8.jpg",0)
field = cv2.imread('field1.png',0)

lsd = cv2.line_descriptor.LSDDetector.createLSDDetector()


#Detect lines in the image
keylines1 = lsd.detect(img, scale=2, numOctaves=2)
keylines2 = lsd.detect(field, scale=2, numOctaves=2)


# # # compute descriptors for lines
bd = cv2.line_descriptor.BinaryDescriptor.createBinaryDescriptor()
k1,descr1 = bd.compute(img, keylines1)
k2,descr2 = bd.compute(field, keylines2)

bdm =  cv2.line_descriptor.BinaryDescriptorMatcher()

matches = bdm.match(descr1, descr2)

matches = sorted(matches, key = lambda x:x.distance)
#img3 = cv2.drawMatches(img,k1,field,k2,matches[10],None,1)

img3 = cv2.line_descriptor.drawLineMatches(img,keylines1,field,keylines2,matches[:10],None)

#Show image
cv2.imshow("LSD",img3 )
cv2.waitKey(0)

答案1

得分: 4

It looks like there is a bug in the Python to C++ binding in cv2.line_descriptor.drawLineMatches method.
当添加matchesMask参数时,该方法似乎可以工作。

Note that the code is working only with very specific versions of OpenCV.
请注意,此代码仅适用于非常特定版本的OpenCV。

The LSDDetector was removed in OpenCV version 4 (it may be part of the reason that the bug is not fixed).
在OpenCV版本4中删除了LSDDetector(这可能是未修复错误的部分原因)。

The following example is working with opencv-contrib-python version 3.4.18.65:
以下示例适用于opencv-contrib-python版本3.4.18.65

...

matches = sorted(matches, key = lambda x:x.distance)

good_matches = np.ones(10, np.uint8)

#Recommended to use colored images:
#img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
#field = cv2.cvtColor(field, cv2.COLOR_GRAY2BGR)

img3 = cv2.line_descriptor.drawLineMatches(img, keylines1, field, keylines2, matches[:10], None, matchColor=-1, singleLineColor=-1, matchesMask=good_matches)

英文:

It looks like there is a bug in the Python to C++ binding in cv2.line_descriptor.drawLineMatches method.
When adding the matchesMask argument, the method seems to work.

Note that the code is working only with very specific versions of OpenCV.
The LSDDetector was removed in OpenCV version 4 (it may be part of the reason that the bug is not fixed).

The following example is working with opencv-contrib-python version 3.4.18.65:

...

matches = sorted(matches, key = lambda x:x.distance)

good_matches = np.ones(10, np.uint8)

#Recommended to use colored images:
#img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
#field = cv2.cvtColor(field, cv2.COLOR_GRAY2BGR)

img3 = cv2.line_descriptor.drawLineMatches(img, keylines1, field, keylines2, matches[:10], None, matchColor=-1, singleLineColor=-1, matchesMask=good_matches)

huangapple
  • 本文由 发表于 2023年5月14日 12:57:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定