Engine
Engine

Reputation: 5432

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

I have a cv::Mat that I want to convert into a cv::Matx33f. I try to do it like this:

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

but all the data gets lost! Any idea how to do this ?

UPDATE here is a part of the code which causes my problem :

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

Upvotes: 3

Views: 8554

Answers (4)

Kikaxa
Kikaxa

Reputation: 152

There are now special conversion operators available in cv::Mat class for both ways:

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

Upvotes: 0

Toke Nielsen
Toke Nielsen

Reputation: 138

I realise that the question is old, but this should also work:

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

Upvotes: 1

Ocean CV
Ocean CV

Reputation: 119

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;

Upvotes: 11

Barshan Das
Barshan Das

Reputation: 3767

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;

Upvotes: 0

Related Questions