3

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

4 回答 4

12

To convert from Mat to Matx, one can use the data pointer. For example,

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

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

This should do the job, assuming continuous memory in m. You can check it by:

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

std::cout << "m33 " << m33 << std::endl;
于 2015-03-31T03:04:21.470 回答
2

我意识到这个问题很老了,但这也应该有效:

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));
于 2016-07-21T10:57:50.920 回答
1

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

"If you need to do some operation on Matx that is not implemented, it is easy to convert the matrix to Mat and backwards."

Matx33f m(1, 2, 3,
          4, 5, 6,
          7, 8, 9);
cout << sum(Mat(m*m.t())) << endl;
于 2013-07-29T09:33:24.350 回答
-1

现在在 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);
于 2021-06-04T10:47:43.617 回答