Ask Your Question

Revision history [back]

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;
}