将T *数组(Jet *或float *)转换为cv :: Mat <cv_32f>

时间:2015-10-22 12:11:36

标签: c++ opencv ceres-solver

我正在使用具有AutoDiffCostFunction的ceres-solver。我的成本函数作为参数1x3向量并输出1x1残差。 如何从我的T *参数向量中创建opencv Mat?它可能是Jet或浮点数。 我尝试了下面的代码,但得到错误“无法从Jet转换为浮动”

struct ErrorFunc
{
    template <typename T>
    bool operator()(const T * const Kparams, T * residual) const // Kparams - [f, u, v]
    {
        cv::Mat K = cv::Mat::eye(3, 3, CV_32F);
        K.at<float>(0, 0) = float(Kparams[0]); // error
        K.at<float>(0, 2) = float(Kparams[1]); // error
        K.at<float>(1, 1) = float(Kparams[0]); // error
        K.at<float>(1, 2) = float(Kparams[2]); // error

        Mat Hdot = K.inv() * H * K;

        cv::decomposeHomographyMat(Hdot, K, rot, tr, norm); //want to call this opencv function

        residual[0] = calcResidual(norm);
        return true;
    }
    Mat H;
}

有一种方法可以从T *矩阵中得到特征矩阵:

const Eigen::Matrix< T, 3, 3, Eigen::RowMajor> hom = Eigen::Map< const Eigen::Matrix< T, 3, 3, Eigen::RowMajor> >(Matrix)

但我想致电cv::decomposeHomographyMat。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

您不能以这种方式在ceres :: AutoDiffCostFunction中使用OpenCV方法。根据ceres要求进行自动区分,OpenCV方法不会使用类型T进行模板化。由于Jacobians的ceres射流是一个向量而不是一个标量,因此无法完成浮动投射。

您有两种选择:

1)使用数值区分:见http://ceres-solver.org/nnls_tutorial.html#numeric-derivatives

2)使用模板化库(例如Eigen http://eigen.tuxfamily.org/index.php?title=Main_Page)重写所需的单应性分解