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

标签 c++ algorithm image-processing opencv pointer-to-member

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

无论如何,我试图引用结构中的一个成员,即“uchar[3]”,所以我将它包装在 cv::Ptr 中。 当我在 addParam 方法上不使用 getter 或 setter 来编译程序时

obj.info()->addParam<uchar[3]>(obj, "arrPtr",  *arrPtr, false);

代码编译得很好,但当我尝试将 MyAlgorithm 对象写入文件时,出现运行时错误,因为它无法获取数据。它正在寻找名称为 arr 的成员变量,但该变量不存在。因此,我为 m_model 类成员中的 arr 参数定义了一些 getter 和 setter 方法。

但是,我不确定如何将成员函数指针传递到 addParams 方法中。我知道我不能像我当前在下面的代码中所做的那样将它们传递到 addParams 方法中。我还尝试过以下方法:

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

但我收到编译错误:

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

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

my_algorithm.h

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();

my_algorithm.cpp

cv::Algorithm* createMyAlgorithm()
{
   return new MyAlgorithm();
}

cv::AlgorithmInfo& MyAlgorithm_info() 
{
   static cv::AlgorithmInfo MyAlgorithm_info_var("MyAlgorithm", createMyAlgorithm);
   return MyAlgorithm_info_var;
}

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

   if( !initialized ) 
   { 
      initialized = true;
      MyAlgorithm obj; 
      cv::Ptr<uchar[3]> *arrPtr = new cv::Ptr<uchar[3]>(&(obj.m_model->arr));
      obj.info()->addParam<uchar[3]>(obj, "arr",  *arrPtr, false, &getArr, &setArr);
   } 
   return &MyAlgorithm_info();
}

cv::Ptr<uchar[3]> MyAlgorithm::getArr(){
   //Logic to get arr
}
void MyAlgorithm::setArr(const cv::Ptr<uchar[3]> &arrPtr){
   //Logic to set arr
}

最佳答案

我设法让 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);
}

关于c++ - 在 AlgorithmInfo::addParam 中使用 Setters 和 Getters 进行算法继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12323107/

相关文章:

c++ - 指向函数参数与函数参数的指针?

regex - 将多个正则表达式组合成一个/构建小正则表达式以匹配一组固定字符串

android - 使用实际像素在图像上绘制

c++ - 如何静态和动态链接C/C++库

c++ - 不断产生链接器和库错误

sql - SQL 之上的列表实现

algorithm - 负权重的 Dijkstra

python - 在 python 中读取卫星图像文件时优化性能

opencv - 在噪声很大的图像中检测窄线

c++ - 为什么 clock() 在集群机器上不起作用