Ask Your Question

Revision history [back]

I managed to get the setter and getter for the addParam method to work. The issue was that I needed to perform a static_cast to the parent class like so:

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

However, I also found that OpenCV doesn't know how to handle the read and writes of a uchar[3]. I tried a cv::Vec3b which didn't seem to work either so I settled on a cv::Mat using the setters and getters. So the final solution looks something like this.

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