如何将cv :: Mat转换为cv :: Matx33f

时间:2013-07-29 08:58:03

标签: c++ opencv

我想要cv::Mat转换为cv::Matx33f。我试着这样做:

cv::Mat m;
cv::Matx33f m33;
.........
m33 = m;

但所有数据都丢失了!知道怎么做吗?

更新 这是导致我的问题的代码的一部分:

cv::Point2f Order::warpPoint(cv::Point2f pTmp){
    cv::Matx33f warp = this->getTransMatrix() ; // the getter gives a cv::Mat back 
    transformMatrix.copyTo(warp); // because the first method didn't work, I tried to use the copyto function 

    // and the last try was 
    warp = cv::Matx33f(transformationMatrix); // and waro still 0 
    cv::Point3f  warpPoint = cv::Matx33f(transformMatrix)*pTmp;
    cv::Point2f result(warpPoint.x, warpPoint.y);
    return result;
}

4 个答案:

答案 0 :(得分:10)

要从Mat转换为Matx,可以使用数据指针。例如,

cv::Mat m; // assume we know it is CV_32F type, and its size is 3x3

cv::Matx33f m33((float*)m.ptr());

这应该可以完成这项工作,假设m中的连续记忆。您可以通过以下方式查看:

std::cout << "m " << m << std::endl;

std::cout << "m33 " << m33 << std::endl;

答案 1 :(得分:3)

http://opencv.willowgarage.com/documentation/cpp/core_basic_structures.html说:

“如果你需要在未实现的Matx上做一些操作,很容易将矩阵转换为Mat和向后。”

Matx33f m(1, 2, 3,
          4, 5, 6,
          7, 8, 9);
cout << sum(Mat(m*m.t())) << endl;

答案 2 :(得分:0)

我意识到问题已经过时了,但这也应该有效:

auto m33 = Matx33f(m.at<float>(0, 0), m.at<float>(0, 1), m.at<float>(0, 2),
                   m.at<float>(1, 0), m.at<float>(1, 1), m.at<float>(1, 2),
                   m.at<float>(2, 0), m.at<float>(2, 1), m.at<float>(2, 2));

答案 3 :(得分:0)

现在在 cv::Mat 类中提供了两种方式的特殊转换运算符:

cv::Mat {
    template<typename _Tp, int m, int n> operator Matx<_Tp, m, n>() const;
}

        cv::Mat tM = cv::getPerspectiveTransform(uvp, svp);
        auto ttM = cv::Matx33f(tM);
        ...
        tM = cv::Mat(ttM);