久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 OpenCV 中有效地將大型 Mat 加載到內存中

Efficiently load a large Mat into memory in OpenCV(在 OpenCV 中有效地將大型 Mat 加載到內存中)
本文介紹了在 OpenCV 中有效地將大型 Mat 加載到內存中的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

是否有比 OpenCV 中的 FileStorage 方法更有效的方法將大型 Mat 對象加載到內存中?

Is there a more efficient way to load a large Mat object into memory than the FileStorage method in OpenCV?

我有一個包含 192 列和 100 萬行的大型 Mat,我想將其本地存儲在一個文件中并加載到內存中,然后我的應用程序啟動.使用 FileStorage 沒有問題,但我想知道是否有更有效的方法來做到這一點.目前在Visual Studio中使用Debug模式將Mat加載到內存大約需要5分鐘,在Release模式下需要大約3分鐘,數據文件大小約為1.2GB.

I have a large Mat with 192 columns and 1 million rows I want to store locally in a file and load into memory then my application starts. There is no problem using the FileStorage, but I was wondering if there exists a more efficient method to do this. At the moment it takes about 5 minutes to load the Mat into memory using the Debug mode in Visual Studio and around 3 minutes in the Release mode and the size of the data file is around 1.2GB.

FileStorage 方法是唯一可用于執行此任務的方法嗎?

Is the FileStorage method the only method available to do this task?

推薦答案

100x 加速你是否滿意?

Are you ok with a 100x speedup?

您應該以二進制格式保存和加載圖像.您可以使用下面代碼中的 matwritematread 函數來實現.

You should save and load your images in binary format. You can do that with the matwrite and matread function in the code below.

我測試了從 FileStorage 和二進制文件加載,對于 250K 行、192 列的較小圖像,輸入 CV_8UC1 我得到了這些結果(時間在女士):

I tested both loading from a FileStorage and the binary file, and for a smaller image with 250K rows, 192 columns, type CV_8UC1 I got these results (time in ms):

// Mat: 250K rows, 192 cols, type CV_8UC1
Using FileStorage: 5523.45
Using Raw:         50.0879    

使用我得到的二進制模式(時間以毫秒為單位)在具有 100 萬行和 192 列的圖像上:

On a image with 1M rows and 192 cols using the binary mode I got (time in ms):

// Mat: 1M rows, 192 cols, type CV_8UC1
Using FileStorage: (can't load, out of memory)
Using Raw:         197.381

注意

  1. 永遠不要在調試中衡量性能.
  2. 加載矩陣的 3 分鐘似乎太多了,即使對于 FileStorage 也是如此.但是,切換到二進制模式會帶來很多好處.
  1. Never measure performance in debug.
  2. 3 minutes to load a matrix seems way too much, even for FileStorages. However, you'll gain a lot switching to binary mode.

這里是帶有 matwritematread 函數的代碼,以及測試:

Here the code with the functions matwrite and matread, and the test:

#include <opencv2opencv.hpp>
#include <iostream>
#include <fstream>

using namespace std;
using namespace cv;


void matwrite(const string& filename, const Mat& mat)
{
    ofstream fs(filename, fstream::binary);

    // Header
    int type = mat.type();
    int channels = mat.channels();
    fs.write((char*)&mat.rows, sizeof(int));    // rows
    fs.write((char*)&mat.cols, sizeof(int));    // cols
    fs.write((char*)&type, sizeof(int));        // type
    fs.write((char*)&channels, sizeof(int));    // channels

    // Data
    if (mat.isContinuous())
    {
        fs.write(mat.ptr<char>(0), (mat.dataend - mat.datastart));
    }
    else
    {
        int rowsz = CV_ELEM_SIZE(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);

    // Header
    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

    // Data
    Mat mat(rows, cols, type);
    fs.read((char*)mat.data, CV_ELEM_SIZE(type) * rows * cols);

    return mat;
}

int main()
{
    // Save the random generated data
    {
        Mat m(1024*256, 192, CV_8UC1);
        randu(m, 0, 1000);

        FileStorage fs("fs.yml", FileStorage::WRITE);
        fs << "m" << m;

        matwrite("raw.bin", m);
    }

    // Load the saved matrix

    {
        // Method 1: using FileStorage
        double tic = double(getTickCount());

        FileStorage fs("fs.yml", FileStorage::READ);
        Mat m1;
        fs["m"] >> m1;

        double toc = (double(getTickCount()) - tic) * 1000. / getTickFrequency();
        cout << "Using FileStorage: " << toc << endl; 
    }

    {
        // Method 2: usign raw binary data
        double tic = double(getTickCount());

        Mat m2 = matread("raw.bin");

        double toc = (double(getTickCount()) - tic) * 1000. / getTickFrequency();
        cout << "Using Raw: " << toc << endl;
    }

    int dummy;
    cin >> dummy;

    return 0;
}

這篇關于在 OpenCV 中有效地將大型 Mat 加載到內存中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉圖像而不使用 OpenCV 函數)
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設置 SVM 參數)
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 日韩在线成人 | 高清黄色毛片 | 亚洲国产精品一区在线观看 | 成人久草| 国产一区欧美 | 五月婷婷在线播放 | 久在线视频 | 亚洲精品中文字幕在线观看 | 91久久国产综合久久 | 一级毛片免费 | 亚洲成人一区 | 欧美成人专区 | 成人在线观看免费视频 | 精品国产18久久久久久二百 | 欧美黄a| 国产精品国产精品国产专区不卡 | 欧美日韩精品一区二区三区四区 | 视频一区二区三区中文字幕 | 极品电影院 | 91极品尤物在线播放国产 | 久久国产精品视频 | 色综合天天天天做夜夜夜夜做 | 91九色porny首页最多播放 | 日本在线中文 | 国产98色在线 | 日韩 | 国产高清在线精品一区二区三区 | 国产毛片毛片 | 免费成人av | 韩日一区二区 | 免费的av| 亚洲成av人片在线观看无码 | 亚洲欧美视频一区 | 99爱视频 | 亚洲精品视频在线看 | 国产小u女发育末成年 | 成人免费视频网站在线观看 | 日韩在线观看中文字幕 | 精久久久久 | 国产一区二区在线播放 | 综合久久av | 国产乱肥老妇国产一区二 |