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

Eng*_*ine 2 c++ opencv

我有一个cv::Mat我想要转换成一个cv::Matx33f.我尝试这样做:

cv::Mat m;
cv::Matx33f m33;
.........
m33 = m;
Run Code Online (Sandbox Code Playgroud)

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

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

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;
}
Run Code Online (Sandbox Code Playgroud)

小智 11

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

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

cv::Matx33f m33((float*)m.ptr());
Run Code Online (Sandbox Code Playgroud)

这应该做的工作,假设m中的连续记忆.你可以通过以下方式检查:

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

std::cout << "m33 " << m33 << std::endl;
Run Code Online (Sandbox Code Playgroud)

  • 最好使用m.clone().ptr()而不是m.ptr().这样,即使原始矩阵m在内存中不连续,它也能工作. (7认同)