c++opencvmatrixdouble

convert Matrix of type CV_32FC1 to CV_64FC1


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

Solution

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

    There is the method 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);