在AlgorithmInfo :: addParam中使用Setters和Getters进行算法继承

时间:2012-09-07 17:57:22

标签: c++ algorithm image-processing opencv member-function-pointers

我正在使用the reference from the OpenCV docs创建我自己的继承自cv :: Algorithm的算法。我已经创建了自己的类,它继承自cv :: Algorithm并且成功但是我对这个有困难,因为它有一个成员m_model,它是一个无法修改的库的结构,因为{{ 1}}类将此功能包装在此结构中。

无论如何,我试图在结构中引用一个`uchar [3],所以我将它包装在cv :: Ptr中。 当我在addParam方法

上编译没有和getter或setter的程序时
MyAlgorithm

代码编译得很好,但是当我尝试将obj.info()->addParam<uchar[3]>(obj, "arrPtr", *arrPtr, false); 一个MyAlgorithm对象归档时,我收到运行时错误,因为它无法获取数据。它正在寻找具有write名称的成员变量,但它不存在。所以我为arr类成员中的arr参数定义了一些getter和setter方法。

但是,我不知道如何将成员函数指针传递给addParams方法。我知道我不能像我目前在下面的代码中那样将它们传递给addParams方法。我也尝试了以下内容:

m_model

但是我收到了编译错误:

obj.info()->addParam<uchar[3]>(obj, "arr",  *arrPtr, false, &MyAlgorithm::getArr, &MyAlgorithm::setArr);

下面是我的源代码的精简示例。任何帮助将不胜感激。

my_algorithm.h

cannot convert parameter 5 from 'cv::Ptr<_Tp> (__thiscall MyAlgorithm::* )(void)' to 'cv::Ptr<_Tp> (__thiscall cv::Algorithm::* )(void)'

my_algorithm.cpp

class MyAlgorithm : public cv::Algorithm    {
public:
    //Other class logic
    cv::Ptr<uchar[3]> getArr();
    void setArr(const cv::Ptr<uchar[3]> &arrPtr);

    virtual cv::AlgorithmInfo* info() const;
protected:
    //define members such as m_model
};
cv::Algorithm* createMyAlgorithm();
cv::AlgorithmInfo& MyAlgorithm_info();

1 个答案:

答案 0 :(得分:2)

我设法让settergetter使addParam方法有效。问题是我需要像这样对父类执行static_cast

typedef cv::Ptr<uchar[3]> (cv::Algorithm::*ArrGetter)();
typedef void (cv::Algorithm::*ArrSetter)(const cv::Ptr<uchar[3]> &);
obj.info()->addParam<uchar[3]>(obj, "arr",  *arrPtr, false, static_cast<ArrGetter>(&MyAlgorithm::getArr), static_cast<ArrSetter>(&MyAlgorithm::setArr));

但是,我还发现OpenCV不知道如何处理uchar[3]的读写。我尝试了cv::Vec3b这似乎不起作用,所以我使用setter和getter确定了cv::Mat。所以最终的解决方案看起来像这样。

my_algorithm.h

class MyAlgorithm : public cv::Algorithm    {
public:
    typedef cv::Mat (cv::Algorithm::*ArrGetter)();
    typedef void (cv::Algorithm::*ArrSetter)(const cv::Mat &);

    //Other class logic
    cv::Mat getArr();
    void setArr(const cv::Mat &arrPtr);

    virtual cv::AlgorithmInfo* info() const;
protected:
    uchar[3] arr;
};
cv::Algorithm* createMyAlgorithm();
cv::AlgorithmInfo& MyAlgorithm_info();

my_algorithm.cpp

cv::AlgorithmInfo* MyAlgorithm::info() const
{
    static volatile bool initialized = false;

    if( !initialized ) 
    { 
        initialized = true;
        MyAlgorithm obj;
        cv::Vec3b arrVec(arr);
        obj.info()->addParam(obj, "arr",  (cv::Mat)arrVec, false, static_cast<ArrGetter>(&MyAlgorithm::getArr), static_cast<ArrSetter>(&MyAlgorithm::setArr));
    } 
    return &MyAlgorithm_info();
}

cv::Mat MyAlgorithm::getArr(){
    cv::Vec3b arrVec(arr);
    return (cv::Mat)arrVec;
}
void MyAlgorithm::setArr(const cv::Mat &arrMat){
    for (int i = 0; i < 3; i++)
        arr[i] = arrMat.at<uchar>(i);
}