简体   繁体   English

使用Python在OpenCv中设置ORB参数

[英]Setting ORB parameters in OpenCv with Python

I've been using OpenCV 2.4 in Python to match features between two images, but I want to change one of the parameters of the "ORB" detector (the number of features it extracts "nfeatures") and there seems to be no way to do so in Python. 我一直在Python中使用OpenCV 2.4来匹配两个图像之间的特征,但是我想更改“ ORB”检测器的参数之一(它提取“ nfeatures”的特征数量),似乎没有办法在Python中这样做。

For C++ you can load a parameter yml/xml file by the 'read' (or 'load' for java?) methods of FeatureDetector/DescriptorExtractor. 对于C ++,您可以通过FeatureDetector / DescriptorExtractor的“读取”(或Java的“加载”)方法加载yml / xml参数文件。 However the Python binding is missing this function/method. 但是,Python绑定缺少此函数/方法。

It's also missing the binding to create an ORB object directly, so I can't pass the parameters there (Python binding seems to requires you to use cv2.DescriptorExtractor_create by string name -- which will segfault if you pass a bad string name or the parameters along with it... Additionally that function cannot take any other arguments it seems to pass onto the constructor. 它还缺少直接创建ORB对象的绑定,因此我无法在该处传递参数(Python绑定似乎要求您通过字符串名称使用cv2.DescriptorExtractor_create -如果传递错误的字符串名称或参数以及它...另外,该函数不能接受似乎传递给构造函数的任何其他参数。

My only hope seemed to be loading the complete object from xml with cv2.cv.Load(filename), but that seems to expect an object instance and not an Algorithm definition, for which I can't find any Python bindings in new or old syntax. 我唯一的希望似乎是使用cv2.cv.Load(filename)从xml加载完整的对象,但这似乎期望对象实例而不是Algorithm定义,为此我找不到新旧的Python绑定句法。 I tried several variations on the file loading step, including mimicking the style of saved xml files from OpenCV with no luck. 我在文件加载步骤中尝试了几种变体,包括模仿从OpenCV中保存的xml文件的样式,没有运气。

Has anyone has success in one of the steps I tried above to pass parameters onto a detector (SURF or ORB, or any generic algorithm) in OpenCV? 在我上面尝试过的将参数传递到OpenCV中的检测器(SURF或ORB或任何通用算法)上的步骤之一中,是否有人成功?

Here is the code I am using to extract features: 这是我用来提取特征的代码:

def findFeatures(greyimg, detector="ORB", descriptor="ORB"):
    nfeatures = 2000 # No way to pass to detector...?
    detector = cv2.FeatureDetector_create(detector)
    descriptorExtractor = cv2.DescriptorExtractor_create(descriptor)
    keypoints = detector.detect(greyimg)
    (keypoints, descriptors) = descriptorExtractor.compute(greyimg, keypoints)
    return keypoints, descriptors

EDIT 编辑

Changing detector settings seems to only segfault on windows implementation -- waiting for a patch or fix to appear on OpenCV's site. 更改检测器设置似乎仅在Windows实施上出现段错误-等待补丁或修复程序出现在OpenCV的网站上。

import cv2

# to see all ORB parameters and their values
detector = cv2.FeatureDetector_create("ORB")    
print "ORB parameters (dict):", detector.getParams()
for param in detector.getParams():
    ptype = detector.paramType(param)
    if ptype == 0:
        print param, "=", detector.getInt(param)
    elif ptype == 2:
        print param, "=", detector.getDouble(param)

# to set the nFeatures
print "nFeatures before:", detector.getInt("nFeatures")
detector.setInt("nFeatures", 1000)
print "nFeatures after:", detector.getInt("nFeatures")

with output: 输出:

ORB parameters (dict): ['WTA_K', 'edgeThreshold', 'firstLevel', 'nFeatures', 'nLevels', 'patchSize', 'scaleFactor', 'scoreType'] ORB参数(字典):['WTA_K','edgeThreshold','firstLevel','nFeatures','nLevels','patchSize','scaleFactor','scoreType']
WTA_K = 2 WTA_K = 2
edgeThreshold = 31 edgeThreshold = 31
firstLevel = 0 firstLevel = 0
nFeatures = 500 n功能= 500
nLevels = 8 nLevels = 8
patchSize = 31 patchSize = 31
scaleFactor = 1.20000004768 scaleFactor = 1.20000004768
scoreType = 0 scoreType = 0
nFeatures before: 500 n功能:500
nFeatures after: 1000 n之后的功能:1000

EDIT: To do the same with OpenCV 3.0 is now easier 编辑:现在更容易使用OpenCV 3.0

import cv2

detector = cv2.ORB_create()
for attribute in dir(new_detector):
    if not attribute.startswith("get"):
        continue
    param = attribute.replace("get", "")
    get_param = getattr(new_backend, attribute)
    val = get_param()
    print param, '=', val

and analogically with a setter. 并类似于二传手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM