2015-10-22 61 views
0

我正在使用ceres-solver和AutoDiffCostFunction。我的成本函数作为参数1x3向量并输出1x1残差。 如何创建我的T *参数向量中的opencv Mat?它可能是Jet或浮动。 我尝试下面的代码,但得到错误“无法从喷气CONVER浮动”将T * array(Jet *或float *)转换为cv :: Mat <CV_32f>

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。我怎样才能做到这一点?

+0

顺便说一句,CV :: Mat没有[]运算符 – berak

+0

您是否可以使用此原型创建函数? float getFromJetAt(Jet * Kparams,int pos)。该函数应该从Jet中获得位置pos的值。而且你不必担心它在该函数中浮动的可能性。 –

回答

2

以这种方式您不能在ceres :: AutoDiffCostFunction中使用OpenCV方法。 OpenCV方法不是根据ceres进行自动微分所需的类型T进行模板化。由于雅各布人的ceres射流是矢量而不是标量,因此无法完成浮法。

你有两个选择:

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

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