convert Matrix of type CV_32FC1 to CV_64FC1

三世轮回 提交于 2021-02-05 14:36:53

问题


How do I convert a cv::Mat of type CV_32FC1 to the type CV_64FC1 (equivalent to a change from float to double)?

I am opening a Matrix that was saved as XML (cvSave) but as a float. This means that the field <dt> has the value f in the file. I need to change it to d to open it. But I'd rather not do this, instead I'd like to open it directly as a Matrix with elements of type double, or convert it later from float to double.

Below is my code for opening the file.

/** Load cv::Mat from XML file. 
 */
cv::Mat loadMat(const std::string filename)
{
    cv::Mat result;
    cv::FileStorage fs(filename, cv::FileStorage::READ);
    fs.getFirstTopLevelNode() >> result;
    return result;
}

回答1:


Okay, I'm a dimwit. Here is how it goes:

There is the function convertTo that does exactly what I want.

Thanks for matrix type conversion in opencv for pointing this out.

Here is how I do it:

cv::Mat A = loadMat("mymat.xml"); // See function loadMat in the question!
A.convertTo(A, CV_64F);


来源:https://stackoverflow.com/questions/6910332/convert-matrix-of-type-cv-32fc1-to-cv-64fc1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!