Boosting Performance: Efficiently Loading Large Mat Objects using Binary Files
Loading massive Mat objects into memory is crucial for various OpenCV applications. While the FileStorage method is a common approach, it may not be the most efficient option for handling large data sets. Here's an alternative technique that promises a notable performance boost.
Binary Format: The Key to Speed and Efficiency
The secret lies in saving and loading images in binary format. Using the matwrite and matread functions, we can achieve remarkable speed gains compared to the FileStorage method.
Benchmarking Results: A World of Difference
In tests conducted with a 250K rows x 192 columns image (CV_8UC1), the performance difference is striking:
For a larger image (1M rows x 192 columns), the FileStorage method failed due to out-of-memory errors, while the binary mode handled it effortlessly in just 197.381 ms.
Code Implementation: Simplified and Effective
Here's the code snippet with the matwrite and matread functions, along with a test to illustrate their performance gains:
void matwrite(const string& filename, const Mat& mat) { ofstream fs(filename, fstream::binary); fs.write((char*)&mat.rows, sizeof(int)); // rows fs.write((char*)&mat.cols, sizeof(int)); // cols fs.write((char*)&mat.type, sizeof(int)); // type fs.write((char*)&mat.channels, sizeof(int)); // channels if (mat.isContinuous()) { fs.write(mat.ptr<char>(0), (mat.dataend - mat.datastart)); } else { int rowsz = CV_ELEM_SIZE(mat.type) * mat.cols; for (int r = 0; r < mat.rows; ++r) { fs.write(mat.ptr<char>(r), rowsz); } } } Mat matread(const string& filename) { ifstream fs(filename, fstream::binary); int rows, cols, type, channels; fs.read((char*)&rows, sizeof(int)); // rows fs.read((char*)&cols, sizeof(int)); // cols fs.read((char*)&type, sizeof(int)); // type fs.read((char*)&channels, sizeof(int)); // channels Mat mat(rows, cols, type); fs.read((char*)mat.data, CV_ELEM_SIZE(type) * rows * cols); return mat; }
Conclusion: Unlocking a New Level of Performance
By embracing binary file format, you gain a significant performance advantage when loading large Mat objects into memory. This technique can drastically reduce loading times, allowing your applications to work more efficiently with massive datasets.
The above is the detailed content of How Can I Efficiently Load Large OpenCV Mat Objects into Memory?. For more information, please follow other related articles on the PHP Chinese website!