Reputation:
How can I read .mat file in C++ without using Any external libraries (matio etc.) or API?
I know what is inside of the file:
I have tried doing it with fstream but the output is not correct ( i am sure that file is not the problem since i have checked it in MATLAB.)
std::ifstream file("file.mat", std::binary);
if (!file.is_open()){
std::cerr << "error opening the file" << std::endl;
return 1;
}
file.seekg(2665452);
cv::Mat img1(512,640, CV_64F);
file.read( reinterpret_cast<char*> (img1.data),2621440);
file.close();
cv::Mat img1P;
img1.convertTo(img1P,CV_8U);
cv.imwrite("img1P.png",img1P);
Upvotes: 0
Views: 112
Reputation: 41474
What's "inside the file" is not just the bytes corresponding to those three objects, but the structures and data which are standard for a MAT file, and those objects serialized into the form induced by the MAT file format. If you don't want to use an external library which parses MAT files, then you will need to write your own MAT file parser. A description of the MAT file format is here. Making a MAT file parser will be a long and involved process.
Upvotes: 7