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

how to convert cv::Mat to cv::Matx33f

本文关键字:cv Matx33f 转换 Mat      更新时间:2023-10-16

我有一个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;
}

要从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;

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

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));

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;

现在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);