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

convert T* array (Jet* or float*) to cv::Mat<CV_32f>

我正在使用带有 AutoDiffCostFunction 的 ceres-solver。我的成本函数将 1x3 向量作为参数并输出 1x1 残差。 如何从我的 T* 参数向量中创建 opencv Mat?它可以是 Jet 或 float。 我尝试了以下代码,但出现错误 "cannot conver from Jet to float"

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。我该怎么做?

您不能以这种方式在 ceres::AutoDiffCostFunction 中使用 OpenCV 方法。 OpenCV 方法没有按照 ceres 进行自动微分所需的类型 T 进行模板化。 float cast 无法完成,因为 Jacobian 的 ceres jet 是矢量而不是标量。

您有两个选择:

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

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