Home > Backend Development > C++ > How Can I Efficiently Load Large OpenCV Mat Objects into Memory?

How Can I Efficiently Load Large OpenCV Mat Objects into Memory?

Susan Sarandon
Release: 2024-12-05 16:09:16
Original
594 people have browsed it

How Can I Efficiently Load Large OpenCV Mat Objects into Memory?

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:

  • FileStorage: 5523.45 ms
  • Binary Format: 50.0879 ms

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&amp; filename)
{
    ifstream fs(filename, fstream::binary);
    int rows, cols, type, channels;
    fs.read((char*)&amp;rows, sizeof(int));         // rows
    fs.read((char*)&amp;cols, sizeof(int));         // cols
    fs.read((char*)&amp;type, sizeof(int));         // type
    fs.read((char*)&amp;channels, sizeof(int));     // channels
    Mat mat(rows, cols, type);
    fs.read((char*)mat.data, CV_ELEM_SIZE(type) * rows * cols);
    return mat;
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template