简体   繁体   English

将参数传递给bagOfFeatures函数中的函数句柄

[英]Passing parameters to function handle in bagOfFeatures function

Lets say we have a custom extractor function 可以说我们有一个自定义提取器功能

[features,featureMetrics] = exampleBagOfFeaturesExtractor(img,param1, param2)

I want to call bagOfFeatures function and pass custom extractor function: 我想调用bagOfFeatures函数并传递自定义提取器函数:

extractorFcn = @exampleBagOfFeaturesExtractor;
bag = bagOfFeatures(imgSets,'CustomExtractor',extractorFcn)

In exampleBagOfFeaturesExtractor function I want to use different local descriptors extractor depending on param1. 在exampleBagOfFeaturesExtractor函数中,我想根据param1使用不同的本地描述符提取器。 How do I pass param1 to exampleBagOfFeaturesExtractor? 如何将param1传递给exampleBagOfFeaturesExtractor?

What is the best way to use different local descriptors in my custom extractor function? 在自定义提取器函数中使用不同的本地描述符的最佳方法是什么?

Thank you for your help! 谢谢您的帮助!

Edit 编辑

This is the custom extractor function I am currently using: 这是我当前正在使用的自定义提取器功能:

function [features,featureMetrics] = exampleBagOfFeaturesExtractor(img,param1,param2)

    keypoint_detector = cv.FeatureDetector(param1);
    descriptor_extractor = cv.DescriptorExtractor(param2);

    kpts = keypoint_detector.detect(img);
    [ features, kpts ] = descriptor_extractor.compute(img, kpts);
    featureMetrics=ones(1,size(features,1))/size(features,1);
end

The expected kind of functions that the bagOfFeatures function requires can only be a single input, namely the input image. bagOfFeatures函数所需的预期函数类型只能是单个输入,即输入图像。 Therefore, if you want to create a custom feature extractor where you can vary the parameters, you will need to first create the parameters, then create an anonymous function that captures these parameters via lexical closure. 因此,如果要创建可在其中更改参数的自定义特征提取器,则需要首先创建参数,然后创建一个匿名函数,该函数通过词汇闭包捕获这些参数。 This means that when you create the anonymous function, make sure the parameters are created so that when you reference them in your anonymous function, they capture the most up to date version of the parameters prior to creating the function. 这意味着在创建匿名函数时,请确保已创建参数,以便在匿名函数中引用参数时,它们会在创建函数之前捕获参数的最新版本。

Therefore, assuming param1 and param2 already exist in your workspace, create a function like so: 因此,假设您的工作空间中已经存在param1param2 ,则创建一个类似如下的函数:

% Create param1 and param2 here
param1 = ...;
param2 = ...;
extractorFcn = @(img) exampleBagOfFeaturesExtractor(img, param1, param2);

This creates an anonymous function that takes in a single input - your image. 这将创建一个匿名函数,该函数只接受一个输入-您的图像。 param1 and param2 are thus captured in your function, so the state of the variables is recorded and are made available within the anonymous function. 这样就在函数中捕获了param1param2 ,因此记录了变量的状态,并使它们在匿名函数中可用。 Also note that the function doesn't take in additional inputs, only the input image. 另请注意,该函数不接受其他输入,仅接受输入图像。 You can then call bagOfFeatures as normal. 然后,您可以正常调用bagOfFeatures However, should you want to change param1 or param2 , not only will you have to change these parameters, but you must re-declare the anonymous function again so that the latest stage of the variables is recaptured. 但是,如果要更改param1param2 ,不仅必须更改这些参数,还必须再次重新声明匿名函数,以便重新捕获变量的最新阶段。

As a quick example, suppose I've created an anonymous function like so: 举个简单的例子,假设我创建了一个匿名函数,如下所示:

x = 5;
y = @(t) t + x;

This function y takes the current state of x and adds it with a variable t . 函数y获取x的当前状态,并将其与变量t相加。 For now, this acts like how we expect it: 现在,这就像我们期望的那样:

>> x = 5;
>> y = @(t) t + x;
>> y(6)

ans =

    11

We put in the value 6 and we get 11 . 我们输入值6 ,得到11 If we try and change x then call y , it will not change this in the function as it captured the state of the variable before you created the function: 如果我们尝试更改x然后调用y ,它将不会在函数中对此进行更改,因为它在创建函数之前捕获了变量的状态:

>> x = 10;
>> y(6)

ans =

    11

Therefore, if you want to change the parameters, you must also re-declare the function again before calling bagOfFeatures , so: 因此,如果要更改参数,还必须在调用bagOfFeatures之前再次重新声明该函数,因此:

param1 = ...; % Change this to something new
param2 = ...; % Change this if you like as well
extractorFcn = @(img) exampleBagOfFeaturesExtractor(img, param1, param2);

In MATLAB terms, these variables persist in the anonymous function. 用MATLAB的术语来说,这些变量保留在匿名函数中。 You can read more about it here: https://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html#f4-71621 您可以在此处了解更多信息: https : //www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html#f4-71621

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

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