如何在 OpenCV 中使用冲浪和筛选检测器 Python
How to use surf and sift detector in OpenCV for Python
我正在尝试使用函数 SURF() 进行特征匹配的代码。执行时会报错 "AttributeError: 'module' object has no attribute 'SURF'".
如何为 Python (Windows) 下载此模块并修复此错误?
您可以尝试 ORB(Oriented FAST and Rotated BRIEF)作为 open cv 中 SURF 的替代品。它的效果几乎与 SURF 和 SIFT 一样好,而且它 免费 不同于 SIFT 和 SURF 已获得专利,不能用于商业用途。
您可以在 opencv-python 文档中了解更多信息
here
这里是方便您使用的示例代码
import cv2
from matplotlib import pyplot as plt
img1 = cv2.imread('text.png',cv2.COLOR_BGR2GRAY) # queryImage
img2 = cv2.imread('original.png',cv2.COLOR_BGR2GRAY) # trainImage
# Initiate SIFT detector
orb = cv2.ORB_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)
# create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
# Match descriptors.
matches = bf.match(des1,des2)
# Sort them in the order of their distance.
matches = sorted(matches, key = lambda x:x.distance)
# Draw first 10 matches.
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10],None, flags=2)
plt.imshow(img3),plt.show()
起初
pip install opencv-contrib-python
然后使用此技巧创建筛选对象
sift = cv2.xfeatures2d.SIFT_create()
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('simple.jpg',0)
# Initiate FAST object with default values
fast = cv.FastFeatureDetector_create()
# find and draw the keypoints
kp = fast.detect(img,None)
img2 = cv.drawKeypoints(img, kp, None, color=(255,0,0))
# Print all default params
print( "Threshold: {}".format(fast.getThreshold()) )
print( "nonmaxSuppression:{}".format(fast.getNonmaxSuppression()) )
print( "neighborhood: {}".format(fast.getType()) )
print( "Total Keypoints with nonmaxSuppression: {}".format(len(kp)) )
cv.imwrite('fast_true.png',img2)
#Disable nonmaxSuppression
fast.setNonmaxSuppression(0)
kp = fast.detect(img,None)
print( "Total Keypoints without nonmaxSuppression: {}".format(len(kp)) )
img3 = cv.drawKeypoints(img, kp, None, color=(255,0,0))
cv.imwrite('fast_false.png',img3)
根据此来源,SIFT 专利已过期。 https://github.com/opencv/opencv/issues/16736[SIFT to main directory]1 SIFT 现在应该在主 opencv 存储库中实现。至少 cv.SIFT_create()
init 对我有用。我有 opencv 版本 4.5.5
.
与其他描述符或检测器可能组合使用的示例(替代 detectAndCompute()
函数)
import cv2 as cv
MAX_FEATURES = 5500
GOOD_MATCH_PERCENT = 0.03
def find_matches(im1, im2, detector_type: str, descriptor_type: str):
# Convert images to grayscale
im1Gray = cv.cvtColor(im1, cv.COLOR_BGR2GRAY)
im2Gray = cv.cvtColor(im2, cv.COLOR_BGR2GRAY)
# init detector
if detector_type == 'sift':
detector = cv.SIFT_create(MAX_FEATURES)
elif detector_type == 'fast':
detector = cv.FastFeatureDetector_create()
# ... ORB etc.
# init descriptor
if descriptor_type == 'sift':
descriptor = cv.SIFT_create(MAX_FEATURES)
keypoints1 = detector.detect(im1Gray, None)
keypoints2 = detector.detect(im2Gray, None)
# find descriptors with descriptor SIFT
keypoints1, descriptors1 = descriptor.compute(im1Gray, keypoints1)
keypoints2, descriptors2 = descriptor.compute(im2Gray, keypoints2)
# BFMatcher object
matcher = cv.BFMatcher(cv.NORM_L1, crossCheck=True)
matches = list(matcher.match(descriptors1, descriptors2))
# Sort matches by score
matches.sort(key=lambda x: x.distance, reverse=False)
# Remove not so good matches
numGoodMatches = int(len(matches) * GOOD_MATCH_PERCENT)
matches = matches[:numGoodMatches]
# Draw top matches
imMatches = cv.drawMatches(
im1, keypoints1, im2, keypoints2, matches, None)
cv.imwrite("matches.jpg", imMatches)
我正在尝试使用函数 SURF() 进行特征匹配的代码。执行时会报错 "AttributeError: 'module' object has no attribute 'SURF'".
如何为 Python (Windows) 下载此模块并修复此错误?
您可以尝试 ORB(Oriented FAST and Rotated BRIEF)作为 open cv 中 SURF 的替代品。它的效果几乎与 SURF 和 SIFT 一样好,而且它 免费 不同于 SIFT 和 SURF 已获得专利,不能用于商业用途。
您可以在 opencv-python 文档中了解更多信息
here
这里是方便您使用的示例代码
import cv2
from matplotlib import pyplot as plt
img1 = cv2.imread('text.png',cv2.COLOR_BGR2GRAY) # queryImage
img2 = cv2.imread('original.png',cv2.COLOR_BGR2GRAY) # trainImage
# Initiate SIFT detector
orb = cv2.ORB_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)
# create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
# Match descriptors.
matches = bf.match(des1,des2)
# Sort them in the order of their distance.
matches = sorted(matches, key = lambda x:x.distance)
# Draw first 10 matches.
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10],None, flags=2)
plt.imshow(img3),plt.show()
起初
pip install opencv-contrib-python
然后使用此技巧创建筛选对象
sift = cv2.xfeatures2d.SIFT_create()
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('simple.jpg',0)
# Initiate FAST object with default values
fast = cv.FastFeatureDetector_create()
# find and draw the keypoints
kp = fast.detect(img,None)
img2 = cv.drawKeypoints(img, kp, None, color=(255,0,0))
# Print all default params
print( "Threshold: {}".format(fast.getThreshold()) )
print( "nonmaxSuppression:{}".format(fast.getNonmaxSuppression()) )
print( "neighborhood: {}".format(fast.getType()) )
print( "Total Keypoints with nonmaxSuppression: {}".format(len(kp)) )
cv.imwrite('fast_true.png',img2)
#Disable nonmaxSuppression
fast.setNonmaxSuppression(0)
kp = fast.detect(img,None)
print( "Total Keypoints without nonmaxSuppression: {}".format(len(kp)) )
img3 = cv.drawKeypoints(img, kp, None, color=(255,0,0))
cv.imwrite('fast_false.png',img3)
根据此来源,SIFT 专利已过期。 https://github.com/opencv/opencv/issues/16736[SIFT to main directory]1 SIFT 现在应该在主 opencv 存储库中实现。至少 cv.SIFT_create()
init 对我有用。我有 opencv 版本 4.5.5
.
与其他描述符或检测器可能组合使用的示例(替代 detectAndCompute()
函数)
import cv2 as cv
MAX_FEATURES = 5500
GOOD_MATCH_PERCENT = 0.03
def find_matches(im1, im2, detector_type: str, descriptor_type: str):
# Convert images to grayscale
im1Gray = cv.cvtColor(im1, cv.COLOR_BGR2GRAY)
im2Gray = cv.cvtColor(im2, cv.COLOR_BGR2GRAY)
# init detector
if detector_type == 'sift':
detector = cv.SIFT_create(MAX_FEATURES)
elif detector_type == 'fast':
detector = cv.FastFeatureDetector_create()
# ... ORB etc.
# init descriptor
if descriptor_type == 'sift':
descriptor = cv.SIFT_create(MAX_FEATURES)
keypoints1 = detector.detect(im1Gray, None)
keypoints2 = detector.detect(im2Gray, None)
# find descriptors with descriptor SIFT
keypoints1, descriptors1 = descriptor.compute(im1Gray, keypoints1)
keypoints2, descriptors2 = descriptor.compute(im2Gray, keypoints2)
# BFMatcher object
matcher = cv.BFMatcher(cv.NORM_L1, crossCheck=True)
matches = list(matcher.match(descriptors1, descriptors2))
# Sort matches by score
matches.sort(key=lambda x: x.distance, reverse=False)
# Remove not so good matches
numGoodMatches = int(len(matches) * GOOD_MATCH_PERCENT)
matches = matches[:numGoodMatches]
# Draw top matches
imMatches = cv.drawMatches(
im1, keypoints1, im2, keypoints2, matches, None)
cv.imwrite("matches.jpg", imMatches)