1

我正在使用带有 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* 矩阵中获取 Eigen 矩阵:

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

但我想打电话cv::decomposeHomographyMat。我怎样才能做到这一点?

4

1 回答 1

2

You cannot use an OpenCV method in a ceres::AutoDiffCostFunction in this way. The OpenCV method is not templated with type T as required by ceres to do the automatic differentiation. The float cast cannot be done because the ceres jet of Jacobians is a vector and not a scalar.

You have two options:

1) Use numerical differentiation: see http://ceres-solver.org/nnls_tutorial.html#numeric-derivatives

2) Use a templated library (e.g. Eigen http://eigen.tuxfamily.org/index.php?title=Main_Page) to rewrite the required homography decomposition

于 2015-12-14T16:26:27.680 回答