JPG BMP TIF PNG 图像编码压缩率和编解码时间比较

Test picture compression format .bmp:
Time of reading : 28.68 ms
Time of writing : 6.79 ms
Time of decoding : 3.98 ms
Time of encoding : 4.82 ms
Encode radio : 100.00%
Psnr : 361.20

Test picture compression format .jpg:
Time of reading : 0.99 ms
Time of writing : 0.54 ms
Time of decoding : 19.51 ms
Time of encoding : 20.15 ms
Encode radio : 5.77%
Psnr : 39.71

Test picture compression format .tif:
Time of reading : 8.79 ms
Time of writing : 2.85 ms
Time of decoding : 70.26 ms
Time of encoding : 97.59 ms
Encode radio : 38.89%
Psnr : 361.20

Test picture compression format .png:
Time of reading : 5.53 ms
Time of writing : 2.38 ms
Time of decoding : 76.55 ms
Time of encoding : 216.28 ms
Encode radio : 32.01%
Psnr : 361.20

具体测试程序:

#include <stdio.h>
#include "m_tools.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <fstream>

using namespace std;

int calPsnr(const string filepath, const char * _ext, double& t_read, double& t_write, double& t_decode, double& t_encode, double& encode_ratio, double & psnr)
{
    long long t0 = _getTickCount();
    cv::Mat src_bmp = cv::imread(filepath);
    long long t1 = _getTickCount();

    int w = src_bmp.cols;
    int h = src_bmp.rows;

    const std::string ext = _ext;

    vector<int> opts;

    if( ext == ".png" ){
        opts.push_back(cv::IMWRITE_PNG_COMPRESSION);
        opts.push_back( 9);
    }else if ( ext == ".jpg" ){
        opts.push_back(cv::IMWRITE_JPEG_QUALITY);
        opts.push_back( 90);
    }else if ( ext == ".bmp" ){
    }


    std::vector<uchar> pkg_buff;
    cv::imencode(ext, src_bmp, pkg_buff, opts);


    long long t2 = _getTickCount();

    cv::Mat src_png = cv::imdecode(pkg_buff,CV_LOAD_IMAGE_COLOR);
    long long t3 = _getTickCount();

    {
        FILE* file = fopen((string("dst")+ext).c_str(), "w" );
        fwrite(&pkg_buff[0],1, pkg_buff.size(), file );
        fclose(file);
    }
    long long t4 = _getTickCount();
    {
        std::ifstream f(string("dst")+ext);
        std::string s;
        while (f>>s)
            ;
    }
    long long t5 = _getTickCount();


    double _psnr= cv::PSNR( src_bmp, src_png );
    psnr += _psnr;

    //printf("Psnr of %dx%d%s and %s is %.2f ",  w, h, filepath.c_str(), ext.c_str(), _psnr);

    float src_kb = w*h*3/1024.0;
    float dst_kb = pkg_buff.size()/1024.0;

    encode_ratio += dst_kb*100.0/src_kb;

    //printf("In=%.2fKB Out=%.2fKB Compression ratio=%.2f%
", src_kb, dst_kb, dst_kb*100.0/src_kb);

    t_read += (t5-t4)/_getTickFrequency();
    t_write += (t4-t3) /_getTickFrequency();
    t_decode += (t3-t2) /_getTickFrequency();
    t_encode += (t2 - t1) /_getTickFrequency();
    
    return 0;
}


int main( int argc, char ** argv )
{
    int max_test_loops = 100;

    vector<string> paths = getFiles(argv[1]);

    max_test_loops = paths.size() < max_test_loops? paths.size() : max_test_loops;

    double t_read=0, t_write=0,  t_decode=0,  t_encode=0,  encode_ratio=0,  psnr=0;

    for( int i=0;i!=max_test_loops; ++i ){
        calPsnr(paths[i], argv[2], t_read, t_write,  t_decode,  t_encode,  encode_ratio,  psnr);
    }

    printf("Test picture compression format %s:
", argv[2]);
    printf("Time of reading  : %.2lf ms
", 1000.0*t_read/max_test_loops);
    printf("Time of writing  : %.2lf ms
", 1000.0*t_write/max_test_loops);
    printf("Time of decoding : %.2lf ms
", 1000.0*t_decode/max_test_loops);
    printf("Time of encoding : %.2lf ms
", 1000.0*t_encode/max_test_loops);

    printf("Encode radio     : %.2lf%% 
", encode_ratio/max_test_loops);
    printf("Psnr : %.2lf 

", psnr/max_test_loops);

    return 0;
}
原文地址:https://www.cnblogs.com/luoyinjie/p/13691808.html