How to write a Float Mat to a file in OpenCV

后端 未结 5 2058
一个人的身影
一个人的身影 2020-12-01 09:09

I have a matrix

Mat B(480,640,CV_32FC1);

containing floating values..I want to write this matrix to a file which could be opened in notepad or M

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 09:44

    Using pure OpenCV API calls:

    // Declare what you need
    cv::FileStorage file("some_name.ext", cv::FileStorage::WRITE);
    cv::Mat someMatrixOfAnyType;
    
    // Write to file!
    file << "matName" << someMatrixOfAnyType;
    

    The file extension can be xml or yml. In both cases you get a small header that you can easily remove/parse, then you have access to the data in a floating point format. I used this approach successfully (with yml files) to get data into Matlab and Matplotlib

    To get the data:

    1. open the file with any editor
    2. then suppress all the text and numbers except the content of the data tag (i.e., the pixel values).
    3. When done, save your file with a txt or csv extension and open it with matlab (drag-and-drop works).

    Voilà. You may have to reshape the resulting matrix in matlab command line if it didn't guess correctly the image size.

提交回复
热议问题