I am using openCV and I have a 95,1 mat object of type CV_32F, which I would like to write to a binary file. I am using the code below however I cannot cast a 32F to an char type. Are there any suggestions? I also want to perform the reverse procedure of reading a binary file and storing the values into a mat object of the same type.
try{
ofstream posBinary;
posBinary.open("C:/Users/Dr.Mollica/Documents/TSR Datasets/signDatabasePublicFramesOnly/posSamps.bin", ios::out | ios::binary);
posBinary.write((char *)featureVector, sizeof(featureVector);
}
catch (exception X){ cout << "Error! Could not write Binary file" << endl; }
Also to note, the reason I want to do it in a binary file is I will be writing a large number of these vectors to the file which will be read in to a machine learning algorithm. And from my understanding reading and writing to a binary file is the fastest way possible.
I recommend you to use OpenCV APIs to write cv::Mat
to xml or yml files, which you can later read the cv::Mat
back from them easily.
For example:
cv::Mat yourMatData;
// write Mat to file
cv::FileStorage fs("file.yml", cv::FileStorage::WRITE);
fs << "yourMat" << yourMatData;
// read Mat from file
cv::FileStorage fs2("file.yml", FileStorage::READ);
fs2["yourMat"] >> yourMatData;
Updated: If you prefer to write/read them to/from binary files, you can first convert cv::Mat
to array/vector. And then write the array/vector to files.
To read on, check out Convert Mat to Array/Vector in OpenCV.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With