imwrite very slow (big Mat)
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 ?
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 ?
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;
}
Asked: 2017-11-02 10:45:59 -0600
Seen: 5,009 times
Last updated: Nov 02 '17
what do you mean with "slow". How long does it take?
i need to know if i can use some multithreading to save image
@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);
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?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
@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 :( )
you can try to change some params. here you can find an example code EDIT: it seems default params for saving png is faster.
on my computer I have the following benchmark: png: 7.3sec bmp:1.7sec