Skip to content

Instantly share code, notes, and snippets.

@komakai
Created September 17, 2023 08:39
Show Gist options
  • Save komakai/ef137f68d7c31594ff1c00a103c89206 to your computer and use it in GitHub Desktop.
Save komakai/ef137f68d7c31594ff1c00a103c89206 to your computer and use it in GitHub Desktop.
Read OpenCV Mat from file
// refer to dumpMat gist regarding how to generate a file readable by this function
void readMatFromFile(std::string fileName, Mat& m) {
std::ifstream file(fileName, std::ios::binary | std::ios::ate);
std::streamsize size = file.tellg();
if (size == 0) {
std::cout << "File is empty or does not exist\n";
}
file.seekg(0, std::ios::beg);
file.read((char*)m.data, MIN(size, m.total()*m.elemSize()));
file.close();
}
Mat m(480, 640, CV_32F);
readMatFromFile("m_640x480_32FC1.bin", m);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment