GML和SML的C++实现

需要自己处理出原始直方图,并给定一个理想直方图。

#include 
#include 
using namespace std;
const int N = 8;

double origin[N] = {
     0.174, 0.088, 0.086, 0.08, 0.068, 0.058, 0.062, 0.384};
double given[N] = {
     0, 0.4, 0, 0, 0.2, 0, 0, 0.4};
double origin_sum[N], given_sum[N];
double gml_histogram[N], sml_histogram[N];
int sml_map[N], gml_map[N];
void SML()
{
     
    int last_pos = 0;
    for( int i = 0; i < N; ++ i )
    {
     
        double minV = 1E9; int pos = -1;
        for( int j = last_pos; j < N; ++ j )
        {
     
            if( !given[j] ) continue;
            if( minV > abs( origin_sum[i] - given_sum[j] ) )
            {
     
                minV = abs( origin_sum[i] - given_sum[j] );
                pos = j;
            }
        }
        sml_map[i] = pos; last_pos = pos;
    }
    for( int i = 0; i < N; ++ i ) sml_histogram[sml_map[i]] += origin[i];
}
void GML()
{
     
    int last = -1;
    for( int i = 0; i < N; ++ i )
    {
     
        if( !given[i] ) continue;
        int pos = -1; double minV = 1E9;
        for( int j = last; j < N; ++ j )
        {
     
            if( !origin[j] ) continue;
            if( minV > abs(given_sum[i] - origin_sum[j]) )
            {
     
                minV = abs(given_sum[i] - origin_sum[j]);
                pos = j;
            }
        }
        for( int j = last; j <= pos; ++ j ) gml_map[j] = i;
        last = pos + 1;
    }
    for( int i = 0; i < N; ++ i ) gml_histogram[gml_map[i]] += origin[i];
}
void Print()
{
     
    double ans = 0;
    cout << "origin histogram:" << endl;
    for( auto i : origin ) printf("%.3f ", i );
    cout << "\n\nSML mapping:" << endl;
    for( int i = 0; i < N; ++ i )
        cout << sml_map[i] << " ";
    cout << "\nthe histogram after SML: " << endl;
    for( int i = 0; i < N; ++ i  )
    {
     
        ans += abs(sml_histogram[i] - given[i]);
        printf("%.3f ", sml_histogram[i]);
    }
    cout << "\nSML error:" << ans << endl;

    ans = 0;
    cout << "\n\nGML mapping:" << endl;
    for( int i = 0; i < N; ++ i )
        cout << gml_map[i] << " ";
    cout << "\nthe histogram after GML: " << endl;
    for( int i = 0; i < N; ++ i )
    {
     
        ans += abs(gml_histogram[i] - given[i]);
        printf("%.3f ", gml_histogram[i]);
    }
    cout << "\nGML error:" << ans << endl;
}
int main()
{
     
    for( int i = 0; i < N; ++ i )
    {
     
        if( !i ) {
      origin_sum[i] = origin[i]; given_sum[i] = given[i]; continue; }
        origin_sum[i] = origin_sum[i-1] + origin[i];
        given_sum[i] = given_sum[i - 1] + given[i];
    }
    GML(); SML();
    Print();
}

你可能感兴趣的:(数字视频处理)