使用Python在OpenCv中设置ORB参数 [英] Setting ORB parameters in OpenCv with Python

查看:567
本文介绍了使用Python在OpenCv中设置ORB参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在Python中使用OpenCV 2.4来匹配两个图像之间的特征,但是我想更改"ORB"检测器的参数之一(它提取"nfeatures"的特征数),并且似乎有用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.

对于C ++,您可以通过FeatureDetector/DescriptorExtractor的读取"(对于Java是加载"?)方法来加载参数yml/xml文件.但是,Python绑定缺少此函数/方法.

For C++ you can load a parameter yml/xml file by the 'read' (or 'load' for java?) methods of FeatureDetector/DescriptorExtractor. However the Python binding is missing this function/method.

它也缺少直接创建ORB对象的绑定,因此我无法在该处传递参数(Python绑定似乎要求您使用字符串名称使用cv2.DescriptorExtractor_create -如果传递错误的字符串则会出现段错误名称或参数以及它的名称...此外,该函数不能接受似乎传递给构造函数的任何其他参数.

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.

我唯一的希望似乎是使用cv2.cv.Load(filename)从xml加载完整的对象,但这似乎期望对象实例而不是Algorithm定义,为此我无法在其中找到任何Python绑定新的或旧的语法.我在文件加载步骤中尝试了几种变体,包括模仿从OpenCV中保存的xml文件的样式,但是没有运气.

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. I tried several variations on the file loading step, including mimicking the style of saved xml files from OpenCV with no luck.

在我上面尝试过的将参数传递到OpenCV中的检测器(SURF或ORB或任何通用算法)上的步骤中,有没有人能成功?

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?

这是我用来提取特征的代码:

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

编辑

更改检测器设置似乎仅在Windows实施上出现段错误-等待补丁或修复程序出现在OpenCV的网站上.

Changing detector settings seems to only segfault on windows implementation -- waiting for a patch or fix to appear on OpenCV's site.

推荐答案

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")

输出:

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

ORB parameters (dict): ['WTA_K', 'edgeThreshold', 'firstLevel', 'nFeatures', 'nLevels', 'patchSize', 'scaleFactor', 'scoreType']
WTA_K = 2
edgeThreshold = 31
firstLevel = 0
nFeatures = 500
nLevels = 8
patchSize = 31
scaleFactor = 1.20000004768
scoreType = 0
nFeatures before: 500
nFeatures after: 1000

编辑:现在更容易使用OpenCV 3.0进行操作

To do the same with OpenCV 3.0 is now easier

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.

这篇关于使用Python在OpenCv中设置ORB参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆