python - 使用MSER作为关键点检测器并使用SIFT作为描述符

标签 python image opencv image-processing computer-vision

我正在尝试使用MSER提取关键点并将SIFT用作功能描述符,然后匹配匹配的关键点。我使用Python执行以下操作:

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

img1 = cv2.imread('1.jpg')
img2 = cv2.imread('2.jpg') 

mser = cv2.MSER_create()

kp1 = mser.detect(img1)
kp2 = mser.detect(img2)

sift = cv2.xfeatures2d.SIFT_create()

kp1, des1 = sift.detectAndCompute(img1, kp1)
kp2, des2 = sift.detectAndCompute(img2, kp2)

bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2, k=2)

good = []
for m, n in matches:
    if m.distance < 0.8 * n.distance:
        good.append(m)

good = sorted(good, key=lambda x: x.distance)
print len(good)

matching_result = cv2.drawMatches(img1, kp1, img2, kp2, good, None, flags=2)
cv2.imwrite('result.jpg', matching_result)

但是,出现以下错误:
kp1, des1 = sift.detectAndCompute(img1, kp1)
TypeError: mask is not a numpy array, neither a scalar

如何解决此问题?而且,使用检测器和描述符的方式是否正确?

最佳答案

您可以基于现有关键点来计算描述符。一个快速的解决方法是从您的代码中更改这些行:

kp1, des1 = sift.detectAndCompute(img1, kp1)
kp2, des2 = sift.detectAndCompute(img2, kp2)

而是使用compute()函数:
kp1, des1 = sift.compute(img1, kp1)
kp2, des2 = sift.compute(img2, kp2)

关于python - 使用MSER作为关键点检测器并使用SIFT作为描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51716948/

相关文章:

css - 如何在 BuddyPress 子主题中拉伸(stretch) IE 中的背景图像

html, css img 停止收缩 <div> :)

python - 距离变换 - 该功能无法正常工作

c++ - 将两个 std::vector<cv::Point> vector 和安全公共(public)点与第三个 std::vector<cv::Point> 进行比较

python - 在 PyInstaller 中是否有一个内置函数来保持命令窗口打开

javascript - 将 SVG 导出为 PDF,且组/图层完好无损

javascript - 从具有 ID 的 div 中删除没有 ID 的图像

c++ - 使用 SWIG 在 GO 中包装 OpenCV 的 C++ API

Python AIOHTTP.web 服务器多处理负载均衡器?

python - 返回方程最接近于零的输入数字