Ask Your Question
0

imwrite very slow (big Mat)

asked 2017-11-02 10:45:59 -0600

aniskraini@yahoo.fr gravatar image

please i need to save a mat of M=Mat::zeros(10 000,10 0000) in png file. The problem is the time of "imwrite" this function is very slower (with a big mat) any help please ?

edit retag flag offensive close merge delete

Comments

what do you mean with "slow". How long does it take?

VxW gravatar imageVxW ( 2017-11-02 11:10:08 -0600 )edit

i need to know if i can use some multithreading to save image

aniskraini@yahoo.fr gravatar image[email protected] ( 2017-11-02 11:10:52 -0600 )edit

@VxW: in fact each image take 5/6 scond my code is like :

dst = Mat::zeros(h_a4, w_a4, CV_8UC3); imwrite("test.png", dst);

aniskraini@yahoo.fr gravatar image[email protected] ( 2017-11-02 11:12:30 -0600 )edit

i just wonder: what is the purpose of dst = Mat::zeros(h_a4, w_a4, CV_8UC3); imwrite("test.png", dst); is it just for test?

sturkmen gravatar imagesturkmen ( 2017-11-02 11:19:09 -0600 )edit

do you need 'png'? if you are using png most of the time is used for compression (depending on the compression level). Writung the raw data should be faster, i.e. 'bmp'. Multithreading is useable if you have several images/frames

VxW gravatar imageVxW ( 2017-11-02 11:20:23 -0600 )edit

@sturkmen : the goal is to save many small images (not having the same size) in a big image of (10000,100000). i have 150 frames and i try to save the maximum number of frame in different big images. but i have the problem of speed (with png or bmp i have the same time :( )

aniskraini@yahoo.fr gravatar image[email protected] ( 2017-11-02 11:29:35 -0600 )edit

you can try to change some params. here you can find an example code EDIT: it seems default params for saving png is faster.

sturkmen gravatar imagesturkmen ( 2017-11-02 11:44:18 -0600 )edit

on my computer I have the following benchmark: png: 7.3sec bmp:1.7sec

VxW gravatar imageVxW ( 2017-11-02 11:44:58 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-11-02 12:26:20 -0600

it is not an answer for your question.

just wanted to share the code below about testing saving time using different ImwritePNGFlags

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>

using namespace cv;
using namespace std;

void createAlphaMat(Mat &mat)
{
    CV_Assert(mat.channels() == 4);
    for (int i = 0; i < mat.rows; ++i) {
        for (int j = 0; j < mat.cols; ++j) {
            Vec4b& bgra = mat.at<Vec4b>(i, j);
            bgra[0] = UCHAR_MAX; // Blue
            bgra[1] = saturate_cast<uchar>((float(mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX); // Green
            bgra[2] = saturate_cast<uchar>((float(mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX); // Red
            bgra[3] = saturate_cast<uchar>(0.5 * (bgra[1] + bgra[2])); // Alpha
        }
    }
}

int main(int argc, char** argv)
{
    // Create mat with alpha channel
    Mat mat(4800, 6400, CV_8UC4);
    createAlphaMat(mat);

    vector<int> compression_params;
    compression_params.push_back(IMWRITE_PNG_COMPRESSION);
    compression_params.push_back(0);
    compression_params.push_back(IMWRITE_PNG_STRATEGY);
    compression_params.push_back(IMWRITE_PNG_STRATEGY_DEFAULT);
    for (int i = 0; i < 10; i++)
        for (int j = 0; j < 5; j++)
        {
            compression_params[1] = i;
            compression_params[3] = j;
            double t = (double)getTickCount();
            imwrite(format("PNG_STRATEGY_%d_PNG_COMPRESSION_%d.png", j, i), mat, compression_params);
            t = ((double)getTickCount() - t) / getTickFrequency();
            cout << format("PNG_STRATEGY_%d_PNG_COMPRESSION_%d.png", j, i);
            cout << " times passed in seconds: " << t << endl;
        }
    double t = (double)getTickCount();
    imwrite("PNG_SAVED_DEFAULT.png", mat);
    t = ((double)getTickCount() - t) / getTickFrequency();
    cout << "DEFAULT times passed in seconds: " << t << endl;
    imshow("Sample", mat);
    waitKey();
    return 0;
}
edit flag offensive delete link more

Comments

thank you for your help :) :)

aniskraini@yahoo.fr gravatar image[email protected] ( 2017-11-02 13:45:52 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-11-02 10:45:59 -0600

Seen: 4,530 times

Last updated: Nov 02 '17